Skip to content

Commit 1795d68

Browse files
StableDeref trait into core
1 parent dde8cfa commit 1795d68

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ symbols! {
302302
SliceIter,
303303
Some,
304304
SpanCtxt,
305+
StableDeref,
305306
String,
306307
StructuralPartialEq,
307308
SubdiagMessage,

library/alloc/src/boxed.rs

+5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ use core::iter::FusedIterator;
159159
use core::marker::Tuple;
160160
use core::marker::Unsize;
161161
use core::mem::{self, SizedTypeProperties};
162+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
163+
use core::ops::StableDeref;
162164
use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce};
163165
use core::ops::{
164166
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver,
@@ -2505,3 +2507,6 @@ impl<T: core::error::Error> core::error::Error for Box<T> {
25052507
core::error::Error::provide(&**self, request);
25062508
}
25072509
}
2510+
2511+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
2512+
unsafe impl<T: ?Sized, A: Allocator> StableDeref for Box<T, A> {}

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
#![feature(slice_index_methods)]
150150
#![feature(slice_ptr_get)]
151151
#![feature(slice_range)]
152+
#![feature(stable_deref_trait)]
152153
#![feature(std_internals)]
153154
#![feature(str_internals)]
154155
#![feature(strict_provenance)]

library/core/src/ops/deref.rs

+21
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,24 @@ impl<T: ?Sized> Receiver for &T {}
309309

310310
#[unstable(feature = "receiver_trait", issue = "none")]
311311
impl<T: ?Sized> Receiver for &mut T {}
312+
313+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
314+
/// # Safety
315+
///
316+
/// Any two calls to `deref` must return the same value at the same address unless
317+
/// `self` has been modified in the meantime. Moves and unsizing coercions of `self`
318+
/// are not considered modifications.
319+
///
320+
/// Here, "same value" means that if `deref` returns a trait object, then the actual
321+
/// type behind that trait object must not change. Additionally, when you unsize
322+
/// coerce from `Self` to `Unsized`, then if you call `deref` on `Unsized` and get a
323+
/// trait object, then the underlying type of that trait object must be `<Self as
324+
/// Deref>::Target`.
325+
///
326+
/// Analogous requirements apply to other unsized types. E.g., if `deref` returns
327+
/// `[T]`, then the length must not change. In other words, the underlying type
328+
/// must not change from `[T; N]` to `[T; M]`.
329+
///
330+
/// If this type implements `DerefMut`, then the same restrictions apply to calls
331+
/// to `deref_mut`.
332+
pub unsafe trait StableDeref: Deref {}

library/core/src/ops/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ pub use self::deref::DerefPure;
171171
#[unstable(feature = "receiver_trait", issue = "none")]
172172
pub use self::deref::Receiver;
173173

174+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
175+
pub use self::deref::StableDeref;
176+
174177
#[stable(feature = "rust1", since = "1.0.0")]
175178
pub use self::drop::Drop;
176179

library/core/src/pin.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,9 @@
923923
use crate::cmp;
924924
use crate::fmt;
925925
use crate::hash::{Hash, Hasher};
926-
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};
926+
use crate::ops::{
927+
CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver, StableDeref,
928+
};
927929

928930
#[allow(unused_imports)]
929931
use crate::{
@@ -1717,10 +1719,29 @@ impl<Ptr: fmt::Pointer> fmt::Pointer for Pin<Ptr> {
17171719
// for other reasons, though, so we just need to take care not to allow such
17181720
// impls to land in std.
17191721
#[stable(feature = "pin", since = "1.33.0")]
1720-
impl<Ptr, U> CoerceUnsized<Pin<U>> for Pin<Ptr> where Ptr: CoerceUnsized<U> {}
1722+
impl<Ptr, U> CoerceUnsized<Pin<U>> for Pin<Ptr>
1723+
where
1724+
Ptr: CoerceUnsized<U> + StableDeref,
1725+
U: StableDeref,
1726+
{
1727+
}
1728+
1729+
#[stable(feature = "pin", since = "1.33.0")]
1730+
impl<Ptr, U> DispatchFromDyn<Pin<U>> for Pin<Ptr>
1731+
where
1732+
Ptr: DispatchFromDyn<U> + StableDeref,
1733+
U: StableDeref,
1734+
{
1735+
}
1736+
1737+
#[stable(feature = "pin", since = "1.33.0")]
1738+
unsafe impl<'a, T: ?Sized> StableDeref for &'a T {}
1739+
1740+
#[stable(feature = "pin", since = "1.33.0")]
1741+
unsafe impl<'a, T: ?Sized> StableDeref for &'a mut T {}
17211742

17221743
#[stable(feature = "pin", since = "1.33.0")]
1723-
impl<Ptr, U> DispatchFromDyn<Pin<U>> for Pin<Ptr> where Ptr: DispatchFromDyn<U> {}
1744+
unsafe impl<T: StableDeref> StableDeref for Pin<T> {}
17241745

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

0 commit comments

Comments
 (0)