1#![feature(assert_matches)]
10
11mod pyobject;
12mod python;
13mod pytree;
14
15use pyo3::PyErr;
16use pyo3::exceptions::PyValueError;
17pub use pyobject::PickledPyObject;
18pub use python::SerializablePyErr;
19pub use python::TryIntoPyObjectUnsafe;
20pub use pytree::PyTree;
21
22#[macro_export]
28macro_rules! py_global {
29 ($fn_name:ident, $python_module:literal, $python_class:literal) => {
30 fn $fn_name<'py>(py: ::pyo3::Python<'py>) -> ::pyo3::Bound<'py, ::pyo3::PyAny> {
31 static CACHE: ::pyo3::sync::GILOnceCell<::pyo3::PyObject> =
32 ::pyo3::sync::GILOnceCell::new();
33 CACHE
34 .import(py, $python_module, $python_class)
35 .unwrap()
36 .clone()
37 }
38 };
39}
40
41pub trait MapPyErr<T> {
42 fn map_pyerr(self) -> Result<T, PyErr>;
43}
44impl<T, E> MapPyErr<T> for Result<T, E>
45where
46 E: ToString,
47{
48 fn map_pyerr(self) -> Result<T, PyErr> {
49 self.map_err(|err| PyErr::new::<PyValueError, _>(err.to_string()))
50 }
51}