Skip to content

Commit 072c114

Browse files
StableDeref trait into core
1 parent dde8cfa commit 072c114

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
@@ -163,7 +163,7 @@ use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce};
163163
use core::ops::{
164164
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver,
165165
};
166-
use core::pin::Pin;
166+
use core::pin::{Pin, PinCoerceUnsized};
167167
use core::ptr::{self, addr_of_mut, NonNull, Unique};
168168
use core::task::{Context, Poll};
169169

@@ -2505,3 +2505,6 @@ impl<T: core::error::Error> core::error::Error for Box<T> {
25052505
core::error::Error::provide(&**self, request);
25062506
}
25072507
}
2508+
2509+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2510+
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
@@ -139,6 +139,7 @@
139139
#![feature(maybe_uninit_uninit_array_transpose)]
140140
#![feature(panic_internals)]
141141
#![feature(pattern)]
142+
#![feature(pin_coerce_unsized_trait)]
142143
#![feature(ptr_internals)]
143144
#![feature(ptr_metadata)]
144145
#![feature(ptr_sub_ptr)]

library/alloc/src/rc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Rece
264264
use core::panic::{RefUnwindSafe, UnwindSafe};
265265
#[cfg(not(no_global_oom_handling))]
266266
use core::pin::Pin;
267+
use core::pin::PinCoerceUnsized;
267268
use core::ptr::{self, drop_in_place, NonNull};
268269
#[cfg(not(no_global_oom_handling))]
269270
use core::slice::from_raw_parts_mut;
@@ -2127,6 +2128,9 @@ impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
21272128
}
21282129
}
21292130

2131+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2132+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Rc<T, A> {}
2133+
21302134
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21312135
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
21322136

@@ -3572,6 +3576,9 @@ impl<T> Deref for UniqueRc<T> {
35723576
}
35733577
}
35743578

3579+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
3580+
unsafe impl<T> PinCoerceUnsized for UniqueRc<T> {}
3581+
35753582
#[unstable(feature = "unique_rc_arc", issue = "112566")]
35763583
impl<T> DerefMut for UniqueRc<T> {
35773584
fn deref_mut(&mut self) -> &mut T {

library/alloc/src/sync.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::mem::size_of_val;
2323
use core::mem::{self, align_of_val_raw};
2424
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
2525
use core::panic::{RefUnwindSafe, UnwindSafe};
26-
use core::pin::Pin;
26+
use core::pin::{Pin, PinCoerceUnsized};
2727
use core::ptr::{self, NonNull};
2828
#[cfg(not(no_global_oom_handling))]
2929
use core::slice::from_raw_parts_mut;
@@ -2115,6 +2115,9 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
21152115
}
21162116
}
21172117

2118+
#[unstable(feature = "pin_coerce_unsized_trait", issue = "123430")]
2119+
unsafe impl<T: ?Sized, A: Allocator> PinCoerceUnsized for Arc<T, A> {}
2120+
21182121
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21192122
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
21202123

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)