Path: blob/main/crates/polars-utils/src/python_convert_registry.rs
6939 views
use std::any::Any;1use std::ops::Deref;2use std::sync::{Arc, LazyLock, RwLock};34use pyo3::{Py, PyAny, PyResult};56pub type FromPython = Arc<dyn Fn(Py<PyAny>) -> PyResult<Box<dyn Any>> + Send + Sync>;7pub type ToPython = Arc<dyn Fn(Box<dyn Any>) -> PyResult<Py<PyAny>> + Send + Sync>;89#[derive(Clone)]10pub struct FromPythonConvertRegistry {11pub partition_target_cb_result: FromPython,12pub series: FromPython,13pub df: FromPython,14pub dsl_plan: FromPython,15pub schema: FromPython,16}1718#[derive(Clone)]19pub struct ToPythonConvertRegistry {20pub df: ToPython,21pub series: ToPython,22pub dsl_plan: ToPython,23pub schema: ToPython,24}2526#[derive(Clone)]27pub struct PythonConvertRegistry {28pub from_py: FromPythonConvertRegistry,29pub to_py: ToPythonConvertRegistry,30}3132static PYTHON_CONVERT_REGISTRY: LazyLock<RwLock<Option<PythonConvertRegistry>>> =33LazyLock::new(Default::default);3435pub fn get_python_convert_registry() -> PythonConvertRegistry {36PYTHON_CONVERT_REGISTRY37.deref()38.read()39.unwrap()40.as_ref()41.unwrap()42.clone()43}4445pub fn register_converters(registry: PythonConvertRegistry) {46*PYTHON_CONVERT_REGISTRY.deref().write().unwrap() = Some(registry);47}484950