15 releases
| 0.4.3 | Apr 10, 2025 |
|---|---|
| 0.4.2 | Jul 20, 2024 |
| 0.3.1 | Jul 19, 2024 |
| 0.3.0 | Jun 25, 2024 |
| 0.1.3 | Jun 17, 2024 |
#1347 in Rust patterns
5,733 downloads per month
20KB
343 lines
Derive macros to make Rust objects more friendly 🤗.
Please see these docs for the PyDisplay and PyDebug
traits which power Str and Repr.
This crate automatically derives the following functions for structs and enums:
-
__str__ -
__repr__ -
__dir__ -
__getattr__ -
__dict__ -
Skip exposure of variants or fields per derive macro with the
#[skip(...)]attribute (eg.#[skip(Dir, Str)]) -
Struct fields which are not
pubare skipped automatically
When you have custom Rust structs which need to implement PyDisplay and PyDebug, you should use the AutoDisplay and AutoDebug traits.
This will have the same output as Str and Repr respectively.
This crate exports 2 traits (PyDisplay`` and PyDebug`) which should be implemented for every type for which its field or variant is not skipped.
It also exports a macro to use the Debug and Display traits to generate a PyDebug and PyDisplay
implementation.
In addition, the AutoDisplay and AutoDebug macros enable usage of arbitrary Rust, non-pyclass structs.
pyo3-special-method-derive
This crate enables you to automatically derive Python dunder methods for your Rust crate using PyO3.
Key features
- The following methods may be automatically derived on structs and enums:
__str____repr____dir____getattr____dict__
- Support for structs and enums (only unit and complex enums due to a PyO3 limitation)
- Support for skipping variants or fields per derive macro with the
#[skip(...)]attribute - Automatically skip struct fields which are not
pub
Example
#[pyclass]
#[derive(Dir, Str, Repr)]
struct Person {
pub name: String,
occupation: String,
#[pyo3_smd(skip)]
pub phone_num: String,
}
PyO3 feature note
To use pyo3-special-method-derive, you should enable the multiple-pymethods feature on PyO3:
pyo3 = { version = "0.22", features = ["multiple-pymethods"] }
Dependencies
~4MB
~84K SLoC