Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,16 @@ impl<T: ?Sized> Rc<T> {
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
Expand Down
12 changes: 11 additions & 1 deletion library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -760,6 +760,16 @@ impl<T: ?Sized> Arc<T> {
}
}

/// 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
Expand Down