monarch_types/
lib.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9#![feature(assert_matches)]
10
11mod pyobject;
12mod python;
13mod pytree;
14
15pub use pyobject::PickledPyObject;
16pub use python::SerializablePyErr;
17pub use python::TryIntoPyObjectUnsafe;
18pub use pytree::PyTree;
19
20/// Macro to generate a Python object lookup function with caching
21///
22/// # Arguments
23/// * `$fn_name` - Name of the Rust function to generate
24/// * `$python_path` - Path to the Python object as a string (e.g., "module.submodule.function")
25#[macro_export]
26macro_rules! py_global {
27    ($fn_name:ident, $python_module:literal, $python_class:literal) => {
28        fn $fn_name<'py>(py: ::pyo3::Python<'py>) -> ::pyo3::Bound<'py, ::pyo3::PyAny> {
29            static CACHE: ::pyo3::sync::GILOnceCell<::pyo3::PyObject> =
30                ::pyo3::sync::GILOnceCell::new();
31            CACHE
32                .import(py, $python_module, $python_class)
33                .unwrap()
34                .clone()
35        }
36    };
37}