Improve string comparison impls

This commit is contained in:
Jonas Platte 2020-07-22 19:17:24 +02:00
parent b24df92692
commit 4cbdc079b0
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
8 changed files with 56 additions and 89 deletions

View File

@ -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("*"),
}
}

View File

@ -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]

View File

@ -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"
);
}

View File

@ -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

View File

@ -110,29 +110,8 @@ impl<'de> serde::Deserialize<'de> for Box<DeviceId> {
}
}
impl PartialEq<str> for DeviceId {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<DeviceId> for str {
fn eq(&self, other: &DeviceId) -> bool {
self == other.as_str()
}
}
impl PartialEq<String> for DeviceId {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialEq<DeviceId> for String {
fn eq(&self, other: &DeviceId) -> bool {
self.as_str() == other.as_str()
}
}
partial_eq_string!(DeviceId);
partial_eq_string!(Box<DeviceId>);
/// Generates a random `DeviceId`, suitable for assignment to a new device.
#[cfg(feature = "rand")]

View File

@ -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::<str>::as_ref(self)
== ::std::convert::AsRef::<str>::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);
};
}

View File

@ -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<RoomVersionId> for String {
impl AsRef<str> 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<String> for RoomVersionId {
impl PartialEq<&str> for RoomVersionId {
fn eq(&self, other: &&str) -> bool {
self.as_ref() == *other
self.as_str() == *other
}
}
impl PartialEq<RoomVersionId> for &str {
fn eq(&self, other: &RoomVersionId) -> bool {
*self == other.as_ref()
*self == other.as_str()
}
}
impl PartialEq<String> for RoomVersionId {
fn eq(&self, other: &String) -> bool {
self.as_ref() == other
self.as_str() == other
}
}
impl PartialEq<RoomVersionId> 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<str>);
impl CustomRoomVersion {
/// Creates a string slice from this `CustomRoomVersion`
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<CustomRoomVersion> for String {
fn from(v: CustomRoomVersion) -> Self {
v.0.into()
@ -286,7 +298,7 @@ impl From<CustomRoomVersion> for String {
impl AsRef<str> for CustomRoomVersion {
fn as_ref(&self) -> &str {
&self.0
self.as_str()
}
}

View File

@ -160,29 +160,8 @@ impl<'de> serde::Deserialize<'de> for Box<ServerName> {
}
}
impl PartialEq<str> for ServerName {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<ServerName> for str {
fn eq(&self, other: &ServerName) -> bool {
self == other.as_str()
}
}
impl PartialEq<String> for ServerName {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialEq<ServerName> for String {
fn eq(&self, other: &ServerName) -> bool {
self.as_str() == other.as_str()
}
}
partial_eq_string!(ServerName);
partial_eq_string!(Box<ServerName>);
#[cfg(test)]
mod tests {