From 5aa0f2993584b1acca50e4ccbbb6560e0341ce64 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Mar 2023 11:06:34 +0100 Subject: [PATCH] identifiers: Add AsRef<[u8]> impls for ID types --- crates/ruma-common/CHANGELOG.md | 1 + crates/ruma-common/src/identifiers/room_id.rs | 8 +++--- .../src/identifiers/room_or_room_alias_id.rs | 4 +-- .../src/identifiers/room_version_id.rs | 28 +++++++++++-------- crates/ruma-common/src/identifiers/user_id.rs | 2 +- crates/ruma-macros/src/identifiers.rs | 14 ++++++++++ 6 files changed, 39 insertions(+), 18 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 32c17c45..8d620cb8 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -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 diff --git a/crates/ruma-common/src/identifiers/room_id.rs b/crates/ruma-common/src/identifiers/room_id.rs index 3a0b9fbc..283de8d4 100644 --- a/crates/ruma-common/src/identifiers/room_id.rs +++ b/crates/ruma-common/src/identifiers/room_id.rs @@ -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" ); } diff --git a/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs b/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs index 16cbb518..b90b9c6e 100644 --- a/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs +++ b/crates/ruma-common/src/identifiers/room_or_room_alias_id.rs @@ -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" ); } diff --git a/crates/ruma-common/src/identifiers/room_version_id.rs b/crates/ruma-common/src/identifiers/room_version_id.rs index d8b6a9f4..4b7f3128 100644 --- a/crates/ruma-common/src/identifiers/room_version_id.rs +++ b/crates/ruma-common/src/identifiers/room_version_id.rs @@ -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 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 { - 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" ); } diff --git a/crates/ruma-common/src/identifiers/user_id.rs b/crates/ruma-common/src/identifiers/user_id.rs index 4c2cc671..fdb7f1b6 100644 --- a/crates/ruma-common/src/identifiers/user_id.rs +++ b/crates/ruma-common/src/identifiers/user_id.rs @@ -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" ); } diff --git a/crates/ruma-macros/src/identifiers.rs b/crates/ruma-macros/src/identifiers.rs index b0c500d0..262aa832 100644 --- a/crates/ruma-macros/src/identifiers.rs +++ b/crates/ruma-macros/src/identifiers.rs @@ -142,6 +142,20 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { } } + #[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 {