identifiers: Add as_bytes to all types with as_str

This commit is contained in:
Jonas Platte 2020-07-26 14:55:39 +02:00
parent 2e5fceb2b5
commit 82b7cc1637
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
4 changed files with 23 additions and 1 deletions

View File

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

View File

@ -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<str> for $id {

View File

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

View File

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