14 releases
new 0.2.2 | Mar 2, 2025 |
---|---|
0.2.1 | Feb 27, 2025 |
0.1.10 | Feb 3, 2025 |
#689 in Encoding
882 downloads per month
125KB
3K
SLoC
A Rust crate which defines a trait for serializing and deserializing Python objects. Because this trait does not have the 'py lifetime specifier, trait objects can be stored in Python classes, which is often useful for rust backends for Python projects. This allows for the different PyAnySerde implementations to be created and bound in the Python interpreter. There are a variety of implementations provided for standard Python objects, and there is a PythonSerde abstract Python class which can be implemented and turned into a Box for custom composition with other instances of Box (such as that for List) without needing to write any Rust code. Due to issues sharing code for Rust-defined python classes between crates, this crate must be used as a dependency and expose the Python type information itself. To help, a .pyi file exists in the root of this project which defines all the pyclasses which come from this crate.
The use case for this crate is demonstrated in rlgym-learn - the user writes Python code which instantiates an enum defining the type of PyAnySerde to be used to communicate across shared memory, and then this enum is used to construct the Box to be used by the Rust side of the python module. The enum can be wrapped to be made pickleable and this is the intended way of transferring serde information across processes. In Rust, these Box instances can be used via the append
and retrieve
methods. Some structs (like NumpyDynamicShapeSerde) implementing the PyAnySerde trait rely on alignment for deserialization, and so these append
and retrieve
methods must write directly to the buffer so that the alignment is known.
Dependencies
~7–32MB
~448K SLoC