diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-09-27 21:25:56 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-27 21:25:56 +0200 |
| commit | 01c17f83cf9c129e2d4d27cb432cd5039ea86779 (patch) | |
| tree | ce36885e86ac78754e2fdba1e7d9e47e1473d137 /library | |
| parent | c0ee51f07d271f7cf3227c60a2c59aa18c959192 (diff) | |
| parent | e88fa086fb597041a207a9bb5df0654628d57b1a (diff) | |
| download | rust-01c17f83cf9c129e2d4d27cb432cd5039ea86779.tar.gz rust-01c17f83cf9c129e2d4d27cb432cd5039ea86779.zip | |
Rollup merge of #146037 - aapoalas:reborrow-lang-experiment, r=tmandry
Introduce CoerceShared lang item and trait, and basic Reborrow tests Part of rust-lang/rust#145612: This introduces the `CoerceShared` trait which is the `Reborrow` equivalent of a `&mut T` -> `&T` coercion. The trait has a `Target` GAT which makes this (currently) unique in the `core/src/marker.rs`; I'm not sure if this can be considered problematic. Maybe this is not the way such things should be done at the marker trait level? Or maybe it is fine. Improtantly, this PR introduces a battery of basic `Reborrow` and `CoerceShared` tests. These test the very basics of the feature; custom marker types intended to have exclusive semantics (`Custom<'a>(PhantomData<&'a mut ()>)`), custom exclusive reference wrappers, and standard library exclusive reference wrappers (`Pin<&mut T>` and `Option<&mut T>`). None of these of course work since the implementation for `Reborrow` and `CoerceShared` is entirely missing, but this is the first step towards making these work. Future PRs will introduce more tests, such as "recursive" reborrowing (ie. reborrowing structs that contain multiple reborrowable fields) and checks around the lifetime semantics of reborrowing ie. that a reborrow produces a new type with the same lifetime as the original.
Diffstat (limited to 'library')
| -rw-r--r-- | library/core/src/marker.rs | 8 | ||||
| -rw-r--r-- | library/core/src/ops/mod.rs | 3 | ||||
| -rw-r--r-- | library/core/src/ops/reborrow.rs | 16 |
3 files changed, 19 insertions, 8 deletions
diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 1c100312a9a..4b767d8d622 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -1341,11 +1341,3 @@ pub macro CoercePointee($item:item) { pub trait CoercePointeeValidated { /* compiler built-in */ } - -/// Allows value to be reborrowed as exclusive, creating a copy of the value -/// that disables the source for reads and writes for the lifetime of the copy. -#[lang = "reborrow"] -#[unstable(feature = "reborrow", issue = "145612")] -pub trait Reborrow { - // Empty. -} diff --git a/library/core/src/ops/mod.rs b/library/core/src/ops/mod.rs index 87dd873fdb5..ab1ad407ee2 100644 --- a/library/core/src/ops/mod.rs +++ b/library/core/src/ops/mod.rs @@ -149,6 +149,7 @@ mod function; mod index; mod index_range; mod range; +mod reborrow; mod try_trait; mod unsize; @@ -189,6 +190,8 @@ pub use self::range::{Bound, RangeBounds, RangeInclusive, RangeToInclusive}; pub use self::range::{OneSidedRange, OneSidedRangeBound}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::range::{Range, RangeFrom, RangeFull, RangeTo}; +#[unstable(feature = "reborrow", issue = "145612")] +pub use self::reborrow::{CoerceShared, Reborrow}; #[unstable(feature = "try_trait_v2_residual", issue = "91285")] pub use self::try_trait::Residual; #[unstable(feature = "try_trait_v2_yeet", issue = "96374")] diff --git a/library/core/src/ops/reborrow.rs b/library/core/src/ops/reborrow.rs new file mode 100644 index 00000000000..f83f4233a4d --- /dev/null +++ b/library/core/src/ops/reborrow.rs @@ -0,0 +1,16 @@ +/// Allows value to be reborrowed as exclusive, creating a copy of the value +/// that disables the source for reads and writes for the lifetime of the copy. +#[lang = "reborrow"] +#[unstable(feature = "reborrow", issue = "145612")] +pub trait Reborrow { + // Empty. +} + +/// Allows reborrowable value to be reborrowed as shared, creating a copy +/// that disables the source for writes for the lifetime of the copy. +#[lang = "coerce_shared"] +#[unstable(feature = "reborrow", issue = "145612")] +pub trait CoerceShared: Reborrow { + /// The type of this value when reborrowed as shared. + type Target: Copy; +} |
