diff --git a/ruma-identifiers/src/device_id.rs b/ruma-identifiers/src/device_id.rs index 86d3d8bf..c0e92651 100644 --- a/ruma-identifiers/src/device_id.rs +++ b/ruma-identifiers/src/device_id.rs @@ -42,6 +42,11 @@ impl DeviceId { pub fn as_str(&self) -> &str { &self.0 } + + /// Creates a byte slice from this `DeviceId`. + pub fn as_bytes(&self) -> &[u8] { + self.0.as_bytes() + } } impl Clone for Box { diff --git a/ruma-identifiers/src/macros.rs b/ruma-identifiers/src/macros.rs index a4badd35..600949a9 100644 --- a/ruma-identifiers/src/macros.rs +++ b/ruma-identifiers/src/macros.rs @@ -33,6 +33,13 @@ macro_rules! common_impls { &self.full_id } } + + doc_concat! { + #[doc = concat!("Creates a byte slice from this `", stringify!($id), "`")] + pub fn as_bytes(&self) -> &[u8] { + self.full_id.as_bytes() + } + } } impl ::std::convert::AsRef for $id { diff --git a/ruma-identifiers/src/room_version_id.rs b/ruma-identifiers/src/room_version_id.rs index 290996b5..94dce568 100644 --- a/ruma-identifiers/src/room_version_id.rs +++ b/ruma-identifiers/src/room_version_id.rs @@ -86,7 +86,7 @@ impl RoomVersionId { Self::Version6 } - /// Creates a string slice from this `RoomVersionId` + /// Creates a string slice from this `RoomVersionId`. pub fn as_str(&self) -> &str { match &self { Self::Version1 => "1", @@ -99,6 +99,11 @@ impl RoomVersionId { } } + /// Creates a byte slice from this `RoomVersionId`. + pub fn as_bytes(&self) -> &[u8] { + self.as_str().as_bytes() + } + /// Whether or not this room version is an official one specified by the Matrix protocol. pub fn is_official(&self) -> bool { !self.is_custom() diff --git a/ruma-identifiers/src/server_name.rs b/ruma-identifiers/src/server_name.rs index 947bd4fa..58808ba3 100644 --- a/ruma-identifiers/src/server_name.rs +++ b/ruma-identifiers/src/server_name.rs @@ -32,6 +32,11 @@ impl ServerName { pub fn as_str(&self) -> &str { &self.0 } + + /// Creates a byte slice from this `ServerName`. + pub fn as_bytes(&self) -> &[u8] { + self.0.as_bytes() + } } impl Clone for Box {