Skip to content

Commit aa5d7af

Browse files
committed
Refactor
1 parent de1e9cd commit aa5d7af

File tree

3 files changed

+67
-89
lines changed

3 files changed

+67
-89
lines changed

library/alloc/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,10 @@
102102
#![feature(assert_matches)]
103103
#![feature(async_fn_traits)]
104104
#![feature(async_iterator)]
105-
#![feature(box_as_ptr)]
106105
#![feature(box_uninit_write)]
107106
#![feature(clone_to_uninit)]
108107
#![feature(coerce_unsized)]
109108
#![feature(const_alloc_layout)]
110-
#![feature(const_box)]
111109
#![feature(const_eval_select)]
112110
#![feature(const_heap)]
113111
#![feature(core_intrinsics)]

library/alloc/src/raw_rc.rs

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use core::num::NonZeroUsize;
1515
use core::ops::{CoerceUnsized, DispatchFromDyn};
1616
use core::pin::PinCoerceUnsized;
1717
use core::ptr::{self, NonNull};
18-
#[cfg(not(no_global_oom_handling))]
19-
use core::slice;
2018

2119
#[cfg(not(no_global_oom_handling))]
2220
use crate::alloc;
@@ -165,6 +163,30 @@ where
165163
}
166164
}
167165

166+
#[cfg(not(no_global_oom_handling))]
167+
#[track_caller]
168+
unsafe fn allocate_for_rc_with_value<T, A, const STRONG_COUNT: usize>(
169+
value: &T,
170+
alloc: &A,
171+
) -> NonNull<T>
172+
where
173+
A: Allocator,
174+
T: ?Sized,
175+
{
176+
unsafe {
177+
let rc_layout = RcLayout::from_value_ptr(NonNull::from(value));
178+
let ptr = allocate_for_rc::<A, _, STRONG_COUNT>(alloc, A::allocate, &rc_layout);
179+
180+
ptr::copy_nonoverlapping::<u8>(
181+
ptr::from_ref(value).cast(),
182+
ptr.as_ptr().cast(),
183+
mem::size_of_val(value),
184+
);
185+
186+
NonNull::new_unchecked(ptr.as_ptr().with_metadata_of(value))
187+
}
188+
}
189+
168190
fn try_allocate_uninit_for_rc<A, const STRONG_COUNT: usize>(
169191
alloc: &A,
170192
rc_layout: &RcLayout,
@@ -686,30 +708,6 @@ where
686708
unsafe { Self::from_weak(RawWeak::from_raw_parts(ptr, alloc)) }
687709
}
688710

689-
#[cfg(not(no_global_oom_handling))]
690-
fn from_box(value: Box<T, A>) -> Self
691-
where
692-
A: Allocator,
693-
{
694-
unsafe {
695-
let box_ptr = Box::as_ptr(&value);
696-
let rc_layout = RcLayout::from_value_ptr(NonNull::new_unchecked(box_ptr.cast_mut()));
697-
let rc_ptr = allocate_uninit_for_rc::<A, 1>(&Box::allocator(&value), &rc_layout);
698-
let value_size = mem::size_of_val_raw(box_ptr);
699-
700-
ptr::copy_nonoverlapping::<u8>(box_ptr.cast(), rc_ptr.as_ptr().cast(), value_size);
701-
702-
let alloc = Box::into_non_null_with_allocator(value).1;
703-
704-
drop(Box::from_raw_in(box_ptr as *mut ManuallyDrop<T>, &alloc));
705-
706-
Self::from_raw_parts(
707-
NonNull::new_unchecked(rc_ptr.as_ptr().with_metadata_of(box_ptr)),
708-
alloc,
709-
)
710-
}
711-
}
712-
713711
pub unsafe fn from_raw(ptr: NonNull<T>) -> Self
714712
where
715713
A: Default,
@@ -859,9 +857,9 @@ where
859857

860858
mem::forget(guard);
861859

862-
*ptr_ref = NonNull::new_unchecked(new_ptr.as_ptr().with_metadata_of(ptr.as_ptr()));
863-
864860
RawRc::from_raw_parts(ptr, &*alloc).drop::<R>();
861+
862+
*ptr_ref = NonNull::new_unchecked(new_ptr.as_ptr().with_metadata_of(ptr.as_ptr()));
865863
}
866864
}
867865

@@ -875,15 +873,11 @@ where
875873
let ptr = *ptr_ref;
876874

877875
unsafe {
878-
let rc_layout = RcLayout::from_value_ptr(ptr);
879-
let new_ptr = allocate_uninit_for_rc::<A, 1>(alloc, &rc_layout);
880-
let length = mem::size_of_val_raw(ptr.as_ptr());
881-
882-
ptr::copy_nonoverlapping::<u8>(ptr.as_ptr().cast(), new_ptr.as_ptr().cast(), length);
883-
884-
*ptr_ref = NonNull::new_unchecked(new_ptr.as_ptr().with_metadata_of(ptr.as_ptr()));
876+
let new_ptr = allocate_for_rc_with_value::<T, A, 1>(ptr.as_ref(), alloc);
885877

886878
RawWeak::from_raw_parts(ptr, &*alloc).drop_unchecked::<R>();
879+
880+
*ptr_ref = new_ptr;
887881
}
888882
}
889883

@@ -1168,34 +1162,6 @@ impl<T, A> RawRc<[T], A> {
11681162
uninit_rc.assume_init()
11691163
}
11701164
}
1171-
1172-
#[cfg(not(no_global_oom_handling))]
1173-
#[track_caller]
1174-
unsafe fn from_slice_copy_in(slice: &[T], alloc: A) -> Self
1175-
where
1176-
A: Allocator,
1177-
{
1178-
let uninit_rc = RawRc::new_uninit_slice_in(slice.len(), alloc);
1179-
1180-
unsafe {
1181-
ptr::copy_nonoverlapping::<T>(
1182-
slice.as_ptr(),
1183-
uninit_rc.as_ptr().as_ptr().cast(),
1184-
slice.len(),
1185-
);
1186-
1187-
uninit_rc.assume_init()
1188-
}
1189-
}
1190-
1191-
#[cfg(not(no_global_oom_handling))]
1192-
#[track_caller]
1193-
unsafe fn from_slice_copy(slice: &[T]) -> Self
1194-
where
1195-
A: Allocator + Default,
1196-
{
1197-
unsafe { Self::from_slice_copy_in(slice, A::default()) }
1198-
}
11991165
}
12001166

12011167
impl<T, A> RawRc<[MaybeUninit<T>], A> {
@@ -1562,7 +1528,17 @@ where
15621528
A: Allocator,
15631529
{
15641530
fn from(value: Box<T, A>) -> Self {
1565-
Self::from_box(value)
1531+
let value_ref = &*value;
1532+
let alloc_ref = Box::allocator(&value);
1533+
1534+
unsafe {
1535+
let rc_ptr = allocate_for_rc_with_value::<T, A, 1>(value_ref, alloc_ref);
1536+
let (box_ptr, alloc) = Box::into_raw_with_allocator(value);
1537+
1538+
drop(Box::from_raw_in(box_ptr as *mut ManuallyDrop<T>, &alloc));
1539+
1540+
Self::from_raw_parts(rc_ptr, alloc)
1541+
}
15661542
}
15671543
}
15681544

@@ -1589,7 +1565,12 @@ where
15891565
T: Copy,
15901566
{
15911567
default fn spec_from_slice(slice: &[T]) -> Self {
1592-
unsafe { RawRc::from_slice_copy(slice) }
1568+
unsafe {
1569+
let alloc = A::default();
1570+
let ptr = allocate_for_rc_with_value::<[T], A, 1>(slice, &alloc);
1571+
1572+
Self::from_raw_parts(ptr, alloc)
1573+
}
15931574
}
15941575
}
15951576

@@ -1668,12 +1649,17 @@ where
16681649
A: Allocator,
16691650
{
16701651
fn from(value: Vec<T, A>) -> Self {
1671-
let (ptr, length, capacity, alloc) = value.into_raw_parts_with_alloc();
1672-
let rc = unsafe { RawRc::from_slice_copy_in(slice::from_raw_parts(ptr, length), alloc) };
1652+
let value_ref = &*value;
1653+
let alloc_ref = value.allocator();
1654+
1655+
unsafe {
1656+
let rc_ptr = allocate_for_rc_with_value::<[T], A, 1>(value_ref, alloc_ref);
1657+
let (vec_ptr, _length, capacity, alloc) = value.into_raw_parts_with_alloc();
16731658

1674-
drop(unsafe { Vec::from_raw_parts_in(ptr, 0, capacity, rc.allocator()) });
1659+
drop(Vec::from_raw_parts_in(vec_ptr, 0, capacity, &alloc));
16751660

1676-
rc
1661+
Self::from_raw_parts(rc_ptr, alloc)
1662+
}
16771663
}
16781664
}
16791665

library/alloc/src/rc/tests.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cell::RefCell;
22
use std::clone::Clone;
33

44
use super::*;
5-
use crate::raw_rc::RcOps as _;
65

76
#[test]
87
fn test_clone() {
@@ -62,20 +61,25 @@ fn weak_self_cyclic() {
6261

6362
#[test]
6463
fn is_unique() {
65-
fn is_unique_impl(rc: &Rc<i32>) -> bool {
66-
unsafe { RcOps::is_unique(rc.raw_rc.strong_count(), rc.raw_rc.weak_count()) }
64+
fn rc_is_unique(rc: &Rc<i32>) -> bool {
65+
unsafe {
66+
<RcOps as crate::raw_rc::RcOps>::is_unique(
67+
rc.raw_rc.strong_count(),
68+
rc.raw_rc.weak_count(),
69+
)
70+
}
6771
}
6872

6973
let x = Rc::new(3);
70-
assert!(is_unique_impl(&x));
74+
assert!(rc_is_unique(&x));
7175
let y = x.clone();
72-
assert!(!is_unique_impl(&x));
76+
assert!(!rc_is_unique(&x));
7377
drop(y);
74-
assert!(is_unique_impl(&x));
78+
assert!(rc_is_unique(&x));
7579
let w = Rc::downgrade(&x);
76-
assert!(!is_unique_impl(&x));
80+
assert!(!rc_is_unique(&x));
7781
drop(w);
78-
assert!(is_unique_impl(&x));
82+
assert!(rc_is_unique(&x));
7983
}
8084

8185
#[test]
@@ -214,21 +218,11 @@ fn into_from_weak_raw() {
214218
unsafe {
215219
assert_eq!(**y_ptr, "hello");
216220

217-
dbg!(Rc::strong_count(&x));
218-
219221
let y = Weak::from_raw(y_ptr);
220-
221-
dbg!(Rc::strong_count(&x));
222-
223222
let y_up = Weak::upgrade(&y).unwrap();
224223
assert_eq!(**y_up, "hello");
225-
226-
dbg!(Rc::strong_count(&x));
227-
228224
drop(y_up);
229225

230-
dbg!(Rc::strong_count(&x));
231-
232226
assert_eq!(Rc::try_unwrap(x).map(|x| *x), Ok("hello"));
233227
}
234228
}

0 commit comments

Comments
 (0)