identifiers: Add AsRef<[u8]> impls for ID types
This commit is contained in:
parent
471c46273a
commit
5aa0f29935
@ -10,6 +10,7 @@ Improvements:
|
||||
- Add convenience methods for `push::Ruleset`:
|
||||
- To update the server-default push rules
|
||||
- To remove a user-defined push rule
|
||||
- Add `AsRef<[u8]>` implementations for identifier types
|
||||
|
||||
# 0.11.3
|
||||
|
||||
|
@ -218,7 +218,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
<&RoomId>::try_from("!29fhd83h92h0:example.com")
|
||||
.expect("Failed to create RoomId.")
|
||||
.as_ref(),
|
||||
.as_str(),
|
||||
"!29fhd83h92h0:example.com"
|
||||
);
|
||||
}
|
||||
@ -226,7 +226,7 @@ mod tests {
|
||||
#[test]
|
||||
fn empty_localpart() {
|
||||
assert_eq!(
|
||||
<&RoomId>::try_from("!:example.com").expect("Failed to create RoomId.").as_ref(),
|
||||
<&RoomId>::try_from("!:example.com").expect("Failed to create RoomId.").as_str(),
|
||||
"!:example.com"
|
||||
);
|
||||
}
|
||||
@ -268,7 +268,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
<&RoomId>::try_from("!29fhd83h92h0:example.com:443")
|
||||
.expect("Failed to create RoomId.")
|
||||
.as_ref(),
|
||||
.as_str(),
|
||||
"!29fhd83h92h0:example.com:443"
|
||||
);
|
||||
}
|
||||
@ -278,7 +278,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
<&RoomId>::try_from("!29fhd83h92h0:example.com:5000")
|
||||
.expect("Failed to create RoomId.")
|
||||
.as_ref(),
|
||||
.as_str(),
|
||||
"!29fhd83h92h0:example.com:5000"
|
||||
);
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
<&RoomOrAliasId>::try_from("#ruma:example.com")
|
||||
.expect("Failed to create RoomAliasId.")
|
||||
.as_ref(),
|
||||
.as_str(),
|
||||
"#ruma:example.com"
|
||||
);
|
||||
}
|
||||
@ -161,7 +161,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
<&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
|
||||
.expect("Failed to create RoomId.")
|
||||
.as_ref(),
|
||||
.as_str(),
|
||||
"!29fhd83h92h0:example.com"
|
||||
);
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ use super::IdParseError;
|
||||
///
|
||||
/// ```
|
||||
/// # use ruma_common::RoomVersionId;
|
||||
/// assert_eq!(RoomVersionId::try_from("1").unwrap().as_ref(), "1");
|
||||
/// assert_eq!(RoomVersionId::try_from("1").unwrap().as_str(), "1");
|
||||
/// ```
|
||||
///
|
||||
/// Any string consisting of at minimum 1, at maximum 32 unicode codepoints is a room version ID.
|
||||
@ -110,6 +110,12 @@ impl AsRef<str> for RoomVersionId {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for RoomVersionId {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for RoomVersionId {
|
||||
/// Compare the two given room version IDs by comparing their string representations.
|
||||
///
|
||||
@ -117,7 +123,7 @@ impl PartialOrd for RoomVersionId {
|
||||
/// specification. This implementation only exists to be able to use `RoomVersionId`s or
|
||||
/// types containing `RoomVersionId`s as `BTreeMap` keys.
|
||||
fn partial_cmp(&self, other: &RoomVersionId) -> Option<Ordering> {
|
||||
self.as_ref().partial_cmp(other.as_ref())
|
||||
self.as_str().partial_cmp(other.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +134,7 @@ impl Ord for RoomVersionId {
|
||||
/// specification. This implementation only exists to be able to use `RoomVersionId`s or
|
||||
/// types containing `RoomVersionId`s as `BTreeMap` keys.
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.as_ref().cmp(other.as_ref())
|
||||
self.as_str().cmp(other.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
@ -137,7 +143,7 @@ impl Serialize for RoomVersionId {
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.as_ref())
|
||||
serializer.serialize_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
@ -257,7 +263,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_version_1_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("1").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("1").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"1"
|
||||
);
|
||||
}
|
||||
@ -265,7 +271,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_version_2_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("2").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("2").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"2"
|
||||
);
|
||||
}
|
||||
@ -273,7 +279,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_version_3_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("3").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("3").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"3"
|
||||
);
|
||||
}
|
||||
@ -281,7 +287,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_version_4_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("4").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("4").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"4"
|
||||
);
|
||||
}
|
||||
@ -289,7 +295,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_version_5_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("5").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("5").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"5"
|
||||
);
|
||||
}
|
||||
@ -297,7 +303,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_version_6_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("6").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("6").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"6"
|
||||
);
|
||||
}
|
||||
@ -305,7 +311,7 @@ mod tests {
|
||||
#[test]
|
||||
fn valid_custom_room_version_id() {
|
||||
assert_eq!(
|
||||
RoomVersionId::try_from("io.ruma.1").expect("Failed to create RoomVersionId.").as_ref(),
|
||||
RoomVersionId::try_from("io.ruma.1").expect("Failed to create RoomVersionId.").as_str(),
|
||||
"io.ruma.1"
|
||||
);
|
||||
}
|
||||
|
@ -289,7 +289,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
<&UserId>::try_from("@carl:example.com:443")
|
||||
.expect("Failed to create UserId.")
|
||||
.as_ref(),
|
||||
.as_str(),
|
||||
"@carl:example.com:443"
|
||||
);
|
||||
}
|
||||
|
@ -142,6 +142,20 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result<TokenStream> {
|
||||
}
|
||||
}
|
||||
|
||||
#[automatically_derived]
|
||||
impl #impl_generics AsRef<[u8]> for #id_ty {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[automatically_derived]
|
||||
impl #impl_generics AsRef<[u8]> for Box<#id_ty> {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
self.as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
#[automatically_derived]
|
||||
impl #impl_generics From<&#id_ty> for String {
|
||||
fn from(id: &#id_ty) -> Self {
|
||||
|
Loading…
x
Reference in New Issue
Block a user