Skip to content

Commit aa0f612

Browse files
committed
Hyperlink niche and discriminant-elision optimization
1 parent ba1f116 commit aa0f612

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

reference/src/glossary.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ In this document, *layout* and *representation* are not synonyms.
6464
The *niche* of a type determines invalid bit-patterns that will be used by layout optimizations.
6565

6666
For example, `&mut T` has at least one niche, the "all zeros" bit-pattern. This
67-
niche is used by layout optimizations like "`enum` discriminant elision" to
67+
niche is used by layout optimizations like ["`enum` discriminant
68+
elision"](layout/enums.html#discriminant-elision-on-option-like-enums) to
6869
guarantee that `Option<&mut T>` has the same size as `&mut T`.
6970

7071
While all niches are invalid bit-patterns, not all invalid bit-patterns are

reference/src/layout/enums.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,18 @@ field which it contains; in the case of `Option<T>`, the payload has
313313
type `T`.
314314
315315
**Definition.** In some cases, the payload type may contain illegal
316-
values, which are called **niches**. For example, a value of type `&T`
317-
may never be `NULL`, and hence defines a niche consisting of the
316+
values, which are called **[niches][niche]**. For example, a value of type `&T`
317+
may never be `NULL`, and hence defines a [niche] consisting of the
318318
bitstring `0`. Similarly, the standard library types [`NonZeroU8`]
319319
and friends may never be zero, and hence also define the value of `0`
320-
as a niche.
320+
as a [niche].
321321
322322
[`NonZeroU8`]: https://doc.rust-lang.org/std/num/struct.NonZeroU8.html
323323
324-
The niche values must be disjoint from the values allowed by the validity
324+
The [niche] values must be disjoint from the values allowed by the validity
325325
invariant. The validity invariant is, as of this writing, the current active
326326
discussion topic in the unsafe code guidelines process. [rust-lang/rust#60300]
327-
specifies that the following types have at least one niche (the all-zeros
327+
specifies that the following types have at least one [niche] (the all-zeros
328328
bit-pattern):
329329
330330
* `&T`
@@ -334,15 +334,15 @@ bit-pattern):
334334
* `core::ptr::NonNull<T>`
335335
* `#[repr(transparent)] struct` around one of the types in this list.
336336
337-
**Option-like enums where the payload defines at least one niche value
337+
**Option-like enums where the payload defines at least one [niche] value
338338
are guaranteed to be represented using the same memory layout as their
339339
payload.** This is called **discriminant elision**, as there is no
340-
explicit discriminant value stored anywhere. Instead, niche values are
340+
explicit discriminant value stored anywhere. Instead, [niche] values are
341341
used to represent the unit variant.
342342
343343
The most common example is that `Option<&u8>` can be represented as an
344344
nullable `&u8` reference -- the `None` variant is then represented
345-
using the niche value zero. This is because a valid `&u8` value can
345+
using the [niche] value zero. This is because a valid `&u8` value can
346346
never be zero, so if we see a zero value, we know that this must be
347347
`None` variant.
348348
@@ -376,6 +376,8 @@ enum Enum2<T> {
376376
}
377377
```
378378

379+
[niche]: ../glossary.html#niche
380+
379381
## Unresolved questions
380382

381383
### Layout of single variant enums

0 commit comments

Comments
 (0)