diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index a96be57143d38..0e53fed8d0e99 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -743,6 +743,16 @@ impl Rc { unsafe { Self::from_ptr(rc_ptr) } } + /// Obtains a new reference to a raw [`Rc`] pointer, without taking ownership. + #[unstable(feature = "rc_clone_raw", issue = "48108")] + pub unsafe fn clone_raw(ptr: *const T) -> Self { + let result = unsafe { Rc::from_raw(ptr) }; + + forget(result.clone()); + + result + } + /// Creates a new [`Weak`] pointer to this allocation. /// /// # Examples diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 9d478a302e96c..eca4588ce435d 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -14,7 +14,7 @@ use core::hint; use core::intrinsics::abort; use core::iter; use core::marker::{PhantomData, Unpin, Unsize}; -use core::mem::{self, align_of_val, size_of_val}; +use core::mem::{self, align_of_val, forget, size_of_val}; use core::ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}; use core::pin::Pin; use core::ptr::{self, NonNull}; @@ -760,6 +760,16 @@ impl Arc { } } + /// Obtains a new reference to a raw [`Arc`] pointer, without taking ownership. + #[unstable(feature = "rc_clone_raw", issue = "48108")] + pub fn clone_raw(ptr: *const T) -> Self { + let result = unsafe { Arc::from_raw(ptr) }; + + forget(result.clone()); + + result + } + /// Creates a new [`Weak`] pointer to this allocation. /// /// # Examples