identifiers: Derive PartialEq, Eq, PartialOrd, Ord, Hash for str newtypes

… instead of generating them in our own macro.
This commit is contained in:
Jonas Platte 2021-11-26 00:03:24 +01:00
parent 5852658da5
commit f448f6756e
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
14 changed files with 13 additions and 26 deletions

View File

@ -5,6 +5,7 @@
/// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
/// must not exceed 255 characters.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ClientSecret(str);
opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate);

View File

@ -24,6 +24,7 @@ use crate::generate_localpart;
/// assert_eq!(owned_id.as_str(), "ijklmnop");
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(str);
opaque_identifier!(DeviceId);

View File

@ -4,6 +4,7 @@ use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId};
/// A key algorithm and a device id, combined with a ':'.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceKeyId(str);
opaque_identifier_validated!(DeviceKeyId, ruma_identifiers_validation::device_key_id::validate);

View File

@ -38,6 +38,7 @@ use crate::ServerName;
/// );
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EventId(str);
opaque_identifier_validated!(EventId, ruma_identifiers_validation::event_id::validate);

View File

@ -3,6 +3,7 @@
/// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is
/// provided simply for its semantic value.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct KeyName(str);
opaque_identifier!(KeyName);

View File

@ -38,32 +38,6 @@ macro_rules! as_str_based_impls {
}
}
impl PartialEq for $id {
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
}
impl Eq for $id {}
impl PartialOrd for $id {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
PartialOrd::partial_cmp(self.as_str(), other.as_str())
}
}
impl Ord for $id {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
Ord::cmp(self.as_str(), other.as_str())
}
}
impl std::hash::Hash for $id {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for $id {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>

View File

@ -15,6 +15,7 @@ type Result<T, E = MxcUriError> = std::result::Result<T, E>;
/// [MXC URI]: https://matrix.org/docs/spec/client_server/r0.6.1#mxc-uri
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MxcUri(str);
opaque_identifier!(MxcUri);

View File

@ -18,6 +18,7 @@ use crate::{server_name::ServerName, EventId, MatrixToRef};
/// );
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoomAliasId(str);
opaque_identifier_validated!(RoomAliasId, ruma_identifiers_validation::room_alias_id::validate);

View File

@ -18,6 +18,7 @@ use crate::{EventId, MatrixToRef, ServerName};
/// );
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoomId(str);
opaque_identifier_validated!(RoomId, ruma_identifiers_validation::room_id::validate);

View File

@ -27,6 +27,7 @@ use crate::{server_name::ServerName, RoomAliasId, RoomId};
/// );
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoomIdOrAliasId(str);
opaque_identifier_validated!(

View File

@ -4,6 +4,7 @@
///
/// It can't exceed 255 bytes or be empty.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoomName(str);
opaque_identifier_validated!(RoomName, ruma_identifiers_validation::room_name::validate);

View File

@ -2,6 +2,7 @@
/// A Matrix-spec compliant server name.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServerName(str);
opaque_identifier_validated!(ServerName, ruma_identifiers_validation::server_name::validate);

View File

@ -5,6 +5,7 @@
/// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
/// must not exceed 255 characters.
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SessionId(str);
opaque_identifier_validated!(SessionId, ruma_identifiers_validation::session_id::validate);

View File

@ -18,6 +18,7 @@ use crate::{MatrixToRef, ServerName};
/// );
/// ```
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UserId(str);
opaque_identifier_validated!(UserId, ruma_identifiers_validation::user_id::validate);