qcs_api_client_common/py/
mod.rs

1/// Implements __repr__ on a [`pyo3::pyclass`] by using it's [`std::fmt::Debug`]
2/// implementation.
3///
4/// The pyclass must implement [`std::fmt::Debug`].
5#[macro_export]
6macro_rules! impl_repr {
7    ($name: ident) => {
8        #[::pyo3::pymethods]
9        impl $name {
10            #[must_use]
11            fn __repr__(&self) -> String {
12                format!("{:?}", self)
13            }
14        }
15    };
16}
17
18/// Provides support for equality checks of a [`pyo3::pyclass`] by implementing `__richcmp__`.
19///
20/// The pyclass must implement [`PartialEq`].
21#[macro_export]
22macro_rules! impl_eq {
23    ($name: ident) => {
24        #[::pyo3::pymethods]
25        impl $name {
26            fn __richcmp__(
27                &self,
28                py: ::pyo3::Python<'_>,
29                other: &Self,
30                op: ::pyo3::pyclass::CompareOp,
31            ) -> ::pyo3::PyObject {
32                use ::pyo3::IntoPy;
33                match op {
34                    ::pyo3::pyclass::CompareOp::Eq => (self == other).into_py(py),
35                    ::pyo3::pyclass::CompareOp::Ne => (self != other).into_py(py),
36                    _ => py.NotImplemented(),
37                }
38            }
39        }
40    };
41}