From a81769c3fdd2890cb4f7d7fcd0e1b6988a959f4c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 26 Nov 2021 00:10:19 +0100 Subject: [PATCH] identifiers: Clean up internal macros --- crates/ruma-identifiers/src/macros.rs | 67 +++++++++++++-------------- 1 file changed, 31 insertions(+), 36 deletions(-) diff --git a/crates/ruma-identifiers/src/macros.rs b/crates/ruma-identifiers/src/macros.rs index c6ed7e1b..19ff2c4d 100644 --- a/crates/ruma-identifiers/src/macros.rs +++ b/crates/ruma-identifiers/src/macros.rs @@ -24,32 +24,6 @@ macro_rules! partial_eq_string { } } -macro_rules! as_str_based_impls { - ($id:ty) => { - impl AsRef for $id { - fn as_ref(&self) -> &str { - self.as_str() - } - } - - impl std::fmt::Display for $id { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_str()) - } - } - - #[cfg(feature = "serde")] - impl serde::Serialize for $id { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(self.as_str()) - } - } - }; -} - macro_rules! opaque_identifier_common_impls { ($id:ty) => { impl $id { @@ -80,12 +54,6 @@ macro_rules! opaque_identifier_common_impls { } } - impl std::fmt::Debug for $id { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ::fmt(self.as_str(), f) - } - } - impl Clone for Box<$id> { fn clone(&self) -> Self { (**self).to_owned() @@ -100,9 +68,9 @@ macro_rules! opaque_identifier_common_impls { } } - impl From<&$id> for Box<$id> { - fn from(id: &$id) -> Self { - id.to_owned() + impl AsRef for $id { + fn as_ref(&self) -> &str { + self.as_str() } } @@ -112,6 +80,12 @@ macro_rules! opaque_identifier_common_impls { } } + impl From<&$id> for Box<$id> { + fn from(id: &$id) -> Self { + id.to_owned() + } + } + impl From<&$id> for std::rc::Rc<$id> { fn from(s: &$id) -> std::rc::Rc<$id> { let rc = std::rc::Rc::::from(s.as_str()); @@ -150,7 +124,28 @@ macro_rules! opaque_identifier_common_impls { } } - as_str_based_impls!($id); + impl std::fmt::Debug for $id { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + ::fmt(self.as_str(), f) + } + } + + impl std::fmt::Display for $id { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.as_str()) + } + } + + #[cfg(feature = "serde")] + impl serde::Serialize for $id { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.as_str()) + } + } + partial_eq_string!($id); partial_eq_string!(Box<$id>); };