Skip to content
Open
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
82 changes: 82 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4159,3 +4159,85 @@ impl<T: ?Sized, A: Allocator> Drop for UniqueRcUninit<T, A> {
}
}
}

/// An uninitialized Rc that allows deferred construction whilst exposing weak pointers before
/// being constructed.
///
/// Weak pointers will return `None` on `upgrade` as long as [RcUninit::init] has not been called.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
#[cfg(not(no_global_oom_handling))]
pub struct RcUninit<
T,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
> {
ptr: NonNull<RcInner<T>>,
weak: Weak<T, A>,
}

impl<T> RcUninit<T> {
/// Creates new RcUninit.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn new() -> Self {
Self::new_in(Global)
}
}

impl<T, A: Allocator + Clone> RcUninit<T, A> {
/// Creates new RcUninit.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn new_in(alloc: A) -> Self {
let ptr = unsafe {
Rc::allocate_for_layout(
Layout::new::<T>(),
|layout| alloc.allocate(layout),
<*mut u8>::cast,
)
};

unsafe {
(*ptr).strong.set(0);
(*ptr).weak.set(2);
};
let ptr = NonNull::new(ptr).unwrap();

Self { ptr, weak: Weak { ptr, alloc } }
}

/// Get a weak reference.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn weak(&self) -> &Weak<T, A> {
&self.weak
}

/// Write a value and return Rc.
#[unstable(feature = "unique_rc_arc", issue = "112566")]
pub fn init(self, value: T) -> Rc<T, A> {
unsafe {
let ptr = self.weak.ptr.as_ptr();
(*ptr).strong.set(1);
let weak = &(*ptr).weak;
weak.set(weak.get() - 1);
ptr::write(&raw mut (*ptr).value, value);
}

let ptr = self.ptr;
let alloc = self.weak.alloc.clone();
mem::forget(self);

Rc { ptr, phantom: PhantomData, alloc }
}
}

#[unstable(feature = "unique_rc_arc", issue = "112566")]
impl<T> fmt::Debug for RcUninit<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(RcUninit)")
}
}

#[unstable(feature = "unique_rc_arc", issue = "112566")]
impl<T, A: Allocator> Drop for RcUninit<T, A> {
fn drop(&mut self) {
unsafe { Rc::from_inner(self.ptr) };
}
}
26 changes: 25 additions & 1 deletion library/alloctests/tests/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;
use std::cell::{Cell, RefCell};
use std::iter::TrustedLen;
use std::mem;
use std::rc::{Rc, UniqueRc, Weak};
use std::rc::{Rc, RcUninit, UniqueRc, Weak};

#[test]
fn uninhabited() {
Expand Down Expand Up @@ -922,3 +922,27 @@ fn test_unique_rc_unsizing_coercion() {
let rc: Rc<[u8]> = UniqueRc::into_rc(rc);
assert_eq!(*rc, [123, 0, 0]);
}

#[test]
fn test_rc_uninit() {
RcUninit::<()>::new();
RcUninit::<i32>::new();
RcUninit::<String>::new();
}

#[test]
fn test_rc_uninit_init() {
let x: RcUninit<i32> = RcUninit::new();
assert_eq!(Weak::strong_count(x.weak()), 0);
assert_eq!(Weak::weak_count(x.weak()), 0);
let weak = x.weak().clone();
assert!(weak.upgrade().is_none());

let rc = x.init(123);
assert_eq!(Rc::strong_count(&rc), 1);
assert_eq!(Rc::weak_count(&rc), 1);

assert_eq!(*rc, 123);
assert_eq!(weak.upgrade().map(|x| *x), Some(123));
assert!(Rc::ptr_eq(&weak.upgrade().unwrap(), &rc));
}
Loading