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 PythonToSinkTarget = Arc<dyn Fn(Py<PyAny>) -> PyResult<Box<dyn Any>> + Send + Sync>;
8
9#[derive(Clone)]
10pub struct FromPythonConvertRegistry {
11    pub sink_target: PythonToSinkTarget,
12}
13
14#[derive(Clone)]
15pub struct PythonConvertRegistry {
16    pub from_py: FromPythonConvertRegistry,
17}
18
19static PYTHON_CONVERT_REGISTRY: LazyLock<RwLock<Option<PythonConvertRegistry>>> =
20    LazyLock::new(Default::default);
21
22pub fn get_python_convert_registry() -> PythonConvertRegistry {
23    PYTHON_CONVERT_REGISTRY
24        .deref()
25        .read()
26        .unwrap()
27        .as_ref()
28        .unwrap()
29        .clone()
30}
31
32pub fn register_converters(registry: PythonConvertRegistry) {
33    *PYTHON_CONVERT_REGISTRY.deref().write().unwrap() = Some(registry);
34}