Skip to content

Commit

Permalink
allocate before calling T::default in <Arc<T>>::default()
Browse files Browse the repository at this point in the history
Same rationale as in the previous commit.
  • Loading branch information
jwong101 committed Oct 10, 2024
1 parent 669e255 commit 8547f51
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
#![feature(async_closure)]
#![feature(async_fn_traits)]
#![feature(async_iterator)]
#![feature(box_uninit_write)]
#![feature(clone_to_uninit)]
#![feature(coerce_unsized)]
#![feature(const_align_of_val)]
Expand Down
8 changes: 7 additions & 1 deletion alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3447,7 +3447,13 @@ impl<T: Default> Default for Arc<T> {
/// assert_eq!(*x, 0);
/// ```
fn default() -> Arc<T> {
Arc::new(Default::default())
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)) }
}
}

Expand Down

0 comments on commit 8547f51

Please sign in to comment.