Add doc for #[pyclass_repr] (#8110)

This commit is contained in:
xzfc
2026-02-12 14:48:15 +00:00
committed by GitHub
parent 3c5497e0d3
commit bd9c87bbbe
3 changed files with 42 additions and 0 deletions

View File

@@ -1,5 +1,45 @@
mod pyclass_repr;
/// `#[pyclass_repr]` - implements `trait Repr`.
///
/// Only methods with `#[getter]` are included into the output.
///
/// # Usage example
///
/// Input:
/// ```ignore
/// #[pyclass_repr]
/// #[pymethods]
/// impl PyMyStruct {
/// #[getter]
/// pub fn foo(&self) -> &str {
/// &self.0.foo
/// }
///
/// #[getter]
/// pub fn bar(&self) -> f32 {
/// self.0.bar
/// }
///
/// pub fn __repr__(&self) -> String {
/// self.repr()
/// }
/// }
/// ```
///
/// Generated output is roughly equivalent to:
/// ```ignore
/// impl crate::repr::Repr for PyMyStruct {
/// fn fmt(&self, f: &mut crate::repr::Formatter<'_>) -> std::fmt::Result {
/// use crate::repr::WriteExt as _;
///
/// f.class::<Self>(&[
/// ("foo", &self.foo()),
/// ("bar", &self.bar()),
/// ])
/// }
/// }
/// ```
#[proc_macro_attribute]
pub fn pyclass_repr(
_attributes: proc_macro::TokenStream,