### PolarsAllocator Methods Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/struct Details the core methods for the PolarsAllocator, including its constructor and global allocator trait implementations. ```APIDOC ## PolarsAllocator Methods ### `new()` Creates a new `PolarsAllocator` instance. #### Signature ```rust pub const fn new() -> Self ``` ### `GlobalAlloc` Trait Implementation This section details the methods required by the `GlobalAlloc` trait, which `PolarsAllocator` implements. #### `alloc(layout: Layout) -> *mut u8` Allocates memory according to the specified `layout`. #### `dealloc(ptr: *mut u8, layout: Layout)` Deallocates a block of memory at `ptr` with the specified `layout`. #### `alloc_zeroed(layout: Layout) -> *mut u8` Allocates memory and ensures the contents are zeroed. #### `realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8` Reallocates (shrinks or grows) a memory block to `new_size`. ``` -------------------------------- ### PyTimeUnit Struct and Implementations Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Documentation for the PyTimeUnit struct, including its definition, and implementations of Clone, FromPyObject, and IntoPyObject traits. It also covers auto trait implementations like Send, Sync, and blanket implementations for generic traits. ```APIDOC ## Struct PyTimeUnit ### Summary ```rust pub struct PyTimeUnit(/* private fields */); ``` ### Trait Implementations #### `impl Clone for PyTimeUnit` * `fn clone(&self) -> PyTimeUnit`: Returns a duplicate of the value. * `fn clone_from(&mut self, source: &Self)`: Performs copy-assignment from `source`. #### `impl<'py> FromPyObject<'py> for PyTimeUnit` * `fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult`: Extracts `Self` from the bound smart pointer `obj`. #### `impl<'py> IntoPyObject<'py> for PyTimeUnit` * `type Target = PyString`: The Python output type. * `type Output = Bound<'py, >::Target>`: The smart pointer type to use. * `type Error = Infallible`: The type returned in the event of a conversion error. * `fn into_pyobject(self, py: Python<'py>) -> Result`: Performs the conversion. #### `impl Copy for PyTimeUnit` ### Auto Trait Implementations * `impl Freeze for PyTimeUnit` * `impl RefUnwindSafe for PyTimeUnit` * `impl Send for PyTimeUnit` * `impl Sync for PyTimeUnit` * `impl Unpin for PyTimeUnit` * `impl UnwindSafe for PyTimeUnit` ### Blanket Implementations * `impl Any for T where T: 'static + ?Sized` * `impl Borrow for T where T: ?Sized` * `impl BorrowMut for T where T: ?Sized` * `impl CloneToUninit for T where T: Clone` * `impl DynClone for T where T: Clone` * `impl From for T` * `impl<'py, T> FromPyObjectBound<'_, 'py> for T where T: FromPyObject<'py>` * `impl Into for T where U: From` * `impl IntoEither for T` * `impl<'py, T> IntoPyObjectExt<'py> for T where T: IntoPyObject<'py>` * `impl Key for T where T: Clone` * `impl Pointable for T` ``` -------------------------------- ### PolarsAllocator Usage Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/struct Demonstrates how to set PolarsAllocator as the global memory allocator in a Rust project. ```APIDOC ## PolarsAllocator ### Description A memory allocator that relays allocations to the allocator used by Polars. It can be set as the global memory allocator. ### Usage ```rust use pyo3_polars::PolarsAllocator; #[global_allocator] static ALLOC: PolarsAllocator = PolarsAllocator::new(); ``` **Note**: If the allocator capsule (`polars.polars._allocator`) is not available, this allocator falls back to `std::alloc::System`. ``` -------------------------------- ### Key and Pointable Trait Implementations Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Shows implementations for Key and Pointable traits, which are likely used for low-level memory management, data serialization, or inter-process communication within the pyo3-polars ecosystem. ```rust impl Key for T impl Pointable for T ``` -------------------------------- ### Python: Call Rust function with Polars DataFrame Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/index This Python script demonstrates how to import and use the `my_cool_function` defined in the Rust `pyo3-polars` crate. It creates a sample Polars DataFrame, passes it to the Rust function, and receives the processed DataFrame back. This showcases the integration facilitated by `maturin`. ```python from expression_lib import my_cool_function df = pl.DataFrame({ "foo": [1, 2, None], "bar": ["a", None, "c"], }) out_df = my_cool_function(df) ``` -------------------------------- ### StringCacheMismatchError Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/struct Details for the StringCacheMismatchError struct, including how to create a new instance and its associated type information. ```APIDOC ## Struct StringCacheMismatchError ### Description Represents an error indicating a mismatch in string caching behavior, likely within the context of pyo3-polars interactions. ### Fields (Private fields - not exposed for direct manipulation) ### Methods #### `new_err(args: A) -> PyErr` - **Description**: Creates a new `PyErr` of type `StringCacheMismatchError`. - **Type Parameters**: `A` where `A: PyErrArguments + Send + Sync + 'static` - **Return Type**: `PyErr` ### Trait Implementations #### `PyTypeInfo` - `NAME`: "StringCacheMismatchError" - `MODULE`: (Optional) Module name, if any. - `type_object_raw(py: Python<'_>) -> *mut PyTypeObject`: Returns the raw `PyTypeObject` instance. - `type_object(py: Python<'_>) -> Bound<'_, PyType>`: Returns a safe abstraction over the type object. - `is_type_of(object: &Bound<'_, PyAny>) -> bool`: Checks if an object is an instance of this type or a subclass. - `is_exact_type_of(object: &Bound<'_, PyAny>) -> bool`: Checks if an object is an exact instance of this type. #### `DerefToPyAny` - Implements dereferencing to a `PyAny` type. #### `ToPyErr` - Provides functionality to convert this error type into a `PyErr`. ``` -------------------------------- ### PolarsAllocator GlobalAlloc Interface (Rust) Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/struct Shows the core memory allocation functions provided by the GlobalAlloc trait for PolarsAllocator. These include methods for allocating, deallocating, allocating zeroed memory, and reallocating memory blocks according to specified layouts. ```rust impl GlobalAlloc for PolarsAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8; unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout); unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8; unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8; } ``` -------------------------------- ### Clone Implementation for PyTimeUnit Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Demonstrates the implementation of the Clone trait for PyTimeUnit. This allows creating duplicate instances of PyTimeUnit, essential for data manipulation and passing values between systems. ```rust impl Clone for PyTimeUnit fn clone(&self) -> PyTimeUnit fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### PyObject Conversion and Extension Traits Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Details trait implementations related to Python object conversion and extensions, such as FromPyObjectBound, IntoPyObjectExt, allowing for more flexible interaction with Python objects. ```rust impl<'py, T> FromPyObjectBound<'_, 'py> for T impl<'py, T> IntoPyObjectExt<'py> for T ``` -------------------------------- ### Use PolarsAllocator as Global Allocator (Rust) Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/struct Demonstrates how to set PolarsAllocator as the global memory allocator in a Rust program. This allows Polars to manage memory allocations globally. If the Polars allocator capsule is unavailable, it falls back to the system allocator. ```rust use pyo3_polars::PolarsAllocator; #[global_allocator] static ALLOC: PolarsAllocator = PolarsAllocator::new(); ``` -------------------------------- ### Basic Trait Implementations for PyTimeUnit Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Highlights the core Rust trait implementations for PyTimeUnit, including Copy, which allows for bitwise copies of the struct. ```rust impl Copy for PyTimeUnit ``` -------------------------------- ### Blanket Trait Implementations for Generic Types Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Illustrates blanket implementations that apply to generic types like T, including Any, Borrow, BorrowMut, CloneToUninit, DynClone, From, Into, and IntoEither. These provide common functionality across various types. ```rust impl Any for T impl Into for T impl From for T impl Borrow for T impl BorrowMut for T impl CloneToUninit for T impl DynClone for T impl IntoEither for T ``` -------------------------------- ### Implement VZip for T Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct This implementation enables zipping multiple lanes of data together. It requires the type `V` to implement the `MultiLane` trait for type `T`. ```rust impl VZip for T where V: MultiLane, { fn vzip(self) -> V { // Implementation details for zipping lanes. } } ``` -------------------------------- ### Python Conversion for PyTimeUnit Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Shows how PyTimeUnit can be converted to and from Python objects using pyo3. This includes extracting PyTimeUnit from Python objects and converting it into Python types like PyString. ```rust impl<'py> FromPyObject<'py> for PyTimeUnit fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult impl<'py> IntoPyObject<'py> for PyTimeUnit type Target = PyString type Output = Bound<'py, >::Target> type Error = Infallible fn into_pyobject(self, py: Python<'py>) -> Result ``` -------------------------------- ### Implement TryInto for T Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Provides the `try_into` method for converting a type `T` into another type `U` using `TryFrom`. This is useful for fallible conversions where an error type is defined. ```rust impl TryInto for T where U: TryFrom, { type Error = >::Error; fn try_into(self) -> Result>::Error> { U::try_from(self) } } ``` -------------------------------- ### Implement From for PyPolarsErr in Rust Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/enum Implements the From trait for PyPolarsErr. This allows direct conversion of PolarsError instances into PyPolarsErr, simplifying error handling. ```rust impl From for PyPolarsErr { fn from(source: PolarsError) -> Self { PyPolarsErr::Polars(source) } } ``` -------------------------------- ### Auto-Trait Implementations for PyTimeUnit Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Lists the auto-trait implementations for PyTimeUnit, indicating its thread-safety and memory management characteristics, such as Freeze, Send, Sync, Unpin, RefUnwindSafe, and UnwindSafe. ```rust impl Freeze for PyTimeUnit impl RefUnwindSafe for PyTimeUnit impl Send for PyTimeUnit impl Sync for PyTimeUnit impl Unpin for PyTimeUnit impl UnwindSafe for PyTimeUnit ``` -------------------------------- ### Implement From for PyErr in Rust Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/enum Implements the From trait for PyErr. This is crucial for converting custom Rust errors (PyPolarsErr) into Python exceptions, enabling them to be caught in Python code. ```rust impl From for PyErr { fn from(err: PyPolarsErr) -> PyErr { match err { PyPolarsErr::Polars(e) => e.into(), PyPolarsErr::Other(s) => PyErr::new::(s), } } } ``` -------------------------------- ### Implement TryFrom for T Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Enables fallible conversion from type `U` into type `T`. This is typically used when a conversion might fail and needs to return a specific error type. ```rust impl TryFrom for T where U: Into, { type Error = Infallible; fn try_from(value: U) -> Result>::Error> { Ok(value.into()) } } ``` -------------------------------- ### Rust: Define Python-callable function with PyDataFrame Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/index This Rust function, `my_cool_function`, takes a Python DataFrame (`PyDataFrame`), converts it into a native Polars DataFrame, performs some operations (currently a placeholder `todo!()`), and then wraps the resulting DataFrame back into a `PyDataFrame` for automatic conversion to a Python Polars DataFrame. It demonstrates the use of `pyfunction` and `PyDataFrame` for Rust-Python interoperability. ```rust #[pyfunction] fn my_cool_function(pydf: PyDataFrame) -> PyResult { let df: DataFrame = pydf.into(); let df = { // some work on the dataframe here todo!() }; // wrap the dataframe and it will be automatically converted to a python polars dataframe Ok(PyDataFrame(df)) } /// A Python module implemented in Rust. #[pymodule] fn expression_lib(_py: Python, m: &Bound) -> PyResult<()> { m.add_function(wrap_pyfunction!(my_cool_function, m)?) Ok(()) } ``` -------------------------------- ### PyTimeUnit Struct Definition Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Defines the PyTimeUnit struct in Rust. This struct is used for representing time units in the pyo3-polars library, facilitating interoperability between Rust and Python. ```rust pub struct PyTimeUnit(/* private fields */); ``` -------------------------------- ### Implement ToOwned for T Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct Allows creating an owned version of a type from a borrowed reference, typically by cloning. This is crucial for managing data ownership and lifetimes. ```rust impl ToOwned for T where T: Clone, { type Owned = T; fn to_owned(&self) -> T { self.clone() } fn clone_into(&self, target: &mut T) { *target = self.clone() } } ``` -------------------------------- ### StringCacheMismatchError PyTypeInfo in Rust Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/struct Shows the implementation of the PyTypeInfo trait for StringCacheMismatchError. This allows the struct to be recognized and used within the Python C API through pyo3. It defines the class name and provides methods to access the type object and check type compatibility. ```rust impl PyTypeInfo for StringCacheMismatchError { const NAME: &'static str = "StringCacheMismatchError"; const MODULE: Option<&'static str>; fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject; fn type_object(py: Python<'_>) -> Bound<'_, PyType>; fn is_type_of(object: &Bound<'_, PyAny>) -> bool; fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool; } ``` -------------------------------- ### Create StringCacheMismatchError in Rust Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/struct Demonstrates how to create a new PyErr of type StringCacheMismatchError. This function is generic over arguments that can be converted into PyErr. Ensure that the arguments provided meet the PyErrArguments trait bounds. ```rust pub struct StringCacheMismatchError(/* private fields */); impl StringCacheMismatchError { pub fn new_err(args: A) -> PyErr where A: PyErrArguments + Send + Sync + 'static, { // ... implementation details ... } } ``` -------------------------------- ### Implement Ungil for T Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct The `Ungil` trait is used to release the global interpreter lock (GIL) for Rust code that needs to perform operations that can run in parallel without Python thread synchronization. It requires the type `T` to be `Send`. ```rust impl Ungil for T where T: Send, { // Method implementations would go here, likely involving GIL management. } ``` -------------------------------- ### Error Handling with PyPolarsErr Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/enum The PyPolarsErr enum is used to represent errors that can occur within the pyo3-polars library. It can wrap PolarsError or a custom string message. ```APIDOC ## PyPolarsErr Enum ### Description Represents errors specific to the pyo3-polars library, allowing for both underlying Polars errors and custom error messages. ### Enum Definition ```rust pub enum PyPolarsErr { Polars(PolarsError), Other(String), } ``` ### Variants * **Polars(PolarsError)**: Wraps an error originating from the Polars library. * **Other(String)**: Represents a custom error message. ### Trait Implementations * **Debug**: Allows the error to be printed for debugging purposes. * **Display**: Provides a user-friendly string representation of the error. * **Error**: Implements the standard Rust `Error` trait, providing methods like `source()`. * `source()`: Returns the underlying error if available. * `description()`: (Deprecated) Returns a string description of the error. * `cause()`: (Deprecated) Returns the cause of the error. * `provide()`: (Nightly-only experimental) Provides type-based access to error context. * **From**: Enables conversion from `PolarsError` into `PyPolarsErr`. * **From**: Enables conversion from `PyPolarsErr` into `PyErr` (for Python exceptions). ### Auto Trait Implementations Includes standard Rust traits like `Send`, `Sync`, `Unpin`, and !`Freeze`, !`RefUnwindSafe`, !`UnwindSafe`. ### Blanket Implementations Provides common blanket implementations for traits like `Any`, `Borrow`, `From`, `Into`, `IntoEither`, `Pointable`, `ToCompactString`, `ToString`, `TryFrom`, `TryInto`, `VZip`, and `Ungil`. ``` -------------------------------- ### Define PyPolarsErr Enum in Rust Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/error/enum Defines the PyPolarsErr enum, a custom error type for pyo3-polars. It wraps PolarsError and a generic String for other errors, facilitating error propagation between Rust and Python. ```rust pub enum PyPolarsErr { Polars(PolarsError), Other(String), } ``` -------------------------------- ### Implement PyErrArguments for T Source: https://docs.rs/pyo3-polars/latest/pyo3_polars/types/struct This trait is likely associated with `pyo3` and is used to provide arguments for Python exceptions. It requires the type `T` to be convertible into a Python object and be sendable across threads. ```rust impl PyErrArguments for T where T: for<'py> IntoPyObject<'py> + Send + Sync, { // Method implementations would go here, likely involving Python interaction. } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.