diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2023-09-08 14:10:49 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-08 14:10:49 +0200 |
| commit | 2cceedd0ef9fc26250f3d27f7fd4e980f1deb72d (patch) | |
| tree | 8aebbd2819373e17be3a65bf8c9bedbd18780b77 | |
| parent | 9be4eac2647432aa863f65da8a116f2eafd90ee9 (diff) | |
| parent | 571b0fef01ea994cd9a81405283049b5595499c5 (diff) | |
| download | rust-2cceedd0ef9fc26250f3d27f7fd4e980f1deb72d.tar.gz rust-2cceedd0ef9fc26250f3d27f7fd4e980f1deb72d.zip | |
Rollup merge of #104299 - mkrasnitski:discriminant-transmute-docs, r=oli-obk
Clarify stability guarantee for lifetimes in enum discriminants Since `std::mem::Discriminant` erases lifetimes, it should be clarified that changing the concrete value of a lifetime parameter does not change the value of an enum discriminant for a given variant. This is useful as it guarantees that it is safe to transmute `Discriminant<Foo<'a>>` to `Discriminant<Foo<'b>>` for any combination of `'a` and `'b`. This also holds for type-generics as long as the type parameters do not change, e.g. `Discriminant<Foo<T, 'a>>` can be transmuted to `Discriminant<Foo<T, 'b>>`. Side note: Is what I've written actually enough to imply soundness (or rather codify it), or should it specifically be spelled out that it's OK to transmute in the above way?
| -rw-r--r-- | library/core/src/mem/mod.rs | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 732fcce0f29..bc701d97bbb 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -1126,6 +1126,11 @@ impl<T> fmt::Debug for Discriminant<T> { /// /// [Reference]: ../../reference/items/enumerations.html#custom-discriminant-values-for-fieldless-enumerations /// +/// The value of a [`Discriminant<T>`] is independent of any *lifetimes* in `T`. As such, reading +/// or writing a `Discriminant<Foo<'a>>` as a `Discriminant<Foo<'b>>` (whether via [`transmute`] or +/// otherwise) is always sound. Note that this is **not** true for other kinds of generic +/// parameters; `Discriminant<Foo<A>>` and `Discriminant<Foo<B>>` might be incompatible. +/// /// # Examples /// /// This can be used to compare enums that carry data, while disregarding |
