Skip to content

Implement RcUninit (#112566) #140640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
103 changes: 103 additions & 0 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4159,3 +4159,106 @@ 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 {
let ptr = unsafe {
Rc::allocate_for_layout(
Layout::new::<T>(),
|layout| Global.allocate(layout),
<*mut u8>::cast,
)
};

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

Self {
ptr,
weak: Weak {
ptr,
alloc: 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,
)
};

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

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::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) };
}
}
25 changes: 24 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,26 @@ 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()), 1);
assert_eq!(Weak::weak_count(x.weak()), 1);
let weak = x.weak().clone();

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

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