diff options
| author | Jules Bertholet <julesbertholet@quoi.xyz> | 2025-07-15 17:41:31 -0400 |
|---|---|---|
| committer | Jules Bertholet <julesbertholet@quoi.xyz> | 2025-07-15 17:41:31 -0400 |
| commit | 87dd4695fe5fe2e76999d5040deb20a018e21942 (patch) | |
| tree | 39f20a8c990a437e0b610cf003b7c3c80d4de614 | |
| parent | 607fbd8d3e25997737a0bf25b215e581b922f1b0 (diff) | |
| download | rust-87dd4695fe5fe2e76999d5040deb20a018e21942.tar.gz rust-87dd4695fe5fe2e76999d5040deb20a018e21942.zip | |
Add `Default` impls for `Pin`ned `Box`, `Rc`, `Arc`
| -rw-r--r-- | library/alloc/src/boxed.rs | 16 | ||||
| -rw-r--r-- | library/alloc/src/rc.rs | 19 | ||||
| -rw-r--r-- | library/alloc/src/sync.rs | 13 |
3 files changed, 44 insertions, 4 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 3db37f1d16f..fa12d379c8c 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -1672,7 +1672,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T: Default> Default for Box<T> { - /// Creates a `Box<T>`, with the `Default` value for T. + /// Creates a `Box<T>`, with the `Default` value for `T`. #[inline] fn default() -> Self { let mut x: Box<mem::MaybeUninit<T>> = Box::new_uninit(); @@ -1695,6 +1695,7 @@ impl<T: Default> Default for Box<T> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T> Default for Box<[T]> { + /// Creates an empty `[T]` inside a `Box`. #[inline] fn default() -> Self { let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling(); @@ -1717,6 +1718,19 @@ impl Default for Box<str> { } #[cfg(not(no_global_oom_handling))] +#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")] +impl<T> Default for Pin<Box<T>> +where + T: ?Sized, + Box<T>: Default, +{ + #[inline] + fn default() -> Self { + Box::into_pin(Box::<T>::default()) + } +} + +#[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> { /// Returns a new box with a `clone()` of this box's contents. diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 5018ff4ad71..529b583cdd2 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -2357,7 +2357,7 @@ impl<T: Default> Default for Rc<T> { /// assert_eq!(*x, 0); /// ``` #[inline] - fn default() -> Rc<T> { + fn default() -> Self { unsafe { Self::from_inner( Box::leak(Box::write( @@ -2373,7 +2373,7 @@ impl<T: Default> Default for Rc<T> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "more_rc_default_impls", since = "1.80.0")] impl Default for Rc<str> { - /// Creates an empty str inside an Rc + /// Creates an empty `str` inside an `Rc`. /// /// This may or may not share an allocation with other Rcs on the same thread. #[inline] @@ -2387,7 +2387,7 @@ impl Default for Rc<str> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "more_rc_default_impls", since = "1.80.0")] impl<T> Default for Rc<[T]> { - /// Creates an empty `[T]` inside an Rc + /// Creates an empty `[T]` inside an `Rc`. /// /// This may or may not share an allocation with other Rcs on the same thread. #[inline] @@ -2397,6 +2397,19 @@ impl<T> Default for Rc<[T]> { } } +#[cfg(not(no_global_oom_handling))] +#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")] +impl<T> Default for Pin<Rc<T>> +where + T: ?Sized, + Rc<T>: Default, +{ + #[inline] + fn default() -> Self { + unsafe { Pin::new_unchecked(Rc::<T>::default()) } + } +} + #[stable(feature = "rust1", since = "1.0.0")] trait RcEqIdent<T: ?Sized + PartialEq, A: Allocator> { fn eq(&self, other: &Rc<T, A>) -> bool; diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index b8925f4544f..29caa7bc539 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -3654,6 +3654,19 @@ impl<T> Default for Arc<[T]> { } } +#[cfg(not(no_global_oom_handling))] +#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")] +impl<T> Default for Pin<Arc<T>> +where + T: ?Sized, + Arc<T>: Default, +{ + #[inline] + fn default() -> Self { + unsafe { Pin::new_unchecked(Arc::<T>::default()) } + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized + Hash, A: Allocator> Hash for Arc<T, A> { fn hash<H: Hasher>(&self, state: &mut H) { |
