From e5c93ab928438e282f11f2bbd722a025a4eaee49 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 18 Jun 2021 13:40:32 +0200 Subject: [PATCH] identifiers: Fix up Rc / Arc conversions --- crates/ruma-identifiers/CHANGELOG.md | 4 +++ crates/ruma-identifiers/src/server_name.rs | 34 +++++++++------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/crates/ruma-identifiers/CHANGELOG.md b/crates/ruma-identifiers/CHANGELOG.md index d0b5efb3..8260ce1b 100644 --- a/crates/ruma-identifiers/CHANGELOG.md +++ b/crates/ruma-identifiers/CHANGELOG.md @@ -1,5 +1,9 @@ # [unreleased] +Improvement: + +* Add conversions of borrowed `ServerName`, `DeviceId` and `KeyName` to `Arc<_>` and `Rc<_>` + # 0.19.2 Improvements: diff --git a/crates/ruma-identifiers/src/server_name.rs b/crates/ruma-identifiers/src/server_name.rs index d826af1b..e59ea8c8 100644 --- a/crates/ruma-identifiers/src/server_name.rs +++ b/crates/ruma-identifiers/src/server_name.rs @@ -63,6 +63,20 @@ impl From<&ServerName> for Box { } } +impl From<&ServerName> for Rc { + fn from(s: &ServerName) -> Self { + let rc = Rc::::from(s.as_str()); + unsafe { Rc::from_raw(Rc::into_raw(rc) as *const ServerName) } + } +} + +impl From<&ServerName> for Arc { + fn from(s: &ServerName) -> Self { + let arc = Arc::::from(s.as_str()); + unsafe { Arc::from_raw(Arc::into_raw(arc) as *const ServerName) } + } +} + fn try_from(server_name: S) -> Result, crate::Error> where S: AsRef + Into>, @@ -114,26 +128,6 @@ 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;