identifiers: Fix up Rc / Arc conversions

This commit is contained in:
Jonas Platte 2021-06-18 13:40:32 +02:00
parent 5d0803f63d
commit e5c93ab928
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 18 additions and 20 deletions

View File

@ -1,5 +1,9 @@
# [unreleased] # [unreleased]
Improvement:
* Add conversions of borrowed `ServerName`, `DeviceId` and `KeyName` to `Arc<_>` and `Rc<_>`
# 0.19.2 # 0.19.2
Improvements: Improvements:

View File

@ -63,6 +63,20 @@ impl From<&ServerName> for Box<ServerName> {
} }
} }
impl From<&ServerName> for Rc<ServerName> {
fn from(s: &ServerName) -> Self {
let rc = Rc::<str>::from(s.as_str());
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const ServerName) }
}
}
impl From<&ServerName> for Arc<ServerName> {
fn from(s: &ServerName) -> Self {
let arc = Arc::<str>::from(s.as_str());
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const ServerName) }
}
}
fn try_from<S>(server_name: S) -> Result<Box<ServerName>, crate::Error> fn try_from<S>(server_name: S) -> Result<Box<ServerName>, crate::Error>
where where
S: AsRef<str> + Into<Box<str>>, S: AsRef<str> + Into<Box<str>>,
@ -114,26 +128,6 @@ 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> { impl TryFrom<String> for Box<ServerName> {
type Error = crate::Error; type Error = crate::Error;