Skip to content

Add Default impls for Pinned Box, Rc, Arc #143717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -1692,16 +1692,37 @@ impl<T: Default> Default for Box<T> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T: Default> Default for Pin<Box<T>> {
/// Creates a pinned `Box<T>`, with the `Default` value for `T`.
#[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> Default for Box<[T]> {
/// Creates an empty `[T]` inside a `Box`.
#[inline]
fn default() -> Self {
let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
Box(ptr, Global)
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T> Default for Pin<Box<[T]>> {
/// Creates an empty `[T]` inside a pinned `Box`.
#[inline]
fn default() -> Self {
Box::into_pin(Box::<[T]>::default())
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "default_box_extra", since = "1.17.0")]
impl Default for Box<str> {
Expand Down
26 changes: 23 additions & 3 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -2370,10 +2370,20 @@ impl<T: Default> Default for Rc<T> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T: Default> Default for Pin<Rc<T>> {
/// Creates a new pinned `Rc<T>`, with the `Default` value for `T`.
#[inline]
fn default() -> Self {
unsafe { Pin::new_unchecked(Rc::<T>::default()) }
}
}

#[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]
Expand All @@ -2387,7 +2397,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]
Expand All @@ -2397,6 +2407,16 @@ 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]>> {
/// Creates an empty `[T]` inside a pinned `Rc`.
#[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;
Expand Down
20 changes: 20 additions & 0 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3571,6 +3571,16 @@ impl<T: Default> Default for Arc<T> {
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
impl<T: Default> Default for Pin<Arc<T>> {
/// Creates a new pinned `Arc<T>`, with the `Default` value for `T`.
#[inline]
fn default() -> Pin<Arc<T>> {
unsafe { Pin::new_unchecked(Arc::<T>::default()) }
}
}

/// Struct to hold the static `ArcInner` used for empty `Arc<str/CStr/[T]>` as
/// returned by `Default::default`.
///
Expand Down Expand Up @@ -3654,6 +3664,16 @@ 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]>> {
/// Creates an empty `[T]` inside a pinned `Arc`.
#[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) {
Expand Down
Loading