Skip to main content

data_to_pycapsule

Function data_to_pycapsule 

Source
pub fn data_to_pycapsule(py: Python<'_>, data: Data) -> Py<PyAny>
Expand description

Creates a Python PyCapsule object containing a Rust Data instance.

This function takes ownership of the Data instance and encapsulates it within a PyCapsule object, allowing the Rust data to be passed into the Python runtime.

§Capsule type contract

When conversion to DataFFI fails (e.g. for Data::Custom), this returns a capsule containing a single Data value (no destructor). That capsule must never be passed to drop_cvec_pycapsule, which expects a CVec and would cause undefined behavior. Only capsules produced by code that creates CVec (e.g. for capsule_to_list) may be passed to drop_cvec_pycapsule.

§Panics

This function panics if the PyCapsule creation fails, which may occur if there are issues with memory allocation or if the Data instance cannot be properly encapsulated.