minor optimization

This commit is contained in:
Erik Friese 2023-08-26 21:47:35 +02:00
parent 26da86d2cd
commit d848d05710

View File

@ -3,7 +3,7 @@ use crate::{
py_any_extras::PyAnyExtras, py_any_extras::PyAnyExtras,
}; };
use prost_reflect::{DynamicMessage, ReflectMessage, Value}; use prost_reflect::{DynamicMessage, ReflectMessage, Value};
use pyo3::{IntoPy, PyAny}; use pyo3::{PyAny, ToPyObject, PyObject};
pub fn merge_msg_into_pyobj(obj: &PyAny, msg: &DynamicMessage) -> Result<()> { pub fn merge_msg_into_pyobj(obj: &PyAny, msg: &DynamicMessage) -> Result<()> {
for field in msg.descriptor().fields() { for field in msg.descriptor().fields() {
@ -19,26 +19,26 @@ pub fn merge_msg_into_pyobj(obj: &PyAny, msg: &DynamicMessage) -> Result<()> {
Ok(()) Ok(())
} }
fn map_field_value<'py>( fn map_field_value(
field_name: &str, field_name: &str,
field_value: &Value, field_value: &Value,
proto_meta: &'py PyAny, proto_meta: &PyAny,
) -> Result<&'py PyAny> { ) -> Result<PyObject> {
let py = proto_meta.py(); let py = proto_meta.py();
match field_value { match field_value {
Value::Bool(x) => Ok(x.into_py(py).into_ref(py)), Value::Bool(x) => Ok(x.to_object(py)),
Value::Bytes(x) => Ok(x.into_py(py).into_ref(py)), Value::Bytes(x) => Ok(x.to_object(py)),
Value::F32(x) => Ok(x.into_py(py).into_ref(py)), Value::F32(x) => Ok(x.to_object(py)),
Value::F64(x) => Ok(x.into_py(py).into_ref(py)), Value::F64(x) => Ok(x.to_object(py)),
Value::I32(x) => Ok(x.into_py(py).into_ref(py)), Value::I32(x) => Ok(x.to_object(py)),
Value::I64(x) => Ok(x.into_py(py).into_ref(py)), Value::I64(x) => Ok(x.to_object(py)),
Value::String(x) => Ok(x.into_py(py).into_ref(py)), Value::String(x) => Ok(x.to_object(py)),
Value::U32(x) => Ok(x.into_py(py).into_ref(py)), Value::U32(x) => Ok(x.to_object(py)),
Value::U64(x) => Ok(x.into_py(py).into_ref(py)), Value::U64(x) => Ok(x.to_object(py)),
Value::Message(msg) => { Value::Message(msg) => {
let obj = proto_meta.create_instance(field_name)?; let obj = proto_meta.create_instance(field_name)?;
merge_msg_into_pyobj(obj, msg)?; merge_msg_into_pyobj(obj, msg)?;
Ok(obj) Ok(obj.to_object(py))
} }
value => Err(Error::UnsupportedType(value.to_string())), value => Err(Error::UnsupportedType(value.to_string())),
} }