Rollup merge of #126285 - kpreid:unique-rc, r=dtolnay · model-checking/verify-rust-std@a135342 (original) (raw)

`@@ -3516,7 +3516,7 @@ fn data_offset_align(align: usize) -> usize {

`

3516

3516

` layout.size() + layout.padding_needed_for(align)

`

3517

3517

`}

`

3518

3518

``

3519

``

`` -

/// A uniquely owned Rc

``

``

3519

`` +

/// A uniquely owned [Rc].

``

3520

3520

`///

`

3521

3521

`` /// This represents an Rc that is known to be uniquely owned -- that is, have exactly one strong

``

3522

3522

`/// 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 {

`

3554

3554

`/// including fallible or async constructors.

`

3555

3555

`#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

3556

3556

`#[derive(Debug)]

`

3557

``

`-

pub struct UniqueRc {

`

``

3557

`+

pub struct UniqueRc<

`

``

3558

`+

T: ?Sized,

`

``

3559

`+

#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,

`

``

3560

`+

{

`

3558

3561

`ptr: NonNull<RcBox>,

`

3559

3562

`phantom: PhantomData<RcBox>,

`

``

3563

`+

alloc: A,

`

3560

3564

`}

`

3561

3565

``

``

3566

`+

#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

``

3567

`+

impl<T: ?Sized + Unsize, U: ?Sized, A: Allocator> CoerceUnsized<UniqueRc<U, A>>

`

``

3568

`+

for UniqueRc<T, A>

`

``

3569

`+

{

`

``

3570

`+

}

`

``

3571

+

``

3572

`+

// Depends on A = Global

`

3562

3573

`impl UniqueRc {

`

3563

``

`` -

/// Creates a new UniqueRc

``

``

3574

`` +

/// Creates a new UniqueRc.

``

3564

3575

`///

`

3565

3576

`` /// Weak references to this UniqueRc can be created with [UniqueRc::downgrade]. Upgrading

``

3566

3577

`` /// these weak references will fail before the UniqueRc has been converted into an [Rc].

``

`@@ -3569,54 +3580,78 @@ impl UniqueRc {

`

3569

3580

`#[cfg(not(no_global_oom_handling))]

`

3570

3581

`#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

3571

3582

`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 {

`

3574

3599

`strong: Cell::new(0),

`

3575

3600

`// keep one weak reference so if all the weak pointers that are created are dropped

`

3576

3601

`// the UniqueRc still stays valid.

`

3577

3602

`weak: Cell::new(1),

`

3578

3603

` 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 {

`

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 }

`

3597

3608

`}

`

``

3609

`+

}

`

3598

3610

``

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].

``

3600

3613

`///

`

3601

3614

`` /// This consumes the UniqueRc and returns a regular [Rc] that contains the value that

``

3602

3615

`` /// is passed to into_rc.

``

3603

3616

`///

`

3604

3617

`/// Any weak references created before this method is called can now be upgraded to strong

`

3605

3618

`/// references.

`

3606

3619

`#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

3607

``

`-

pub fn into_rc(this: Self) -> Rc {

`

``

3620

`+

pub fn into_rc(this: Self) -> Rc<T, A> {

`

3608

3621

`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

+

3609

3628

`// SAFETY: This pointer was allocated at creation time so we know it is valid.

`

3610

3629

`unsafe {

`

3611

3630

`// Convert our weak reference into a strong reference

`

3612

3631

` 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();

`

3614

3648

`}

`

``

3649

`+

Weak { ptr: this.ptr, alloc: this.alloc.clone() }

`

3615

3650

`}

`

3616

3651

`}

`

3617

3652

``

3618

3653

`#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

3619

``

`-

impl Deref for UniqueRc {

`

``

3654

`+

impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {

`

3620

3655

`type Target = T;

`

3621

3656

``

3622

3657

`fn deref(&self) -> &T {

`

`@@ -3626,7 +3661,7 @@ impl Deref for UniqueRc {

`

3626

3661

`}

`

3627

3662

``

3628

3663

`#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

3629

``

`-

impl DerefMut for UniqueRc {

`

``

3664

`+

impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {

`

3630

3665

`fn deref_mut(&mut self) -> &mut T {

`

3631

3666

`// SAFETY: This pointer was allocated at creation time so we know it is valid. We know we

`

3632

3667

`// have unique ownership and therefore it's safe to make a mutable reference because

`

`@@ -3636,7 +3671,7 @@ impl DerefMut for UniqueRc {

`

3636

3671

`}

`

3637

3672

``

3638

3673

`#[unstable(feature = "unique_rc_arc", issue = "112566")]

`

3639

``

`-

unsafe impl<#[may_dangle] T> Drop for UniqueRc {

`

``

3674

`+

unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for UniqueRc<T, A> {

`

3640

3675

`fn drop(&mut self) {

`

3641

3676

`unsafe {

`

3642

3677

`// destroy the contained object

`

`@@ -3646,7 +3681,7 @@ unsafe impl<#[may_dangle] T> Drop for UniqueRc {

`

3646

3681

`self.ptr.as_ref().dec_weak();

`

3647

3682

``

3648

3683

`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()));

`

3650

3685

`}

`

3651

3686

`}

`

3652

3687

`}

`