identifiers: Add AsRef<[u8]> impls for ID types

This commit is contained in:
Jonas Platte 2023-03-07 11:06:34 +01:00
parent 471c46273a
commit 5aa0f29935
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
6 changed files with 39 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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