Skip to content

Commit 6e1cefc

Browse files
committed
Refactor
1 parent 9a531fc commit 6e1cefc

File tree

1 file changed

+35
-46
lines changed

1 file changed

+35
-46
lines changed

library/alloc/src/raw_rc.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ where
276276
R: RcOps,
277277
{
278278
fn drop(&mut self) {
279-
unsafe { self.weak.drop_unchecked::<R>() };
279+
unsafe { self.weak.assume_non_dangling_drop::<R>() };
280280
}
281281
}
282282

@@ -343,10 +343,6 @@ where
343343
self.ptr
344344
}
345345

346-
unsafe fn as_ref_unchecked(&self) -> &T {
347-
unsafe { self.ptr.as_ref() }
348-
}
349-
350346
unsafe fn assume_init_drop<R>(&mut self)
351347
where
352348
A: Allocator,
@@ -383,7 +379,7 @@ where
383379
{
384380
unsafe {
385381
if !self.is_dangling() {
386-
R::increment_ref_count(self.weak_count_unchecked());
382+
R::increment_ref_count(self.assume_non_dangling_weak_count());
387383
}
388384

389385
self.clone_without_increment_weak_count()
@@ -417,26 +413,22 @@ where
417413
R: RcOps,
418414
{
419415
if !self.is_dangling() {
420-
unsafe { self.drop_unchecked::<R>() };
416+
unsafe { self.assume_non_dangling_drop::<R>() };
421417
}
422418
}
423419

424-
unsafe fn drop_unchecked<R>(&mut self)
420+
unsafe fn assume_non_dangling_drop<R>(&mut self)
425421
where
426422
A: Allocator,
427423
R: RcOps,
428424
{
429425
unsafe {
430-
if R::decrement_ref_count(self.weak_count_unchecked()) {
426+
if R::decrement_ref_count(self.assume_non_dangling_weak_count()) {
431427
self.deallocate();
432428
}
433429
};
434430
}
435431

436-
unsafe fn get_mut_unchecked(&mut self) -> &mut T {
437-
unsafe { self.ptr.as_mut() }
438-
}
439-
440432
pub fn into_raw(self) -> NonNull<T> {
441433
self.ptr
442434
}
@@ -459,27 +451,27 @@ where
459451

460452
#[cfg(not(no_sync))]
461453
pub fn ref_counts(&self) -> Option<&RefCounts> {
462-
(!self.is_dangling()).then(|| unsafe { self.ref_counts_unchecked() })
454+
(!self.is_dangling()).then(|| unsafe { self.assume_non_dangling_ref_counts() })
463455
}
464456

465457
#[cfg(not(no_sync))]
466-
unsafe fn ref_counts_unchecked(&self) -> &RefCounts {
458+
unsafe fn assume_non_dangling_ref_counts(&self) -> &RefCounts {
467459
unsafe { ref_counts_ptr_from_value_ptr(self.ptr.cast()).as_ref() }
468460
}
469461

470462
pub fn strong_count(&self) -> Option<&UnsafeCell<usize>> {
471-
(!self.is_dangling()).then(|| unsafe { self.strong_count_unchecked() })
463+
(!self.is_dangling()).then(|| unsafe { self.assume_non_dangling_strong_count() })
472464
}
473465

474-
unsafe fn strong_count_unchecked(&self) -> &UnsafeCell<usize> {
466+
unsafe fn assume_non_dangling_strong_count(&self) -> &UnsafeCell<usize> {
475467
unsafe { strong_count_ptr_from_value_ptr(self.ptr.cast()).as_ref() }
476468
}
477469

478470
pub fn weak_count(&self) -> Option<&UnsafeCell<usize>> {
479-
(!self.is_dangling()).then(|| unsafe { self.weak_count_unchecked() })
471+
(!self.is_dangling()).then(|| unsafe { self.assume_non_dangling_weak_count() })
480472
}
481473

482-
unsafe fn weak_count_unchecked(&self) -> &UnsafeCell<usize> {
474+
unsafe fn assume_non_dangling_weak_count(&self) -> &UnsafeCell<usize> {
483475
unsafe { weak_count_ptr_from_value_ptr(self.ptr.cast()).as_ref() }
484476
}
485477

@@ -488,16 +480,16 @@ where
488480
A: Clone,
489481
R: RcOps,
490482
{
491-
if self.is_dangling() { None } else { unsafe { self.upgrade_unchecked::<R>() } }
483+
if self.is_dangling() { None } else { unsafe { self.assume_non_dangling_upgrade::<R>() } }
492484
}
493485

494-
unsafe fn upgrade_unchecked<R>(&self) -> Option<RawRc<T, A>>
486+
unsafe fn assume_non_dangling_upgrade<R>(&self) -> Option<RawRc<T, A>>
495487
where
496488
A: Clone,
497489
R: RcOps,
498490
{
499491
unsafe {
500-
R::upgrade(self.strong_count_unchecked())
492+
R::upgrade(self.assume_non_dangling_strong_count())
501493
.then(|| RawRc::from_raw_parts(self.ptr, self.alloc.clone()))
502494
}
503495
}
@@ -600,7 +592,7 @@ impl<T, A> RawWeak<T, A> {
600592
unsafe {
601593
let result = self.ptr.read();
602594

603-
self.drop_unchecked::<R>();
595+
self.assume_non_dangling_drop::<R>();
604596

605597
result
606598
}
@@ -798,7 +790,7 @@ where
798790
A: Allocator,
799791
R: RcOps,
800792
{
801-
unsafe { self.weak.assume_init_drop::<R>() }
793+
unsafe { self.weak.assume_init_drop::<R>() };
802794
}
803795

804796
pub unsafe fn get_mut<R>(&mut self) -> Option<&mut T>
@@ -811,7 +803,7 @@ where
811803
}
812804

813805
pub unsafe fn get_mut_unchecked(&mut self) -> &mut T {
814-
unsafe { self.weak.get_mut_unchecked() }
806+
unsafe { self.weak.as_ptr().as_mut() }
815807
}
816808

817809
pub fn into_raw(self) -> NonNull<T> {
@@ -877,7 +869,7 @@ where
877869
unsafe {
878870
let new_ptr = allocate_for_rc_with_value::<T, A, 1>(ptr.as_ref(), alloc);
879871

880-
RawWeak::from_raw_parts(ptr, &*alloc).drop_unchecked::<R>();
872+
RawWeak::from_raw_parts(ptr, &*alloc).assume_non_dangling_drop::<R>();
881873

882874
*ptr_ref = new_ptr;
883875
}
@@ -911,15 +903,15 @@ where
911903

912904
#[cfg(all(not(no_global_oom_handling), not(no_sync)))]
913905
pub fn ref_counts(&self) -> &RefCounts {
914-
unsafe { self.weak.ref_counts_unchecked() }
906+
unsafe { self.weak.assume_non_dangling_ref_counts() }
915907
}
916908

917909
pub fn strong_count(&self) -> &UnsafeCell<usize> {
918-
unsafe { self.weak.strong_count_unchecked() }
910+
unsafe { self.weak.assume_non_dangling_strong_count() }
919911
}
920912

921913
pub fn weak_count(&self) -> &UnsafeCell<usize> {
922-
unsafe { self.weak.weak_count_unchecked() }
914+
unsafe { self.weak.assume_non_dangling_weak_count() }
923915
}
924916
}
925917

@@ -996,7 +988,7 @@ impl<T, A> RawRc<T, A> {
996988
R: RcOps,
997989
{
998990
unsafe {
999-
R::decrement_ref_count(&self.strong_count())
991+
R::decrement_ref_count(self.strong_count())
1000992
.then(|| self.weak.assume_init_into_inner::<R>())
1001993
}
1002994
}
@@ -1134,13 +1126,10 @@ impl<T, A> RawRc<[T], A> {
11341126
{
11351127
fn drop(&mut self) {
11361128
unsafe {
1137-
let length = self.tail.sub_ptr(self.uninit_rc.uninit_rc.as_ptr().cast());
1129+
let head = self.uninit_rc.uninit_rc.as_ptr().cast();
1130+
let length = self.tail.sub_ptr(head);
11381131

1139-
NonNull::<[T]>::slice_from_raw_parts(
1140-
self.uninit_rc.uninit_rc.as_ptr().cast(),
1141-
length,
1142-
)
1143-
.drop_in_place();
1132+
NonNull::<[T]>::slice_from_raw_parts(head, length).drop_in_place();
11441133
}
11451134
}
11461135
}
@@ -1241,7 +1230,7 @@ where
12411230
T: ?Sized,
12421231
{
12431232
fn as_ref(&self) -> &T {
1244-
unsafe { self.weak.as_ref_unchecked() }
1233+
unsafe { self.weak.as_ptr().as_ref() }
12451234
}
12461235
}
12471236

@@ -1308,7 +1297,7 @@ where
13081297
T: Default,
13091298
{
13101299
fn spec_default() -> Self {
1311-
unsafe fn spec_default_impl<T, const N: usize>() -> RawRc<T, Global>
1300+
fn spec_default_impl<T, const N: usize>() -> RawRc<T, Global>
13121301
where
13131302
T: Default,
13141303
{
@@ -1354,13 +1343,13 @@ where
13541343
($($value:literal,)*) => {
13551344
match RefCounts::LAYOUT.padding_needed_for(T::LAYOUT.align()) / RefCounts::LAYOUT.size() {
13561345
$($value => spec_default_impl::<T, $value>,)*
1357-
_ => panic!("invalid padding"),
1346+
_ => panic!("internal error: unsupported RC layout"),
13581347
}
13591348
};
13601349
}
13611350

13621351
#[cfg(target_pointer_width = "16")]
1363-
let selected_impl: unsafe fn() -> Self = const {
1352+
let selected_impl: fn() -> Self = const {
13641353
select_impl![
13651354
0x0000, // (1 << 0) - 1
13661355
0x0001, // (1 << 1) - 1
@@ -1380,7 +1369,7 @@ where
13801369
};
13811370

13821371
#[cfg(target_pointer_width = "32")]
1383-
let selected_impl: unsafe fn() -> Self = const {
1372+
let selected_impl: fn() -> Self = const {
13841373
select_impl![
13851374
0x00000000, // (1 << 0) - 1
13861375
0x00000001, // (1 << 1) - 1
@@ -1415,7 +1404,7 @@ where
14151404
};
14161405

14171406
#[cfg(target_pointer_width = "64")]
1418-
let selected_impl: unsafe fn() -> Self = const {
1407+
let selected_impl: fn() -> Self = const {
14191408
select_impl![
14201409
0x0000000000000000, // (1 << 0) - 1
14211410
0x0000000000000001, // (1 << 1) - 1
@@ -1480,7 +1469,7 @@ where
14801469
]
14811470
};
14821471

1483-
unsafe { selected_impl() }
1472+
selected_impl()
14841473
}
14851474
}
14861475

@@ -1860,7 +1849,7 @@ where
18601849
R: RcOps,
18611850
{
18621851
unsafe {
1863-
R::unlock_strong_count(self.weak.strong_count_unchecked());
1852+
R::unlock_strong_count(self.weak.assume_non_dangling_strong_count());
18641853

18651854
RawRc::from_weak(self.weak)
18661855
}
@@ -1897,7 +1886,7 @@ where
18971886
T: ?Sized,
18981887
{
18991888
fn as_ref(&self) -> &T {
1900-
unsafe { self.weak.as_ref_unchecked() }
1889+
unsafe { self.weak.as_ptr().as_ref() }
19011890
}
19021891
}
19031892

@@ -1906,7 +1895,7 @@ where
19061895
T: ?Sized,
19071896
{
19081897
fn as_mut(&mut self) -> &mut T {
1909-
unsafe { self.weak.get_mut_unchecked() }
1898+
unsafe { self.weak.as_ptr().as_mut() }
19101899
}
19111900
}
19121901

0 commit comments

Comments
 (0)