py_extract_any

Function py_extract_any 

Source
pub fn py_extract_any<'py, T>(
    py: Python<'py>,
    pyresult: &'py RUMPyAny,
) -> RUMResult<T>
where T: FromPyObject<'py, 'py> + Clone, <T as FromPyObject<'py, 'py>>::Error: Debug,
Expand description

Extract value returned from functions and modules via a PyAny object.

§Example Usage

§Example W/ RustType

use compact_str::format_compact;
    use pyo3::Python;
    use pyo3::types::{PyListMethods, PyAnyMethods, PyString};
    use crate::rumtk_core::scripting::python_utils::{py_extract_any, py_new_args, py_push_arg, RUMPyArgs, RUMPyList};
    use crate::rumtk_core::scripting::python_utils::{py_buildargs, py_extract_string_vector};


    Python::attach(|py| {
            let example_arg_1 = "Hello";
            let py_arg = PyString::new(py, example_arg_1);
            let arg: String = py_arg.extract().unwrap();
            let arg_1: String = py_extract_any(py, &py_arg.as_any().clone().unbind()).unwrap();
            assert_eq!(&example_arg_1, &arg_1, "{}", format_compact!("Python conversion failed!\nGot: {:?}\nExpected: {:?}", &arg_1, &example_arg_1));
        }
    )

§Example W/ Custom Type

use compact_str::format_compact;
    use pyo3::{Python, pyclass, IntoPyObjectExt};
    use pyo3::types::{PyListMethods, PyAnyMethods, PyString};
    use crate::rumtk_core::scripting::python_utils::{py_extract_any, py_new_args, py_push_arg, RUMPyAny, RUMPyArgs, RUMPyList};
    use crate::rumtk_core::scripting::python_utils::{py_buildargs, py_extract_string_vector};

    #[pyclass]
    #[derive(Clone, Debug, PartialOrd, PartialEq)]
    struct MyWrapper {
        text: String
    }

    Python::attach(|py| {
            let example_arg_1 = MyWrapper{text: String::from("Hello")};
            let py_arg: RUMPyAny = example_arg_1.clone().into_py_any(py).unwrap();
            let arg_1: MyWrapper = py_extract_any(py, &py_arg).unwrap();
            assert_eq!(&example_arg_1, &arg_1, "{}", format_compact!("Python conversion failed!\nGot: {:?}\nExpected: {:?}", &arg_1, &example_arg_1));
        }
    )