`UniqueRc`: support allocators and `T: ?Sized`. · model-checking/verify-rust-std@8ccbe9e
@@ -3516,7 +3516,7 @@ fn data_offset_align(align: usize) -> usize {
35163516 layout.size() + layout.padding_needed_for(align)
35173517}
351835183519-/// 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+> {
35583561ptr: NonNull<RcBox<T>>,
35593562phantom: PhantomData<RcBox<T>>,
3563+alloc: A,
35603564}
356135653566+#[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
35623573impl<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")]
35713582pub 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 {
35743599strong: 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.
35773602weak: 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+}
359836103599-/// 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> {
36083621let 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.
36103629unsafe {
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}
3617365236183653#[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> {
36203655type Target = T;
3621365636223657fn deref(&self) -> &T {
@@ -3626,7 +3661,7 @@ impl<T> Deref for UniqueRc<T> {
36263661}
3627366236283663#[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> {
36303665fn 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}
3637367236383673#[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> {
36403675fn drop(&mut self) {
36413676unsafe {
36423677// destroy the contained object
@@ -3646,7 +3681,7 @@ unsafe impl<#[may_dangle] T> Drop for UniqueRc<T> {
36463681self.ptr.as_ref().dec_weak();
3647368236483683if 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}