about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSlanterns <slanterns.w@gmail.com>2024-10-22 01:08:15 -0700
committerSlanterns <slanterns.w@gmail.com>2024-10-22 01:37:53 -0700
commit5b12d906bb66d308fb3e94d6d79e076fa87c7f7c (patch)
tree387a0e34450b749c71355bf725c372d2f5c999be
parent1de57a5ce952c722f7053aeacfc6c90bc139b678 (diff)
downloadrust-5b12d906bb66d308fb3e94d6d79e076fa87c7f7c.tar.gz
rust-5b12d906bb66d308fb3e94d6d79e076fa87c7f7c.zip
optimize `Rc<T>::default`
-rw-r--r--library/alloc/src/rc.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs
index e98ae7c31ad..582d850e14b 100644
--- a/library/alloc/src/rc.rs
+++ b/library/alloc/src/rc.rs
@@ -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(),
+            )
+        }
     }
 }