From 87194d5bf9902113328142a42ad91a82a65a2cce Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 26 Nov 2021 20:41:59 +0100 Subject: [PATCH] identifiers: Consolidate unsafe code --- crates/ruma-identifiers/src/macros.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/ruma-identifiers/src/macros.rs b/crates/ruma-identifiers/src/macros.rs index a2503234..3aa63b96 100644 --- a/crates/ruma-identifiers/src/macros.rs +++ b/crates/ruma-identifiers/src/macros.rs @@ -35,6 +35,14 @@ macro_rules! opaque_identifier_common_impls { unsafe { Box::from_raw(Box::into_raw(s) as _) } } + pub(super) fn from_rc(s: std::rc::Rc) -> std::rc::Rc { + unsafe { std::rc::Rc::from_raw(std::rc::Rc::into_raw(s) as _) } + } + + pub(super) fn from_arc(s: std::sync::Arc) -> std::sync::Arc { + unsafe { std::sync::Arc::from_raw(std::sync::Arc::into_raw(s) as _) } + } + pub(super) fn into_owned(self: Box) -> Box { unsafe { Box::from_raw(Box::into_raw(self) as _) } } @@ -89,14 +97,14 @@ macro_rules! opaque_identifier_common_impls { 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) } + <$id>::from_rc(rc) } } 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) } + <$id>::from_arc(arc) } }