Skip to content

Commit d5e4fcb

Browse files
PinCoerceUnsized trait into core
1 parent 894f7a4 commit d5e4fcb

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ symbols! {
265265
Path,
266266
PathBuf,
267267
Pending,
268+
PinCoerceUnsized,
268269
Pointer,
269270
Poll,
270271
ProcMacro,

library/alloc/src/boxed.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce};
201201
use core::ops::{
202202
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver,
203203
};
204-
use core::pin::Pin;
204+
use core::pin::{Pin, PinCoerceUnsized};
205205
use core::ptr::{self, addr_of_mut, NonNull, Unique};
206206
use core::slice;
207207
use core::task::{Context, Poll};
@@ -2638,3 +2638,6 @@ impl<T: core::error::Error> core::error::Error for Box<T> {
26382638
core::error::Error::provide(&**self, request);
26392639
}
26402640
}
2641+
2642+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2643+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Box<T, A> {}

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
#![feature(maybe_uninit_uninit_array_transpose)]
139139
#![feature(panic_internals)]
140140
#![feature(pattern)]
141+
#![feature(pin_coerce_unsized_trait)]
141142
#![feature(ptr_internals)]
142143
#![feature(ptr_metadata)]
143144
#![feature(ptr_sub_ptr)]

library/alloc/src/rc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Rece
262262
use core::panic::{RefUnwindSafe, UnwindSafe};
263263
#[cfg(not(no_global_oom_handling))]
264264
use core::pin::Pin;
265+
use core::pin::PinCoerceUnsized;
265266
use core::ptr::{self, drop_in_place, NonNull};
266267
#[cfg(not(no_global_oom_handling))]
267268
use core::slice::from_raw_parts_mut;
@@ -2152,6 +2153,9 @@ impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
21522153
}
21532154
}
21542155

2156+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2157+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Rc<T, A> {}
2158+
21552159
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21562160
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
21572161

@@ -3660,6 +3664,9 @@ impl<T: ?Sized, A: Allocator> Deref for UniqueRc<T, A> {
36603664
}
36613665
}
36623666

3667+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
3668+
unsafe impl<T> PinCoerceUnsized for UniqueRc<T> {}
3669+
36633670
#[unstable(feature = "unique_rc_arc", issue = "112566")]
36643671
impl<T: ?Sized, A: Allocator> DerefMut for UniqueRc<T, A> {
36653672
fn deref_mut(&mut self) -> &mut T {

library/alloc/src/sync.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::marker::{PhantomData, Unsize};
2121
use core::mem::{self, align_of_val_raw};
2222
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
2323
use core::panic::{RefUnwindSafe, UnwindSafe};
24-
use core::pin::Pin;
24+
use core::pin::{Pin, PinCoerceUnsized};
2525
use core::ptr::{self, NonNull};
2626
#[cfg(not(no_global_oom_handling))]
2727
use core::slice::from_raw_parts_mut;
@@ -2144,6 +2144,9 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
21442144
}
21452145
}
21462146

2147+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2148+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Arc<T, A> {}
2149+
21472150
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21482151
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
21492152

library/core/src/pin.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1717,10 +1717,50 @@ impl<Ptr: fmt::Pointer> fmt::Pointer for Pin<Ptr> {
17171717
// for other reasons, though, so we just need to take care not to allow such
17181718
// impls to land in std.
17191719
#[stable(feature = "pin", since = "1.33.0")]
1720-
impl<Ptr, U> CoerceUnsized<Pin<U>> for Pin<Ptr> where Ptr: CoerceUnsized<U> {}
1720+
impl<Ptr, U> CoerceUnsized<Pin<U>> for Pin<Ptr>
1721+
where
1722+
Ptr: CoerceUnsized<U> + PinCoerceUnsized,
1723+
U: PinCoerceUnsized,
1724+
{
1725+
}
1726+
1727+
#[stable(feature = "pin", since = "1.33.0")]
1728+
impl<Ptr, U> DispatchFromDyn<Pin<U>> for Pin<Ptr>
1729+
where
1730+
Ptr: DispatchFromDyn<U> + PinCoerceUnsized,
1731+
U: PinCoerceUnsized,
1732+
{
1733+
}
1734+
1735+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
1736+
/// # Safety
1737+
///
1738+
/// Implementing this unsafe traits requires the guarantee that any two calls
1739+
/// to `Deref::deref` must return the same value at the same address, **even after moves
1740+
/// or unsize-coercions of `self`**, with exceptions of mutations to `self`.
1741+
///
1742+
/// Here, "same value" means that if `deref` returns a trait object, then the actual
1743+
/// concrete type behind that trait object must not change.
1744+
/// Additionally, when you unsize- coerce from `Self` to `Unsized`,
1745+
/// then if you call `deref` on `Unsized` which returns a trait object reference,
1746+
/// the underlying type of that trait object must be `<Self as Deref>::Target`.
1747+
///
1748+
/// Analogous requirements apply to other unsized types. E.g., if `deref` returns
1749+
/// `[T]`, then the length must not change. In other words, the underlying type
1750+
/// must not change from `[T; N]` to `[T; M]` with an `N` different from `M`.
1751+
///
1752+
/// If this type alos implements `DerefMut`, then the same guarantee must be upheld by
1753+
/// calls to `deref_mut`.
1754+
pub unsafe trait PinCoerceUnsized: Deref {}
1755+
1756+
#[stable(feature = "pin", since = "1.33.0")]
1757+
unsafe impl<'a, T: ?Sized> PinCoerceUnsized for &'a T {}
1758+
1759+
#[stable(feature = "pin", since = "1.33.0")]
1760+
unsafe impl<'a, T: ?Sized> PinCoerceUnsized for &'a mut T {}
17211761

17221762
#[stable(feature = "pin", since = "1.33.0")]
1723-
impl<Ptr, U> DispatchFromDyn<Pin<U>> for Pin<Ptr> where Ptr: DispatchFromDyn<U> {}
1763+
unsafe impl<T: PinCoerceUnsized> PinCoerceUnsized for Pin<T> {}
17241764

17251765
/// Constructs a <code>[Pin]<[&mut] T></code>, by pinning a `value: T` locally.
17261766
///

0 commit comments

Comments
 (0)