diff --git a/crates/ruma-identifiers/src/macros.rs b/crates/ruma-identifiers/src/macros.rs index 0a166f71..dcd27856 100644 --- a/crates/ruma-identifiers/src/macros.rs +++ b/crates/ruma-identifiers/src/macros.rs @@ -236,6 +236,20 @@ macro_rules! opaque_identifier { } } + impl From<&$id> for std::rc::Rc<$id> { + fn from(s: &$id) -> std::rc::Rc<$id> { + let rc = std::rc::Rc::::from(s.as_str()); + unsafe { std::rc::Rc::from_raw(std::rc::Rc::into_raw(rc) as *const $id) } + } + } + + impl From<&$id> for std::sync::Arc<$id> { + fn from(s: &$id) -> std::sync::Arc<$id> { + let arc = std::sync::Arc::::from(s.as_str()); + unsafe { std::sync::Arc::from_raw(std::sync::Arc::into_raw(arc) as *const $id) } + } + } + impl<'a> From<&'a str> for &'a $id { fn from(s: &'a str) -> Self { $id::from_borrowed(s) diff --git a/crates/ruma-identifiers/src/server_name.rs b/crates/ruma-identifiers/src/server_name.rs index d11b8e78..d826af1b 100644 --- a/crates/ruma-identifiers/src/server_name.rs +++ b/crates/ruma-identifiers/src/server_name.rs @@ -1,6 +1,5 @@ //! Matrix-spec compliant server names. - -use std::{convert::TryFrom, fmt, mem, str::FromStr}; +use std::{convert::TryFrom, fmt, mem, rc::Rc, str::FromStr, sync::Arc}; use ruma_identifiers_validation::server_name::validate; @@ -115,6 +114,26 @@ impl TryFrom<&str> for Box { } } +impl TryFrom<&ServerName> for Rc { + type Error = crate::Error; + + fn try_from(s: &ServerName) -> Result { + validate(s.as_str())?; + let rc = Rc::::from(s.as_str()); + Ok(unsafe { Rc::from_raw(Rc::into_raw(rc) as *const ServerName) }) + } +} + +impl TryFrom<&ServerName> for Arc { + type Error = crate::Error; + + fn try_from(s: &ServerName) -> Result { + validate(s.as_str())?; + let arc = Arc::::from(s.as_str()); + Ok(unsafe { Arc::from_raw(Arc::into_raw(arc) as *const ServerName) }) + } +} + impl TryFrom for Box { type Error = crate::Error;