Skip to content

Commit 27f5d0f

Browse files
committed
Arc: refactor away PhantomData noise.
1 parent 353c8eb commit 27f5d0f

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/liballoc/sync.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
206206
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
207207
impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T> {}
208208

209+
impl<T: ?Sized> Arc<T> {
210+
fn from_inner(ptr: NonNull<ArcInner<T>>) -> Self {
211+
Self {
212+
ptr,
213+
phantom: PhantomData,
214+
}
215+
}
216+
217+
unsafe fn from_ptr(ptr: *mut ArcInner<T>) -> Self {
218+
Self::from_inner(NonNull::new_unchecked(ptr))
219+
}
220+
}
221+
209222
/// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
210223
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak`
211224
/// pointer, which returns an [`Option`]`<`[`Arc`]`<T>>`.
@@ -290,7 +303,7 @@ impl<T> Arc<T> {
290303
weak: atomic::AtomicUsize::new(1),
291304
data,
292305
};
293-
Arc { ptr: Box::into_raw_non_null(x), phantom: PhantomData }
306+
Self::from_inner(Box::into_raw_non_null(x))
294307
}
295308

296309
/// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
@@ -403,10 +416,7 @@ impl<T: ?Sized> Arc<T> {
403416
let fake_ptr = ptr as *mut ArcInner<T>;
404417
let arc_ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset));
405418

406-
Arc {
407-
ptr: NonNull::new_unchecked(arc_ptr),
408-
phantom: PhantomData,
409-
}
419+
Self::from_ptr(arc_ptr)
410420
}
411421

412422
/// Consumes the `Arc`, returning the wrapped pointer as `NonNull<T>`.
@@ -617,7 +627,7 @@ impl<T: ?Sized> Arc<T> {
617627
// Free the allocation without dropping its contents
618628
box_free(box_unique);
619629

620-
Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
630+
Self::from_ptr(ptr)
621631
}
622632
}
623633
}
@@ -644,7 +654,7 @@ impl<T> Arc<[T]> {
644654
&mut (*ptr).data as *mut [T] as *mut T,
645655
v.len());
646656

647-
Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
657+
Self::from_ptr(ptr)
648658
}
649659
}
650660

@@ -702,7 +712,7 @@ impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
702712
// All clear. Forget the guard so it doesn't free the new ArcInner.
703713
mem::forget(guard);
704714

705-
Arc { ptr: NonNull::new_unchecked(ptr), phantom: PhantomData }
715+
Self::from_ptr(ptr)
706716
}
707717
}
708718
}
@@ -760,7 +770,7 @@ impl<T: ?Sized> Clone for Arc<T> {
760770
}
761771
}
762772

763-
Arc { ptr: self.ptr, phantom: PhantomData }
773+
Self::from_inner(self.ptr)
764774
}
765775
}
766776

@@ -1039,7 +1049,7 @@ impl Arc<dyn Any + Send + Sync> {
10391049
if (*self).is::<T>() {
10401050
let ptr = self.ptr.cast::<ArcInner<T>>();
10411051
mem::forget(self);
1042-
Ok(Arc { ptr, phantom: PhantomData })
1052+
Ok(Arc::from_inner(ptr))
10431053
} else {
10441054
Err(self)
10451055
}
@@ -1260,11 +1270,7 @@ impl<T: ?Sized> Weak<T> {
12601270

12611271
// Relaxed is valid for the same reason it is on Arc's Clone impl
12621272
match inner.strong.compare_exchange_weak(n, n + 1, Relaxed, Relaxed) {
1263-
Ok(_) => return Some(Arc {
1264-
// null checked above
1265-
ptr: self.ptr,
1266-
phantom: PhantomData,
1267-
}),
1273+
Ok(_) => return Some(Arc::from_inner(self.ptr)), // null checked above
12681274
Err(old) => n = old,
12691275
}
12701276
}

0 commit comments

Comments
 (0)