identifiers: Add Arc, Rc conversions to ServerName, DeviceId and KeyName

This commit is contained in:
Sivaram Konanki 2021-06-14 22:47:18 +09:00 committed by GitHub
parent ddc2138141
commit 8f6a7aeedc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -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::<str>::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::<str>::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)

View File

@ -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<ServerName> {
}
}
impl TryFrom<&ServerName> for Rc<ServerName> {
type Error = crate::Error;
fn try_from(s: &ServerName) -> Result<Self, Self::Error> {
validate(s.as_str())?;
let rc = Rc::<str>::from(s.as_str());
Ok(unsafe { Rc::from_raw(Rc::into_raw(rc) as *const ServerName) })
}
}
impl TryFrom<&ServerName> for Arc<ServerName> {
type Error = crate::Error;
fn try_from(s: &ServerName) -> Result<Self, Self::Error> {
validate(s.as_str())?;
let arc = Arc::<str>::from(s.as_str());
Ok(unsafe { Arc::from_raw(Arc::into_raw(arc) as *const ServerName) })
}
}
impl TryFrom<String> for Box<ServerName> {
type Error = crate::Error;