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 12, 2024
1 parent dde8cfa commit 711777b
Show file tree
Hide file tree
Showing 3 changed files with 25 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
22 changes: 22 additions & 0 deletions library/core/src/ops/deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,25 @@ impl<T: ?Sized> Receiver for &T {}

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

#[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 {}

0 comments on commit 711777b

Please sign in to comment.