1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#![macro_use] #[macro_export] macro_rules! py_object_protocol { ($struct_:ty) => { #[pyproto] impl PyObjectProtocol for $struct_ { #[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_wrap)] fn __hash__(&self) -> PyResult<isize> { let mut hasher = std::collections::hash_map::DefaultHasher::new(); self.hash(&mut hasher); Ok(hasher.finish() as isize) } fn __richcmp__( &self, other: &PyAny, op: pyo3::class::basic::CompareOp, ) -> pyo3::PyResult<bool> { let other = other.downcast::<PyCell<$struct_>>(); if other.is_err() { return match op { pyo3::class::basic::CompareOp::Eq => Ok(false), pyo3::class::basic::CompareOp::Ne => Ok(true), _ => Err(PyErr::new::<exceptions::TypeError, _>( "Invalid comparison operator!".to_string(), )), }; } let other = other?.borrow(); match op { pyo3::class::basic::CompareOp::Eq => Ok(self.value == other.value), pyo3::class::basic::CompareOp::Ne => Ok(self.value != other.value), _ => Err(PyErr::new::<exceptions::TypeError, _>( "Invalid comparison operator!".to_string(), )), } } fn __repr__(&self) -> pyo3::PyResult<String> { Ok(format!("{}", self)) } } }; }