Skip to content

Commit 540e5b7

Browse files
authored
Unrolled build for rust-lang#104299
Rollup merge of rust-lang#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?
2 parents cd71a37 + 571b0fe commit 540e5b7

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

library/core/src/mem/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,11 @@ impl<T> fmt::Debug for Discriminant<T> {
11261126
///
11271127
/// [Reference]: ../../reference/items/enumerations.html#custom-discriminant-values-for-fieldless-enumerations
11281128
///
1129+
/// The value of a [`Discriminant<T>`] is independent of any *lifetimes* in `T`. As such, reading
1130+
/// or writing a `Discriminant<Foo<'a>>` as a `Discriminant<Foo<'b>>` (whether via [`transmute`] or
1131+
/// otherwise) is always sound. Note that this is **not** true for other kinds of generic
1132+
/// parameters; `Discriminant<Foo<A>>` and `Discriminant<Foo<B>>` might be incompatible.
1133+
///
11291134
/// # Examples
11301135
///
11311136
/// This can be used to compare enums that carry data, while disregarding

0 commit comments

Comments
 (0)