Skip to content

Commit 87dd469

Browse files
Add Default impls for Pinned Box, Rc, Arc
1 parent 607fbd8 commit 87dd469

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

library/alloc/src/boxed.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
16721672
#[cfg(not(no_global_oom_handling))]
16731673
#[stable(feature = "rust1", since = "1.0.0")]
16741674
impl<T: Default> Default for Box<T> {
1675-
/// Creates a `Box<T>`, with the `Default` value for T.
1675+
/// Creates a `Box<T>`, with the `Default` value for `T`.
16761676
#[inline]
16771677
fn default() -> Self {
16781678
let mut x: Box<mem::MaybeUninit<T>> = Box::new_uninit();
@@ -1695,6 +1695,7 @@ impl<T: Default> Default for Box<T> {
16951695
#[cfg(not(no_global_oom_handling))]
16961696
#[stable(feature = "rust1", since = "1.0.0")]
16971697
impl<T> Default for Box<[T]> {
1698+
/// Creates an empty `[T]` inside a `Box`.
16981699
#[inline]
16991700
fn default() -> Self {
17001701
let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
@@ -1716,6 +1717,19 @@ impl Default for Box<str> {
17161717
}
17171718
}
17181719

1720+
#[cfg(not(no_global_oom_handling))]
1721+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
1722+
impl<T> Default for Pin<Box<T>>
1723+
where
1724+
T: ?Sized,
1725+
Box<T>: Default,
1726+
{
1727+
#[inline]
1728+
fn default() -> Self {
1729+
Box::into_pin(Box::<T>::default())
1730+
}
1731+
}
1732+
17191733
#[cfg(not(no_global_oom_handling))]
17201734
#[stable(feature = "rust1", since = "1.0.0")]
17211735
impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {

library/alloc/src/rc.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ impl<T: Default> Default for Rc<T> {
23572357
/// assert_eq!(*x, 0);
23582358
/// ```
23592359
#[inline]
2360-
fn default() -> Rc<T> {
2360+
fn default() -> Self {
23612361
unsafe {
23622362
Self::from_inner(
23632363
Box::leak(Box::write(
@@ -2373,7 +2373,7 @@ impl<T: Default> Default for Rc<T> {
23732373
#[cfg(not(no_global_oom_handling))]
23742374
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
23752375
impl Default for Rc<str> {
2376-
/// Creates an empty str inside an Rc
2376+
/// Creates an empty `str` inside an `Rc`.
23772377
///
23782378
/// This may or may not share an allocation with other Rcs on the same thread.
23792379
#[inline]
@@ -2387,7 +2387,7 @@ impl Default for Rc<str> {
23872387
#[cfg(not(no_global_oom_handling))]
23882388
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
23892389
impl<T> Default for Rc<[T]> {
2390-
/// Creates an empty `[T]` inside an Rc
2390+
/// Creates an empty `[T]` inside an `Rc`.
23912391
///
23922392
/// This may or may not share an allocation with other Rcs on the same thread.
23932393
#[inline]
@@ -2397,6 +2397,19 @@ impl<T> Default for Rc<[T]> {
23972397
}
23982398
}
23992399

2400+
#[cfg(not(no_global_oom_handling))]
2401+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
2402+
impl<T> Default for Pin<Rc<T>>
2403+
where
2404+
T: ?Sized,
2405+
Rc<T>: Default,
2406+
{
2407+
#[inline]
2408+
fn default() -> Self {
2409+
unsafe { Pin::new_unchecked(Rc::<T>::default()) }
2410+
}
2411+
}
2412+
24002413
#[stable(feature = "rust1", since = "1.0.0")]
24012414
trait RcEqIdent<T: ?Sized + PartialEq, A: Allocator> {
24022415
fn eq(&self, other: &Rc<T, A>) -> bool;

library/alloc/src/sync.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,6 +3654,19 @@ impl<T> Default for Arc<[T]> {
36543654
}
36553655
}
36563656

3657+
#[cfg(not(no_global_oom_handling))]
3658+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
3659+
impl<T> Default for Pin<Arc<T>>
3660+
where
3661+
T: ?Sized,
3662+
Arc<T>: Default,
3663+
{
3664+
#[inline]
3665+
fn default() -> Self {
3666+
unsafe { Pin::new_unchecked(Arc::<T>::default()) }
3667+
}
3668+
}
3669+
36573670
#[stable(feature = "rust1", since = "1.0.0")]
36583671
impl<T: ?Sized + Hash, A: Allocator> Hash for Arc<T, A> {
36593672
fn hash<H: Hasher>(&self, state: &mut H) {

0 commit comments

Comments
 (0)