optimize Rc<T>::default

This commit is contained in:
Slanterns 2024-10-22 01:08:15 -07:00
parent 1de57a5ce9
commit 5b12d906bb
No known key found for this signature in database
GPG Key ID: 1AA37EE7FAAE433A

View File

@ -2312,7 +2312,16 @@ impl<T: Default> Default for Rc<T> {
/// ```
#[inline]
fn default() -> Rc<T> {
Rc::new(Default::default())
unsafe {
Self::from_inner(
Box::leak(Box::write(Box::new_uninit(), RcInner {
strong: Cell::new(1),
weak: Cell::new(1),
value: T::default(),
}))
.into(),
)
}
}
}