Skip to content

Commit 9f3dc91

Browse files
committed
add (private) Rc::into_inner_with_allocator, and use it where Arc does
1 parent f23292a commit 9f3dc91

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

library/alloc/src/rc.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
365365
unsafe { self.ptr.as_ref() }
366366
}
367367

368+
#[inline]
369+
fn into_inner_with_allocator(this: Self) -> (NonNull<RcBox<T>>, A) {
370+
let this = mem::ManuallyDrop::new(this);
371+
(this.ptr, unsafe { ptr::read(&this.alloc) })
372+
}
373+
368374
#[inline]
369375
unsafe fn from_inner_in(ptr: NonNull<RcBox<T>>, alloc: A) -> Self {
370376
Self { ptr, phantom: PhantomData, alloc }
@@ -1136,8 +1142,8 @@ impl<T, A: Allocator> Rc<mem::MaybeUninit<T>, A> {
11361142
#[unstable(feature = "new_uninit", issue = "63291")]
11371143
#[inline]
11381144
pub unsafe fn assume_init(self) -> Rc<T, A> {
1139-
let (ptr, alloc) = Self::into_raw_with_allocator(self);
1140-
unsafe { Rc::from_raw_in(ptr.cast(), alloc) }
1145+
let (ptr, alloc) = Self::into_inner_with_allocator(self);
1146+
unsafe { Rc::from_inner_in(ptr.cast(), alloc) }
11411147
}
11421148
}
11431149

@@ -1177,8 +1183,8 @@ impl<T, A: Allocator> Rc<[mem::MaybeUninit<T>], A> {
11771183
#[unstable(feature = "new_uninit", issue = "63291")]
11781184
#[inline]
11791185
pub unsafe fn assume_init(self) -> Rc<[T], A> {
1180-
let (ptr, alloc) = Self::into_raw_with_allocator(self);
1181-
unsafe { Rc::from_raw_in(ptr as *const [T], alloc) }
1186+
let (ptr, alloc) = Self::into_inner_with_allocator(self);
1187+
unsafe { Rc::from_ptr_in(ptr.as_ptr() as _, alloc) }
11821188
}
11831189
}
11841190

@@ -1892,11 +1898,8 @@ impl<A: Allocator> Rc<dyn Any, A> {
18921898
#[stable(feature = "rc_downcast", since = "1.29.0")]
18931899
pub fn downcast<T: Any>(self) -> Result<Rc<T, A>, Self> {
18941900
if (*self).is::<T>() {
1895-
let this = mem::ManuallyDrop::new(self);
1896-
let ptr = this.ptr.cast::<RcBox<T>>();
1897-
// Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
1898-
let alloc = unsafe { ptr::read(&this.alloc) };
1899-
unsafe { Ok(Rc::from_inner_in(ptr, alloc)) }
1901+
let (ptr, alloc) = Rc::into_inner_with_allocator(self);
1902+
unsafe { Ok(Rc::from_inner_in(ptr.cast(), alloc)) }
19001903
} else {
19011904
Err(self)
19021905
}
@@ -1931,11 +1934,8 @@ impl<A: Allocator> Rc<dyn Any, A> {
19311934
#[inline]
19321935
#[unstable(feature = "downcast_unchecked", issue = "90850")]
19331936
pub unsafe fn downcast_unchecked<T: Any>(self) -> Rc<T, A> {
1934-
let this = mem::ManuallyDrop::new(self);
1935-
let ptr = this.ptr.cast::<RcBox<T>>();
1936-
// Safety: `this` is ManuallyDrop so the allocator will not be double-dropped
1937-
let alloc = unsafe { ptr::read(&this.alloc) };
1938-
unsafe { Rc::from_inner_in(ptr, alloc) }
1937+
let (ptr, alloc) = Rc::into_inner_with_allocator(self);
1938+
unsafe { Rc::from_inner_in(ptr.cast(), alloc) }
19391939
}
19401940
}
19411941

@@ -2688,8 +2688,8 @@ impl<T, A: Allocator, const N: usize> TryFrom<Rc<[T], A>> for Rc<[T; N], A> {
26882688

26892689
fn try_from(boxed_slice: Rc<[T], A>) -> Result<Self, Self::Error> {
26902690
if boxed_slice.len() == N {
2691-
let (ptr, alloc) = Rc::into_raw_with_allocator(boxed_slice);
2692-
Ok(unsafe { Rc::from_raw_in(ptr as *mut [T; N], alloc) })
2691+
let (ptr, alloc) = Rc::into_inner_with_allocator(boxed_slice);
2692+
Ok(unsafe { Rc::from_inner_in(ptr.cast(), alloc) })
26932693
} else {
26942694
Err(boxed_slice)
26952695
}

library/alloc/src/sync.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2529,10 +2529,8 @@ impl<A: Allocator> Arc<dyn Any + Send + Sync, A> {
25292529
T: Any + Send + Sync,
25302530
{
25312531
if (*self).is::<T>() {
2532-
unsafe {
2533-
let (ptr, alloc) = Arc::into_inner_with_allocator(self);
2534-
Ok(Arc::from_inner_in(ptr.cast(), alloc))
2535-
}
2532+
let (ptr, alloc) = Arc::into_inner_with_allocator(self);
2533+
unsafe { Ok(Arc::from_inner_in(ptr.cast(), alloc)) }
25362534
} else {
25372535
Err(self)
25382536
}
@@ -2570,10 +2568,8 @@ impl<A: Allocator> Arc<dyn Any + Send + Sync, A> {
25702568
where
25712569
T: Any + Send + Sync,
25722570
{
2573-
unsafe {
2574-
let (ptr, alloc) = Arc::into_inner_with_allocator(self);
2575-
Arc::from_inner_in(ptr.cast(), alloc)
2576-
}
2571+
let (ptr, alloc) = Arc::into_inner_with_allocator(self);
2572+
unsafe { Arc::from_inner_in(ptr.cast(), alloc) }
25772573
}
25782574
}
25792575

0 commit comments

Comments
 (0)