Skip to content

Commit 77f8779

Browse files
StableDeref trait into core
1 parent dde8cfa commit 77f8779

File tree

11 files changed

+77
-6
lines changed

11 files changed

+77
-6
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-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ use core::marker::Unsize;
161161
use core::mem::{self, SizedTypeProperties};
162162
use core::ops::{AsyncFn, AsyncFnMut, AsyncFnOnce};
163163
use core::ops::{
164-
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver,
164+
CoerceUnsized, Coroutine, CoroutineState, Deref, DerefMut, DerefPure, DispatchFromDyn,
165+
Receiver, StableDeref,
165166
};
166167
use core::pin::Pin;
167168
use core::ptr::{self, addr_of_mut, NonNull, Unique};
@@ -2505,3 +2506,6 @@ impl<T: core::error::Error> core::error::Error for Box<T> {
25052506
core::error::Error::provide(&**self, request);
25062507
}
25072508
}
2509+
2510+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
2511+
unsafe impl<T: ?Sized, A: Allocator> StableDeref for Box<T, A> {}

library/alloc/src/ffi/c_str.rs

+3
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@ impl ops::Deref for CString {
718718
}
719719
}
720720

721+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
722+
unsafe impl ops::StableDeref for CString {}
723+
721724
#[stable(feature = "rust1", since = "1.0.0")]
722725
impl fmt::Debug for CString {
723726
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

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/alloc/src/rc.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ use core::marker::{PhantomData, Unsize};
260260
#[cfg(not(no_global_oom_handling))]
261261
use core::mem::size_of_val;
262262
use core::mem::{self, align_of_val_raw, forget, ManuallyDrop};
263-
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};
263+
use core::ops::{
264+
CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver, StableDeref,
265+
};
264266
use core::panic::{RefUnwindSafe, UnwindSafe};
265267
#[cfg(not(no_global_oom_handling))]
266268
use core::pin::Pin;
@@ -2127,6 +2129,9 @@ impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
21272129
}
21282130
}
21292131

2132+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
2133+
unsafe impl<T: ?Sized, A: Allocator> StableDeref for Rc<T, A> {}
2134+
21302135
#[unstable(feature = "deref_pure_trait", issue = "87121")]
21312136
unsafe impl<T: ?Sized, A: Allocator> DerefPure for Rc<T, A> {}
21322137

@@ -3572,6 +3577,9 @@ impl<T> Deref for UniqueRc<T> {
35723577
}
35733578
}
35743579

3580+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
3581+
unsafe impl<T> StableDeref for UniqueRc<T> {}
3582+
35753583
#[unstable(feature = "unique_rc_arc", issue = "112566")]
35763584
impl<T> DerefMut for UniqueRc<T> {
35773585
fn deref_mut(&mut self) -> &mut T {

library/alloc/src/string.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2481,6 +2481,9 @@ impl ops::Deref for String {
24812481
}
24822482
}
24832483

2484+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
2485+
unsafe impl ops::StableDeref for String {}
2486+
24842487
#[unstable(feature = "deref_pure_trait", issue = "87121")]
24852488
unsafe impl ops::DerefPure for String {}
24862489

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
#[cfg(not(no_global_oom_handling))]
2222
use core::mem::size_of_val;
2323
use core::mem::{self, align_of_val_raw};
24-
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
24+
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver, StableDeref};
2525
use core::panic::{RefUnwindSafe, UnwindSafe};
2626
use core::pin::Pin;
2727
use core::ptr::{self, NonNull};
@@ -2115,6 +2115,9 @@ impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
21152115
}
21162116
}
21172117

2118+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
2119+
unsafe impl<T: ?Sized, A: Allocator> StableDeref 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/alloc/src/vec/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,9 @@ impl<T, A: Allocator> ops::Deref for Vec<T, A> {
28182818
}
28192819
}
28202820

2821+
#[unstable(feature = "stable_deref_trait", issue = "123430")]
2822+
unsafe impl<T, A: Allocator> ops::StableDeref for Vec<T, A> {}
2823+
28212824
#[stable(feature = "rust1", since = "1.0.0")]
28222825
impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
28232826
#[inline]

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)