Skip to content

Commit a135342

Browse files
authored
Rollup merge of rust-lang#126285 - kpreid:unique-rc, r=dtolnay
`UniqueRc`: support allocators and `T: ?Sized`. Added the following (all unstable): * Defaulted type pararameter `A: Allocator`. * `UniqueRc::new_in()`. * `T: ?Sized` where possible. * `impl CoerceUnsized for UniqueRc`. These changes are motivated by supporting the implementation of unsized `Rc::make_mut()` (PR rust-lang#116113), but are also intended to be obvious generalizations of `UniqueRc` to support the things `Rc` does. r? ``````@the8472``````
2 parents 7fafb6d + 8ccbe9e commit a135342

File tree

2 files changed

+91
-30
lines changed

2 files changed

+91
-30
lines changed

alloc/src/rc.rs

+65-30
Original file line numberDiff line numberDiff line change
@@ -3516,7 +3516,7 @@ fn data_offset_align(align: usize) -> usize {
35163516
layout.size() + layout.padding_needed_for(align)
35173517
}
35183518

3519-
/// A uniquely owned `Rc`
3519+
/// A uniquely owned [`Rc`].
35203520
///
35213521
/// This represents an `Rc` that is known to be uniquely owned -- that is, have exactly one strong
35223522
/// reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
@@ -3554,13 +3554,24 @@ fn data_offset_align(align: usize) -> usize {
35543554
/// including fallible or async constructors.
35553555
#[unstable(feature = "unique_rc_arc", issue = "112566")]
35563556
#[derive(Debug)]
3557-
pub struct UniqueRc<T> {
3557+
pub struct UniqueRc<
3558+
T: ?Sized,
3559+
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
3560+
> {
35583561
ptr: NonNull<RcBox<T>>,
35593562
phantom: PhantomData<RcBox<T>>,
3563+
alloc: A,
35603564
}
35613565

3566+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3567+
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<UniqueRc<U, A>>
3568+
for UniqueRc<T, A>
3569+
{
3570+
}
3571+
3572+
// Depends on A = Global
35623573
impl<T> UniqueRc<T> {
3563-
/// Creates a new `UniqueRc`
3574+
/// Creates a new `UniqueRc`.
35643575
///
35653576
/// Weak references to this `UniqueRc` can be created with [`UniqueRc::downgrade`]. Upgrading
35663577
/// these weak references will fail before the `UniqueRc` has been converted into an [`Rc`].
@@ -3569,54 +3580,78 @@ impl<T> UniqueRc<T> {
35693580
#[cfg(not(no_global_oom_handling))]
35703581
#[unstable(feature = "unique_rc_arc", issue = "112566")]
35713582
pub fn new(value: T) -> Self {
3572-
Self {
3573-
ptr: Box::leak(Box::new(RcBox {
3583+
Self::new_in(value, Global)
3584+
}
3585+
}
3586+
3587+
impl<T, A: Allocator> UniqueRc<T, A> {
3588+
/// Creates a new `UniqueRc` in the provided allocator.
3589+
///
3590+
/// Weak references to this `UniqueRc` can be created with [`UniqueRc::downgrade`]. Upgrading
3591+
/// these weak references will fail before the `UniqueRc` has been converted into an [`Rc`].
3592+
/// After converting the `UniqueRc` into an [`Rc`], any weak references created beforehand will
3593+
/// point to the new [`Rc`].
3594+
#[cfg(not(no_global_oom_handling))]
3595+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3596+
pub fn new_in(value: T, alloc: A) -> Self {
3597+
let (ptr, alloc) = Box::into_unique(Box::new_in(
3598+
RcBox {
35743599
strong: Cell::new(0),
35753600
// keep one weak reference so if all the weak pointers that are created are dropped
35763601
// the UniqueRc still stays valid.
35773602
weak: Cell::new(1),
35783603
value,
3579-
}))
3580-
.into(),
3581-
phantom: PhantomData,
3582-
}
3583-
}
3584-
3585-
/// Creates a new weak reference to the `UniqueRc`
3586-
///
3587-
/// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted
3588-
/// to a [`Rc`] using [`UniqueRc::into_rc`].
3589-
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3590-
pub fn downgrade(this: &Self) -> Weak<T> {
3591-
// SAFETY: This pointer was allocated at creation time and we guarantee that we only have
3592-
// one strong reference before converting to a regular Rc.
3593-
unsafe {
3594-
this.ptr.as_ref().inc_weak();
3595-
}
3596-
Weak { ptr: this.ptr, alloc: Global }
3604+
},
3605+
alloc,
3606+
));
3607+
Self { ptr: ptr.into(), phantom: PhantomData, alloc }
35973608
}
3609+
}
35983610

3599-
/// Converts the `UniqueRc` into a regular [`Rc`]
3611+
impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
3612+
/// Converts the `UniqueRc` into a regular [`Rc`].
36003613
///
36013614
/// This consumes the `UniqueRc` and returns a regular [`Rc`] that contains the `value` that
36023615
/// is passed to `into_rc`.
36033616
///
36043617
/// Any weak references created before this method is called can now be upgraded to strong
36053618
/// references.
36063619
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3607-
pub fn into_rc(this: Self) -> Rc<T> {
3620+
pub fn into_rc(this: Self) -> Rc<T, A> {
36083621
let mut this = ManuallyDrop::new(this);
3622+
3623+
// Move the allocator out.
3624+
// SAFETY: `this.alloc` will not be accessed again, nor dropped because it is in
3625+
// a `ManuallyDrop`.
3626+
let alloc: A = unsafe { ptr::read(&this.alloc) };
3627+
36093628
// SAFETY: This pointer was allocated at creation time so we know it is valid.
36103629
unsafe {
36113630
// Convert our weak reference into a strong reference
36123631
this.ptr.as_mut().strong.set(1);
3613-
Rc::from_inner(this.ptr)
3632+
Rc::from_inner_in(this.ptr, alloc)
3633+
}
3634+
}
3635+
}
3636+
3637+
impl<T: ?Sized, A: Allocator + Clone> UniqueRc<T, A> {
3638+
/// Creates a new weak reference to the `UniqueRc`.
3639+
///
3640+
/// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted
3641+
/// to a [`Rc`] using [`UniqueRc::into_rc`].
3642+
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3643+
pub fn downgrade(this: &Self) -> Weak<T, A> {
3644+
// SAFETY: This pointer was allocated at creation time and we guarantee that we only have
3645+
// one strong reference before converting to a regular Rc.
3646+
unsafe {
3647+
this.ptr.as_ref().inc_weak();
36143648
}
3649+
Weak { ptr: this.ptr, alloc: this.alloc.clone() }
36153650
}
36163651
}
36173652

36183653
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3619-
impl<T> Deref for UniqueRc<T> {
3654+
impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {
36203655
type Target = T;
36213656

36223657
fn deref(&self) -> &T {
@@ -3626,7 +3661,7 @@ impl<T> Deref for UniqueRc<T> {
36263661
}
36273662

36283663
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3629-
impl<T> DerefMut for UniqueRc<T> {
3664+
impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {
36303665
fn deref_mut(&mut self) -> &mut T {
36313666
// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we
36323667
// have unique ownership and therefore it's safe to make a mutable reference because
@@ -3636,7 +3671,7 @@ impl<T> DerefMut for UniqueRc<T> {
36363671
}
36373672

36383673
#[unstable(feature = "unique_rc_arc", issue = "112566")]
3639-
unsafe impl<#[may_dangle] T> Drop for UniqueRc<T> {
3674+
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for UniqueRc<T, A> {
36403675
fn drop(&mut self) {
36413676
unsafe {
36423677
// destroy the contained object
@@ -3646,7 +3681,7 @@ unsafe impl<#[may_dangle] T> Drop for UniqueRc<T> {
36463681
self.ptr.as_ref().dec_weak();
36473682

36483683
if self.ptr.as_ref().weak() == 0 {
3649-
Global.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
3684+
self.alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
36503685
}
36513686
}
36523687
}

alloc/src/rc/tests.rs

+26
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,23 @@ fn test_unique_rc_drops_contents() {
606606
assert!(dropped);
607607
}
608608

609+
/// Exercise the non-default allocator usage.
610+
#[test]
611+
fn test_unique_rc_with_alloc_drops_contents() {
612+
let mut dropped = false;
613+
struct DropMe<'a>(&'a mut bool);
614+
impl Drop for DropMe<'_> {
615+
fn drop(&mut self) {
616+
*self.0 = true;
617+
}
618+
}
619+
{
620+
let rc = UniqueRc::new_in(DropMe(&mut dropped), std::alloc::System);
621+
drop(rc);
622+
}
623+
assert!(dropped);
624+
}
625+
609626
#[test]
610627
fn test_unique_rc_weak_clone_holding_ref() {
611628
let mut v = UniqueRc::new(0u8);
@@ -614,3 +631,12 @@ fn test_unique_rc_weak_clone_holding_ref() {
614631
let _ = w.clone(); // touch weak count
615632
*r = 123;
616633
}
634+
635+
#[test]
636+
fn test_unique_rc_unsizing_coercion() {
637+
let mut rc: UniqueRc<[u8]> = UniqueRc::new([0u8; 3]);
638+
assert_eq!(rc.len(), 3);
639+
rc[0] = 123;
640+
let rc: Rc<[u8]> = UniqueRc::into_rc(rc);
641+
assert_eq!(*rc, [123, 0, 0]);
642+
}

0 commit comments

Comments
 (0)