diff --git a/ruma-client-api/src/r0/to_device.rs b/ruma-client-api/src/r0/to_device.rs index b3412f58..1e60ea72 100644 --- a/ruma-client-api/src/r0/to_device.rs +++ b/ruma-client-api/src/r0/to_device.rs @@ -51,7 +51,7 @@ impl Serialize for DeviceIdOrAllDevices { S: Serializer, { match self { - Self::DeviceId(ref device_id) => serializer.serialize_str(&device_id), + Self::DeviceId(device_id) => device_id.serialize(serializer), Self::AllDevices => serializer.serialize_str("*"), } } diff --git a/ruma-events/src/key/verification/start.rs b/ruma-events/src/key/verification/start.rs index 2388ae40..b29b236a 100644 --- a/ruma-events/src/key/verification/start.rs +++ b/ruma-events/src/key/verification/start.rs @@ -288,7 +288,7 @@ mod tests { key_agreement_protocols, message_authentication_codes, short_authentication_string, - }) if from_device.as_str() == "123" + }) if from_device == "123" && transaction_id == "456" && hashes == vec![HashAlgorithm::Sha256] && key_agreement_protocols == vec![KeyAgreementProtocol::Curve25519] @@ -323,7 +323,7 @@ mod tests { message_authentication_codes, short_authentication_string, }) - } if from_device.as_str() == "123" + } if from_device == "123" && transaction_id == "456" && hashes == vec![HashAlgorithm::Sha256] && key_agreement_protocols == vec![KeyAgreementProtocol::Curve25519] diff --git a/ruma-events/src/room/encrypted.rs b/ruma-events/src/room/encrypted.rs index 2e986c66..5020db53 100644 --- a/ruma-events/src/room/encrypted.rs +++ b/ruma-events/src/room/encrypted.rs @@ -161,7 +161,7 @@ mod tests { session_id, }) if ciphertext == "ciphertext" && sender_key == "sender_key" - && device_id.as_str() == "device_id" + && device_id == "device_id" && session_id == "session_id" ); } diff --git a/ruma-events/src/room/member.rs b/ruma-events/src/room/member.rs index d9c868a1..852bda37 100644 --- a/ruma-events/src/room/member.rs +++ b/ruma-events/src/room/member.rs @@ -187,7 +187,7 @@ fn membership_change( (Invite, Invite) | (Leave, Leave) | (Ban, Ban) => MembershipChange::None, (Invite, Join) | (Leave, Join) => MembershipChange::Joined, (Invite, Leave) => { - if sender.as_ref() == state_key { + if sender == state_key { MembershipChange::InvitationRevoked } else { MembershipChange::InvitationRejected @@ -200,7 +200,7 @@ fn membership_change( avatar_url_changed: prev_content.avatar_url != content.avatar_url, }, (Join, Leave) => { - if sender.as_ref() == state_key { + if sender == state_key { MembershipChange::Left } else { MembershipChange::Kicked diff --git a/ruma-identifiers/src/device_id.rs b/ruma-identifiers/src/device_id.rs index 32a570d9..217c4311 100644 --- a/ruma-identifiers/src/device_id.rs +++ b/ruma-identifiers/src/device_id.rs @@ -110,29 +110,8 @@ impl<'de> serde::Deserialize<'de> for Box { } } -impl PartialEq for DeviceId { - fn eq(&self, other: &str) -> bool { - self.as_str() == other - } -} - -impl PartialEq for str { - fn eq(&self, other: &DeviceId) -> bool { - self == other.as_str() - } -} - -impl PartialEq for DeviceId { - fn eq(&self, other: &String) -> bool { - self.as_str() == other.as_str() - } -} - -impl PartialEq for String { - fn eq(&self, other: &DeviceId) -> bool { - self.as_str() == other.as_str() - } -} +partial_eq_string!(DeviceId); +partial_eq_string!(Box); /// Generates a random `DeviceId`, suitable for assignment to a new device. #[cfg(feature = "rand")] diff --git a/ruma-identifiers/src/macros.rs b/ruma-identifiers/src/macros.rs index 60f04d21..08c79a7d 100644 --- a/ruma-identifiers/src/macros.rs +++ b/ruma-identifiers/src/macros.rs @@ -5,6 +5,25 @@ macro_rules! doc_concat { ( $( #[doc = $doc:expr] $( $thing:tt )* )* ) => ( $( #[doc = $doc] $( $thing )* )* ); } +macro_rules! partial_eq_string { + ($id:ty) => { + partial_eq_string!(@imp, $id, str); + partial_eq_string!(@imp, $id, &str); + partial_eq_string!(@imp, $id, String); + partial_eq_string!(@imp, str, $id); + partial_eq_string!(@imp, &str, $id); + partial_eq_string!(@imp, String, $id); + }; + (@imp, $l:ty, $r:ty) => { + impl ::std::cmp::PartialEq<$r> for $l { + fn eq(&self, other: &$r) -> bool { + ::std::convert::AsRef::::as_ref(self) + == ::std::convert::AsRef::::as_ref(other) + } + } + } +} + macro_rules! common_impls { ($id:ty, $try_from:ident, $desc:literal) => { impl $id { @@ -96,28 +115,6 @@ macro_rules! common_impls { } } - impl ::std::cmp::PartialEq<&str> for $id { - fn eq(&self, other: &&str) -> bool { - self.as_str() == *other - } - } - - impl ::std::cmp::PartialEq<$id> for &str { - fn eq(&self, other: &$id) -> bool { - *self == other.as_str() - } - } - - impl ::std::cmp::PartialEq<::std::string::String> for $id { - fn eq(&self, other: &::std::string::String) -> bool { - self.as_str() == other.as_str() - } - } - - impl ::std::cmp::PartialEq<$id> for ::std::string::String { - fn eq(&self, other: &$id) -> bool { - self.as_str() == other.as_str() - } - } + partial_eq_string!($id); }; } diff --git a/ruma-identifiers/src/room_version_id.rs b/ruma-identifiers/src/room_version_id.rs index 8ea7b5d6..c02ab85b 100644 --- a/ruma-identifiers/src/room_version_id.rs +++ b/ruma-identifiers/src/room_version_id.rs @@ -87,6 +87,19 @@ impl RoomVersionId { Self::Version6 } + /// Creates a string slice from this `RoomVersionId` + pub fn as_str(&self) -> &str { + match &self { + Self::Version1 => "1", + Self::Version2 => "2", + Self::Version3 => "3", + Self::Version4 => "4", + Self::Version5 => "5", + Self::Version6 => "6", + Self::Custom(version) => version.as_str(), + } + } + /// Whether or not this room version is an official one specified by the Matrix protocol. pub fn is_official(&self) -> bool { !self.is_custom() @@ -150,15 +163,7 @@ impl From for String { impl AsRef for RoomVersionId { fn as_ref(&self) -> &str { - match &self { - Self::Version1 => "1", - Self::Version2 => "2", - Self::Version3 => "3", - Self::Version4 => "4", - Self::Version5 => "5", - Self::Version6 => "6", - Self::Custom(version) => version.as_ref(), - } + self.as_str() } } @@ -253,31 +258,38 @@ impl TryFrom for RoomVersionId { impl PartialEq<&str> for RoomVersionId { fn eq(&self, other: &&str) -> bool { - self.as_ref() == *other + self.as_str() == *other } } impl PartialEq for &str { fn eq(&self, other: &RoomVersionId) -> bool { - *self == other.as_ref() + *self == other.as_str() } } impl PartialEq for RoomVersionId { fn eq(&self, other: &String) -> bool { - self.as_ref() == other + self.as_str() == other } } impl PartialEq for String { fn eq(&self, other: &RoomVersionId) -> bool { - self == other.as_ref() + self == other.as_str() } } #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct CustomRoomVersion(Box); +impl CustomRoomVersion { + /// Creates a string slice from this `CustomRoomVersion` + pub fn as_str(&self) -> &str { + &self.0 + } +} + impl From for String { fn from(v: CustomRoomVersion) -> Self { v.0.into() @@ -286,7 +298,7 @@ impl From for String { impl AsRef for CustomRoomVersion { fn as_ref(&self) -> &str { - &self.0 + self.as_str() } } diff --git a/ruma-identifiers/src/server_name.rs b/ruma-identifiers/src/server_name.rs index c83dd759..a3783ae5 100644 --- a/ruma-identifiers/src/server_name.rs +++ b/ruma-identifiers/src/server_name.rs @@ -160,29 +160,8 @@ impl<'de> serde::Deserialize<'de> for Box { } } -impl PartialEq for ServerName { - fn eq(&self, other: &str) -> bool { - self.as_str() == other - } -} - -impl PartialEq for str { - fn eq(&self, other: &ServerName) -> bool { - self == other.as_str() - } -} - -impl PartialEq for ServerName { - fn eq(&self, other: &String) -> bool { - self.as_str() == other.as_str() - } -} - -impl PartialEq for String { - fn eq(&self, other: &ServerName) -> bool { - self.as_str() == other.as_str() - } -} +partial_eq_string!(ServerName); +partial_eq_string!(Box); #[cfg(test)] mod tests {