File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -358,6 +358,18 @@ impl<T> Rc<T> {
358
358
}
359
359
}
360
360
361
+ #[ inline( always) ]
362
+ fn new_in_place ( f : impl FnOnce ( ) -> T ) -> Rc < T > {
363
+ let mut r: Rc < mem:: MaybeUninit < T > > = Rc :: new_uninit ( ) ;
364
+
365
+ unsafe {
366
+ let uninit: & mut mem:: MaybeUninit < T > = Rc :: get_mut_unchecked ( & mut r) ;
367
+
368
+ * uninit = mem:: MaybeUninit :: new ( f ( ) ) ;
369
+ r. assume_init ( )
370
+ }
371
+ }
372
+
361
373
/// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
362
374
/// `value` will be pinned in memory and unable to be moved.
363
375
#[ stable( feature = "pin" , since = "1.33.0" ) ]
@@ -1146,7 +1158,7 @@ impl<T: Default> Default for Rc<T> {
1146
1158
/// ```
1147
1159
#[ inline]
1148
1160
fn default ( ) -> Rc < T > {
1149
- Rc :: new ( Default :: default ( ) )
1161
+ Rc :: new_in_place ( T :: default)
1150
1162
}
1151
1163
}
1152
1164
Original file line number Diff line number Diff line change @@ -338,6 +338,18 @@ impl<T> Arc<T> {
338
338
}
339
339
}
340
340
341
+ #[ inline( always) ]
342
+ fn new_in_place ( f : impl FnOnce ( ) -> T ) -> Arc < T > {
343
+ let mut r: Arc < mem:: MaybeUninit < T > > = Arc :: new_uninit ( ) ;
344
+
345
+ unsafe {
346
+ let uninit: & mut mem:: MaybeUninit < T > = Arc :: get_mut_unchecked ( & mut r) ;
347
+
348
+ * uninit = mem:: MaybeUninit :: new ( f ( ) ) ;
349
+ r. assume_init ( )
350
+ }
351
+ }
352
+
341
353
/// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
342
354
/// `data` will be pinned in memory and unable to be moved.
343
355
#[ stable( feature = "pin" , since = "1.33.0" ) ]
@@ -1933,7 +1945,7 @@ impl<T: Default> Default for Arc<T> {
1933
1945
/// assert_eq!(*x, 0);
1934
1946
/// ```
1935
1947
fn default ( ) -> Arc < T > {
1936
- Arc :: new ( Default :: default ( ) )
1948
+ Arc :: new_in_place ( T :: default)
1937
1949
}
1938
1950
}
1939
1951
You can’t perform that action at this time.
0 commit comments