identifiers: Derive PartialEq, Eq, PartialOrd, Ord, Hash for str newtypes
… instead of generating them in our own macro.
This commit is contained in:
parent
5852658da5
commit
f448f6756e
@ -5,6 +5,7 @@
|
|||||||
/// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
|
/// Client secrets in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
|
||||||
/// must not exceed 255 characters.
|
/// must not exceed 255 characters.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct ClientSecret(str);
|
pub struct ClientSecret(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate);
|
opaque_identifier_validated!(ClientSecret, ruma_identifiers_validation::client_secret::validate);
|
||||||
|
@ -24,6 +24,7 @@ use crate::generate_localpart;
|
|||||||
/// assert_eq!(owned_id.as_str(), "ijklmnop");
|
/// assert_eq!(owned_id.as_str(), "ijklmnop");
|
||||||
/// ```
|
/// ```
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct DeviceId(str);
|
pub struct DeviceId(str);
|
||||||
|
|
||||||
opaque_identifier!(DeviceId);
|
opaque_identifier!(DeviceId);
|
||||||
|
@ -4,6 +4,7 @@ use crate::{crypto_algorithms::DeviceKeyAlgorithm, DeviceId};
|
|||||||
|
|
||||||
/// A key algorithm and a device id, combined with a ':'.
|
/// A key algorithm and a device id, combined with a ':'.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct DeviceKeyId(str);
|
pub struct DeviceKeyId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(DeviceKeyId, ruma_identifiers_validation::device_key_id::validate);
|
opaque_identifier_validated!(DeviceKeyId, ruma_identifiers_validation::device_key_id::validate);
|
||||||
|
@ -38,6 +38,7 @@ use crate::ServerName;
|
|||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct EventId(str);
|
pub struct EventId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(EventId, ruma_identifiers_validation::event_id::validate);
|
opaque_identifier_validated!(EventId, ruma_identifiers_validation::event_id::validate);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
/// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is
|
/// Key identifiers in Matrix are opaque character sequences of `[a-zA-Z_]`. This type is
|
||||||
/// provided simply for its semantic value.
|
/// provided simply for its semantic value.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct KeyName(str);
|
pub struct KeyName(str);
|
||||||
|
|
||||||
opaque_identifier!(KeyName);
|
opaque_identifier!(KeyName);
|
||||||
|
@ -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")]
|
#[cfg(feature = "serde")]
|
||||||
impl serde::Serialize for $id {
|
impl serde::Serialize for $id {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
@ -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
|
/// [MXC URI]: https://matrix.org/docs/spec/client_server/r0.6.1#mxc-uri
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct MxcUri(str);
|
pub struct MxcUri(str);
|
||||||
|
|
||||||
opaque_identifier!(MxcUri);
|
opaque_identifier!(MxcUri);
|
||||||
|
@ -18,6 +18,7 @@ use crate::{server_name::ServerName, EventId, MatrixToRef};
|
|||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct RoomAliasId(str);
|
pub struct RoomAliasId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(RoomAliasId, ruma_identifiers_validation::room_alias_id::validate);
|
opaque_identifier_validated!(RoomAliasId, ruma_identifiers_validation::room_alias_id::validate);
|
||||||
|
@ -18,6 +18,7 @@ use crate::{EventId, MatrixToRef, ServerName};
|
|||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct RoomId(str);
|
pub struct RoomId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(RoomId, ruma_identifiers_validation::room_id::validate);
|
opaque_identifier_validated!(RoomId, ruma_identifiers_validation::room_id::validate);
|
||||||
|
@ -27,6 +27,7 @@ use crate::{server_name::ServerName, RoomAliasId, RoomId};
|
|||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct RoomIdOrAliasId(str);
|
pub struct RoomIdOrAliasId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(
|
opaque_identifier_validated!(
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
///
|
///
|
||||||
/// It can't exceed 255 bytes or be empty.
|
/// It can't exceed 255 bytes or be empty.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct RoomName(str);
|
pub struct RoomName(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(RoomName, ruma_identifiers_validation::room_name::validate);
|
opaque_identifier_validated!(RoomName, ruma_identifiers_validation::room_name::validate);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
/// A Matrix-spec compliant server name.
|
/// A Matrix-spec compliant server name.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct ServerName(str);
|
pub struct ServerName(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(ServerName, ruma_identifiers_validation::server_name::validate);
|
opaque_identifier_validated!(ServerName, ruma_identifiers_validation::server_name::validate);
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
/// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
|
/// Session IDs in Matrix are opaque character sequences of `[0-9a-zA-Z.=_-]`. Their length must
|
||||||
/// must not exceed 255 characters.
|
/// must not exceed 255 characters.
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct SessionId(str);
|
pub struct SessionId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(SessionId, ruma_identifiers_validation::session_id::validate);
|
opaque_identifier_validated!(SessionId, ruma_identifiers_validation::session_id::validate);
|
||||||
|
@ -18,6 +18,7 @@ use crate::{MatrixToRef, ServerName};
|
|||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct UserId(str);
|
pub struct UserId(str);
|
||||||
|
|
||||||
opaque_identifier_validated!(UserId, ruma_identifiers_validation::user_id::validate);
|
opaque_identifier_validated!(UserId, ruma_identifiers_validation::user_id::validate);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user