Skip to content

Commit

Permalink
refactor Arc<T>::default
Browse files Browse the repository at this point in the history
  • Loading branch information
slanterns committed Oct 22, 2024
1 parent fe5101d commit 224a60d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3447,13 +3447,16 @@ impl<T: Default> Default for Arc<T> {
/// assert_eq!(*x, 0);
/// ```
fn default() -> Arc<T> {
let x = Box::into_raw(Box::write(Box::new_uninit(), ArcInner {
strong: atomic::AtomicUsize::new(1),
weak: atomic::AtomicUsize::new(1),
data: T::default(),
}));
// SAFETY: `Box::into_raw` consumes the `Box` and never returns null
unsafe { Self::from_inner(NonNull::new_unchecked(x)) }
unsafe {
Self::from_inner(
Box::leak(Box::write(Box::new_uninit(), ArcInner {
strong: atomic::AtomicUsize::new(1),
weak: atomic::AtomicUsize::new(1),
data: T::default(),
}))
.into(),
)
}
}
}

Expand Down

0 comments on commit 224a60d

Please # to comment.