Skip to content

Commit 5ef65a4

Browse files
Auto merge of #143717 - Jules-Bertholet:pin-default, r=<try>
Add `Default` impls for `Pin`ned `Box`, `Rc`, `Arc` Fixes #143688. `@rustbot` label T-libs-api needs-fcp Also needs a crater run, as the `Box` impls could theoretically be breaking due to `#[fundamental]` (though a [cursory search](https://github.com/search?q=%2Fimpl%28%3C.*%3E%29%3F+Default+for+Pin%3C%2F+path%3A*.rs&type=code) suggests this is unlikely to cause issues).
2 parents 32cd911 + 9060b6b commit 5ef65a4

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

library/alloc/src/boxed.rs

Lines changed: 22 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();
@@ -1692,16 +1692,37 @@ impl<T: Default> Default for Box<T> {
16921692
}
16931693
}
16941694

1695+
#[cfg(not(no_global_oom_handling))]
1696+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
1697+
impl<T: Default> Default for Pin<Box<T>> {
1698+
/// Creates a pinned `Box<T>`, with the `Default` value for `T`.
1699+
#[inline]
1700+
fn default() -> Self {
1701+
Box::into_pin(Box::<T>::default())
1702+
}
1703+
}
1704+
16951705
#[cfg(not(no_global_oom_handling))]
16961706
#[stable(feature = "rust1", since = "1.0.0")]
16971707
impl<T> Default for Box<[T]> {
1708+
/// Creates an empty `[T]` inside a `Box`.
16981709
#[inline]
16991710
fn default() -> Self {
17001711
let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
17011712
Box(ptr, Global)
17021713
}
17031714
}
17041715

1716+
#[cfg(not(no_global_oom_handling))]
1717+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
1718+
impl<T> Default for Pin<Box<[T]>> {
1719+
/// Creates an empty `[T]` inside a pinned `Box`.
1720+
#[inline]
1721+
fn default() -> Self {
1722+
Box::into_pin(Box::<[T]>::default())
1723+
}
1724+
}
1725+
17051726
#[cfg(not(no_global_oom_handling))]
17061727
#[stable(feature = "default_box_extra", since = "1.17.0")]
17071728
impl Default for Box<str> {

library/alloc/src/rc.rs

Lines changed: 23 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(
@@ -2370,10 +2370,20 @@ impl<T: Default> Default for Rc<T> {
23702370
}
23712371
}
23722372

2373+
#[cfg(not(no_global_oom_handling))]
2374+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
2375+
impl<T: Default> Default for Pin<Rc<T>> {
2376+
/// Creates a new pinned `Rc<T>`, with the `Default` value for `T`.
2377+
#[inline]
2378+
fn default() -> Self {
2379+
unsafe { Pin::new_unchecked(Rc::<T>::default()) }
2380+
}
2381+
}
2382+
23732383
#[cfg(not(no_global_oom_handling))]
23742384
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
23752385
impl Default for Rc<str> {
2376-
/// Creates an empty str inside an Rc
2386+
/// Creates an empty `str` inside an `Rc`.
23772387
///
23782388
/// This may or may not share an allocation with other Rcs on the same thread.
23792389
#[inline]
@@ -2387,7 +2397,7 @@ impl Default for Rc<str> {
23872397
#[cfg(not(no_global_oom_handling))]
23882398
#[stable(feature = "more_rc_default_impls", since = "1.80.0")]
23892399
impl<T> Default for Rc<[T]> {
2390-
/// Creates an empty `[T]` inside an Rc
2400+
/// Creates an empty `[T]` inside an `Rc`.
23912401
///
23922402
/// This may or may not share an allocation with other Rcs on the same thread.
23932403
#[inline]
@@ -2397,6 +2407,16 @@ impl<T> Default for Rc<[T]> {
23972407
}
23982408
}
23992409

2410+
#[cfg(not(no_global_oom_handling))]
2411+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
2412+
impl<T> Default for Pin<Rc<[T]>> {
2413+
/// Creates an empty `[T]` inside a pinned `Rc`.
2414+
#[inline]
2415+
fn default() -> Self {
2416+
unsafe { Pin::new_unchecked(Rc::<[T]>::default()) }
2417+
}
2418+
}
2419+
24002420
#[stable(feature = "rust1", since = "1.0.0")]
24012421
trait RcEqIdent<T: ?Sized + PartialEq, A: Allocator> {
24022422
fn eq(&self, other: &Rc<T, A>) -> bool;

library/alloc/src/sync.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,6 +3571,16 @@ impl<T: Default> Default for Arc<T> {
35713571
}
35723572
}
35733573

3574+
#[cfg(not(no_global_oom_handling))]
3575+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
3576+
impl<T: Default> Default for Pin<Arc<T>> {
3577+
/// Creates a new pinned `Arc<T>`, with the `Default` value for `T`.
3578+
#[inline]
3579+
fn default() -> Pin<Arc<T>> {
3580+
unsafe { Pin::new_unchecked(Arc::<T>::default()) }
3581+
}
3582+
}
3583+
35743584
/// Struct to hold the static `ArcInner` used for empty `Arc<str/CStr/[T]>` as
35753585
/// returned by `Default::default`.
35763586
///
@@ -3654,6 +3664,16 @@ impl<T> Default for Arc<[T]> {
36543664
}
36553665
}
36563666

3667+
#[cfg(not(no_global_oom_handling))]
3668+
#[stable(feature = "pin_default_impls", since = "CURRENT_RUSTC_VERSION")]
3669+
impl<T> Default for Pin<Arc<[T]>> {
3670+
/// Creates an empty `[T]` inside a pinned `Arc`.
3671+
#[inline]
3672+
fn default() -> Self {
3673+
unsafe { Pin::new_unchecked(Arc::<[T]>::default()) }
3674+
}
3675+
}
3676+
36573677
#[stable(feature = "rust1", since = "1.0.0")]
36583678
impl<T: ?Sized + Hash, A: Allocator> Hash for Arc<T, A> {
36593679
fn hash<H: Hasher>(&self, state: &mut H) {

0 commit comments

Comments
 (0)