Improve string comparison impls
This commit is contained in:
parent
b24df92692
commit
4cbdc079b0
@ -51,7 +51,7 @@ impl Serialize for DeviceIdOrAllDevices {
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
match self {
|
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("*"),
|
Self::AllDevices => serializer.serialize_str("*"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,7 +288,7 @@ mod tests {
|
|||||||
key_agreement_protocols,
|
key_agreement_protocols,
|
||||||
message_authentication_codes,
|
message_authentication_codes,
|
||||||
short_authentication_string,
|
short_authentication_string,
|
||||||
}) if from_device.as_str() == "123"
|
}) if from_device == "123"
|
||||||
&& transaction_id == "456"
|
&& transaction_id == "456"
|
||||||
&& hashes == vec![HashAlgorithm::Sha256]
|
&& hashes == vec![HashAlgorithm::Sha256]
|
||||||
&& key_agreement_protocols == vec![KeyAgreementProtocol::Curve25519]
|
&& key_agreement_protocols == vec![KeyAgreementProtocol::Curve25519]
|
||||||
@ -323,7 +323,7 @@ mod tests {
|
|||||||
message_authentication_codes,
|
message_authentication_codes,
|
||||||
short_authentication_string,
|
short_authentication_string,
|
||||||
})
|
})
|
||||||
} if from_device.as_str() == "123"
|
} if from_device == "123"
|
||||||
&& transaction_id == "456"
|
&& transaction_id == "456"
|
||||||
&& hashes == vec![HashAlgorithm::Sha256]
|
&& hashes == vec![HashAlgorithm::Sha256]
|
||||||
&& key_agreement_protocols == vec![KeyAgreementProtocol::Curve25519]
|
&& key_agreement_protocols == vec![KeyAgreementProtocol::Curve25519]
|
||||||
|
@ -161,7 +161,7 @@ mod tests {
|
|||||||
session_id,
|
session_id,
|
||||||
}) if ciphertext == "ciphertext"
|
}) if ciphertext == "ciphertext"
|
||||||
&& sender_key == "sender_key"
|
&& sender_key == "sender_key"
|
||||||
&& device_id.as_str() == "device_id"
|
&& device_id == "device_id"
|
||||||
&& session_id == "session_id"
|
&& session_id == "session_id"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -187,7 +187,7 @@ fn membership_change(
|
|||||||
(Invite, Invite) | (Leave, Leave) | (Ban, Ban) => MembershipChange::None,
|
(Invite, Invite) | (Leave, Leave) | (Ban, Ban) => MembershipChange::None,
|
||||||
(Invite, Join) | (Leave, Join) => MembershipChange::Joined,
|
(Invite, Join) | (Leave, Join) => MembershipChange::Joined,
|
||||||
(Invite, Leave) => {
|
(Invite, Leave) => {
|
||||||
if sender.as_ref() == state_key {
|
if sender == state_key {
|
||||||
MembershipChange::InvitationRevoked
|
MembershipChange::InvitationRevoked
|
||||||
} else {
|
} else {
|
||||||
MembershipChange::InvitationRejected
|
MembershipChange::InvitationRejected
|
||||||
@ -200,7 +200,7 @@ fn membership_change(
|
|||||||
avatar_url_changed: prev_content.avatar_url != content.avatar_url,
|
avatar_url_changed: prev_content.avatar_url != content.avatar_url,
|
||||||
},
|
},
|
||||||
(Join, Leave) => {
|
(Join, Leave) => {
|
||||||
if sender.as_ref() == state_key {
|
if sender == state_key {
|
||||||
MembershipChange::Left
|
MembershipChange::Left
|
||||||
} else {
|
} else {
|
||||||
MembershipChange::Kicked
|
MembershipChange::Kicked
|
||||||
|
@ -110,29 +110,8 @@ impl<'de> serde::Deserialize<'de> for Box<DeviceId> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<str> for DeviceId {
|
partial_eq_string!(DeviceId);
|
||||||
fn eq(&self, other: &str) -> bool {
|
partial_eq_string!(Box<DeviceId>);
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates a random `DeviceId`, suitable for assignment to a new device.
|
/// Generates a random `DeviceId`, suitable for assignment to a new device.
|
||||||
#[cfg(feature = "rand")]
|
#[cfg(feature = "rand")]
|
||||||
|
@ -5,6 +5,25 @@ macro_rules! doc_concat {
|
|||||||
( $( #[doc = $doc:expr] $( $thing:tt )* )* ) => ( $( #[doc = $doc] $( $thing )* )* );
|
( $( #[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 {
|
macro_rules! common_impls {
|
||||||
($id:ty, $try_from:ident, $desc:literal) => {
|
($id:ty, $try_from:ident, $desc:literal) => {
|
||||||
impl $id {
|
impl $id {
|
||||||
@ -96,28 +115,6 @@ macro_rules! common_impls {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ::std::cmp::PartialEq<&str> for $id {
|
partial_eq_string!($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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,19 @@ impl RoomVersionId {
|
|||||||
Self::Version6
|
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.
|
/// Whether or not this room version is an official one specified by the Matrix protocol.
|
||||||
pub fn is_official(&self) -> bool {
|
pub fn is_official(&self) -> bool {
|
||||||
!self.is_custom()
|
!self.is_custom()
|
||||||
@ -150,15 +163,7 @@ impl From<RoomVersionId> for String {
|
|||||||
|
|
||||||
impl AsRef<str> for RoomVersionId {
|
impl AsRef<str> for RoomVersionId {
|
||||||
fn as_ref(&self) -> &str {
|
fn as_ref(&self) -> &str {
|
||||||
match &self {
|
self.as_str()
|
||||||
Self::Version1 => "1",
|
|
||||||
Self::Version2 => "2",
|
|
||||||
Self::Version3 => "3",
|
|
||||||
Self::Version4 => "4",
|
|
||||||
Self::Version5 => "5",
|
|
||||||
Self::Version6 => "6",
|
|
||||||
Self::Custom(version) => version.as_ref(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,31 +258,38 @@ impl TryFrom<String> for RoomVersionId {
|
|||||||
|
|
||||||
impl PartialEq<&str> for RoomVersionId {
|
impl PartialEq<&str> for RoomVersionId {
|
||||||
fn eq(&self, other: &&str) -> bool {
|
fn eq(&self, other: &&str) -> bool {
|
||||||
self.as_ref() == *other
|
self.as_str() == *other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<RoomVersionId> for &str {
|
impl PartialEq<RoomVersionId> for &str {
|
||||||
fn eq(&self, other: &RoomVersionId) -> bool {
|
fn eq(&self, other: &RoomVersionId) -> bool {
|
||||||
*self == other.as_ref()
|
*self == other.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<String> for RoomVersionId {
|
impl PartialEq<String> for RoomVersionId {
|
||||||
fn eq(&self, other: &String) -> bool {
|
fn eq(&self, other: &String) -> bool {
|
||||||
self.as_ref() == other
|
self.as_str() == other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<RoomVersionId> for String {
|
impl PartialEq<RoomVersionId> for String {
|
||||||
fn eq(&self, other: &RoomVersionId) -> bool {
|
fn eq(&self, other: &RoomVersionId) -> bool {
|
||||||
self == other.as_ref()
|
self == other.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct CustomRoomVersion(Box<str>);
|
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 {
|
impl From<CustomRoomVersion> for String {
|
||||||
fn from(v: CustomRoomVersion) -> Self {
|
fn from(v: CustomRoomVersion) -> Self {
|
||||||
v.0.into()
|
v.0.into()
|
||||||
@ -286,7 +298,7 @@ impl From<CustomRoomVersion> for String {
|
|||||||
|
|
||||||
impl AsRef<str> for CustomRoomVersion {
|
impl AsRef<str> for CustomRoomVersion {
|
||||||
fn as_ref(&self) -> &str {
|
fn as_ref(&self) -> &str {
|
||||||
&self.0
|
self.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,29 +160,8 @@ impl<'de> serde::Deserialize<'de> for Box<ServerName> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq<str> for ServerName {
|
partial_eq_string!(ServerName);
|
||||||
fn eq(&self, other: &str) -> bool {
|
partial_eq_string!(Box<ServerName>);
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user