Skip to content

Commit

Permalink
StableDeref trait into core
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed May 13, 2024
1 parent dde8cfa commit 269eff8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ language_item_table! {
DerefPure, sym::deref_pure, deref_pure_trait, Target::Trait, GenericRequirement::Exact(0);
DerefTarget, sym::deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
Receiver, sym::receiver, receiver_trait, Target::Trait, GenericRequirement::None;
StableDeref, sym::stable_deref, stable_deref_trait, Target::Trait, GenericRequirement::Exact(0);

Fn, kw::Fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
FnMut, sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ symbols! {
SliceIter,
Some,
SpanCtxt,
StableDeref,
String,
StructuralPartialEq,
SubdiagMessage,
Expand Down Expand Up @@ -1767,6 +1768,7 @@ symbols! {
sse,
sse4a_target_feature,
stable,
stable_deref,
staged_api,
start,
state,
Expand Down
23 changes: 23 additions & 0 deletions library/core/src/ops/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,26 @@ impl<T: ?Sized> Receiver for &T {}

#[unstable(feature = "receiver_trait", issue = "none")]
impl<T: ?Sized> Receiver for &mut T {}

#[cfg(not(bootstrap))]
#[lang = "stable_deref"]
#[unstable(feature = "stable_deref_trait", issue = "123430")]
/// # Safety
///
/// Any two calls to `deref` must return the same value at the same address unless
/// `self` has been modified in the meantime. Moves and unsizing coercions of `self`
/// are not considered modifications.
///
/// Here, "same value" means that if `deref` returns a trait object, then the actual
/// type behind that trait object must not change. Additionally, when you unsize
/// coerce from `Self` to `Unsized`, then if you call `deref` on `Unsized` and get a
/// trait object, then the underlying type of that trait object must be `<Self as
/// Deref>::Target`.
///
/// Analogous requirements apply to other unsized types. E.g., if `deref` returns
/// `[T]`, then the length must not change. In other words, the underlying type
/// must not change from `[T; N]` to `[T; M]`.
///
/// If this type implements `DerefMut`, then the same restrictions apply to calls
/// to `deref_mut`.
pub unsafe trait StableDeref: Deref {}
22 changes: 22 additions & 0 deletions library/core/src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@
use crate::cmp;
use crate::fmt;
use crate::hash::{Hash, Hasher};
#[cfg(not(bootstrap))]
use crate::ops::StableDeref;
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};

#[allow(unused_imports)]
Expand Down Expand Up @@ -1716,12 +1718,32 @@ impl<Ptr: fmt::Pointer> fmt::Pointer for Pin<Ptr> {
// `Deref<Target=Unpin>` is unsound. Any such impl would probably be unsound
// for other reasons, though, so we just need to take care not to allow such
// impls to land in std.
#[cfg(bootstrap)]
#[stable(feature = "pin", since = "1.33.0")]
impl<Ptr, U> CoerceUnsized<Pin<U>> for Pin<Ptr> where Ptr: CoerceUnsized<U> {}

#[cfg(bootstrap)]
#[stable(feature = "pin", since = "1.33.0")]
impl<Ptr, U> DispatchFromDyn<Pin<U>> for Pin<Ptr> where Ptr: DispatchFromDyn<U> {}

#[cfg(not(bootstrap))]
#[stable(feature = "pin", since = "1.33.0")]
impl<Ptr, U> CoerceUnsized<Pin<U>> for Pin<Ptr>
where
Ptr: CoerceUnsized<U> + StableDeref,
U: StableDeref,
{
}

#[cfg(not(bootstrap))]
#[stable(feature = "pin", since = "1.33.0")]
impl<Ptr, U> DispatchFromDyn<Pin<U>> for Pin<Ptr>
where
Ptr: DispatchFromDyn<U> + StableDeref,
U: StableDeref,
{
}

/// Constructs a <code>[Pin]<[&mut] T></code>, by pinning a `value: T` locally.
///
/// Unlike [`Box::pin`], this does not create a new heap allocation. As explained
Expand Down

0 comments on commit 269eff8

Please sign in to comment.