From 88c51ae772216720513a9eb7d9a3fe2ff17e18ca Mon Sep 17 00:00:00 2001 From: xzfc <5121426+xzfc@users.noreply.github.com> Date: Thu, 12 Feb 2026 14:48:15 +0000 Subject: [PATCH] Add doc for #[pyclass_repr] (#8110) --- lib/edge/python/codegen/src/lib.rs | 40 ++++++++++++++++++++++++++++++ lib/edge/python/src/repr.rs | 1 + lib/macros/src/lib.rs | 1 + 3 files changed, 42 insertions(+) diff --git a/lib/edge/python/codegen/src/lib.rs b/lib/edge/python/codegen/src/lib.rs index 4e54d5776c..2b2c22758d 100644 --- a/lib/edge/python/codegen/src/lib.rs +++ b/lib/edge/python/codegen/src/lib.rs @@ -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::(&[ +/// ("foo", &self.foo()), +/// ("bar", &self.bar()), +/// ]) +/// } +/// } +/// ``` #[proc_macro_attribute] pub fn pyclass_repr( _attributes: proc_macro::TokenStream, diff --git a/lib/edge/python/src/repr.rs b/lib/edge/python/src/repr.rs index 77ab4c450b..6ea018aeb2 100644 --- a/lib/edge/python/src/repr.rs +++ b/lib/edge/python/src/repr.rs @@ -4,6 +4,7 @@ use std::fmt; pub use edge_py_codegen::pyclass_repr; use pyo3::PyTypeInfo; +/// Can be implemented using [macro pyclass_repr]. pub trait Repr { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result; diff --git a/lib/macros/src/lib.rs b/lib/macros/src/lib.rs index d265dde4bd..d401e2745e 100644 --- a/lib/macros/src/lib.rs +++ b/lib/macros/src/lib.rs @@ -2,6 +2,7 @@ use proc_macro::TokenStream; mod anonymize; +/// Grep for `trait Anonymize` for doc. #[proc_macro_derive(Anonymize, attributes(anonymize))] pub fn derive_anonymize(input: TokenStream) -> TokenStream { match anonymize::derive_anonymize(input.into()) {