polars_utils/
python_convert_registry.rs

1use std::any::Any;
2use std::ops::Deref;
3use std::sync::{Arc, LazyLock, RwLock};
4
5use pyo3::{Py, PyAny, PyResult};
6
7pub type FromPython = Arc<dyn Fn(Py<PyAny>) -> PyResult<Box<dyn Any>> + Send + Sync>;
8pub type ToPython = Arc<dyn Fn(Box<dyn Any>) -> PyResult<Py<PyAny>> + Send + Sync>;
9
10#[derive(Clone)]
11pub struct FromPythonConvertRegistry {
12    pub partition_target_cb_result: FromPython,
13    pub series: FromPython,
14    pub df: FromPython,
15    pub dsl_plan: FromPython,
16    pub schema: FromPython,
17}
18
19#[derive(Clone)]
20pub struct ToPythonConvertRegistry {
21    pub df: ToPython,
22    pub series: ToPython,
23    pub dsl_plan: ToPython,
24    pub schema: ToPython,
25}
26
27#[derive(Clone)]
28pub struct PythonConvertRegistry {
29    pub from_py: FromPythonConvertRegistry,
30    pub to_py: ToPythonConvertRegistry,
31}
32
33static PYTHON_CONVERT_REGISTRY: LazyLock<RwLock<Option<PythonConvertRegistry>>> =
34    LazyLock::new(Default::default);
35
36pub fn get_python_convert_registry() -> PythonConvertRegistry {
37    PYTHON_CONVERT_REGISTRY
38        .deref()
39        .read()
40        .unwrap()
41        .as_ref()
42        .unwrap()
43        .clone()
44}
45
46pub fn register_converters(registry: PythonConvertRegistry) {
47    *PYTHON_CONVERT_REGISTRY.deref().write().unwrap() = Some(registry);
48}