Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-utils/src/python_convert_registry.rs
6939 views
1
use std::any::Any;
2
use std::ops::Deref;
3
use std::sync::{Arc, LazyLock, RwLock};
4
5
use pyo3::{Py, PyAny, PyResult};
6
7
pub type FromPython = Arc<dyn Fn(Py<PyAny>) -> PyResult<Box<dyn Any>> + Send + Sync>;
8
pub type ToPython = Arc<dyn Fn(Box<dyn Any>) -> PyResult<Py<PyAny>> + Send + Sync>;
9
10
#[derive(Clone)]
11
pub 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)]
20
pub struct ToPythonConvertRegistry {
21
pub df: ToPython,
22
pub series: ToPython,
23
pub dsl_plan: ToPython,
24
pub schema: ToPython,
25
}
26
27
#[derive(Clone)]
28
pub struct PythonConvertRegistry {
29
pub from_py: FromPythonConvertRegistry,
30
pub to_py: ToPythonConvertRegistry,
31
}
32
33
static PYTHON_CONVERT_REGISTRY: LazyLock<RwLock<Option<PythonConvertRegistry>>> =
34
LazyLock::new(Default::default);
35
36
pub 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
46
pub fn register_converters(registry: PythonConvertRegistry) {
47
*PYTHON_CONVERT_REGISTRY.deref().write().unwrap() = Some(registry);
48
}
49
50