Skip to content

Commit a88e9b8

Browse files
authored
Merge pull request #2375 from DanielEScherzer/phantomdata-1-zst
PhantomData: clearly document alignment of 1, add to glossary examples
2 parents a286e1e + 243849f commit a88e9b8

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎src/glossary.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ Examples of 1-ZSTs include:
122122
- `repr(C)` [structs] with no fields or where all fields are 1-ZSTs (see [layout.repr.c.struct.align] and [layout.repr.c.struct.size-field-offset]).
123123
- `repr(transparent)` [structs] with no fields or where all fields are 1-ZSTs (see [layout.repr.transparent.layout-abi]).
124124
- [Arrays] of 1-ZSTs (see [layout.array]).
125+
- [`PhantomData<T>`] for any type `T` (see [lang-types.phantom-data]).
125126

126127
```rust
128+
# use core::marker::PhantomData;
127129
# use core::mem::{align_of, size_of};
128130
#[repr(C)]
129131
struct C1 {}
@@ -151,6 +153,8 @@ assert!(is_1_zst::<C2>());
151153
assert!(is_1_zst::<[(); 10]>());
152154
assert!(is_1_zst::<T1>());
153155
assert!(is_1_zst::<T2>());
156+
assert!(is_1_zst::<PhantomData<()>>());
157+
assert!(is_1_zst::<PhantomData<u16>>());
154158
```
155159

156160
### Name
@@ -273,8 +277,10 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
273277
- [Arrays] of zero-sized types (see [layout.array]).
274278
- [Arrays] of length zero (see [layout.array]).
275279
- [Unions] of zero-sized types (see [items.union.common-storage]).
280+
- [`PhantomData<T>`] for any type `T` (see [lang-types.phantom-data]).
276281

277282
```rust
283+
# use core::marker::PhantomData;
278284
# use core::mem::{size_of, size_of_val};
279285
fn f() {}
280286
struct S(u8);
@@ -353,10 +359,16 @@ assert_eq!(0, size_of::<E4>());
353359
assert_eq!(0, size_of::<E5>());
354360
assert_eq!(0, size_of::<E6>());
355361
assert_eq!(0, size_of::<E7>());
362+
# // `PhantomData` for a ZST.
363+
assert_eq!(0, size_of::<PhantomData<()>>());
364+
# // `PhantomData` for a type that is not a ZST.
365+
assert_eq!(0, size_of::<PhantomData<u16>>());
366+
356367
```
357368

358369
[`extern` blocks]: items.extern
359370
[`extern fn`]: items.fn.extern
371+
[`PhantomData<T>`]: lang-types.phantom-data
360372
[alignment]: type-layout.md#size-and-alignment
361373
[arrays]: type.array
362374
[associated item]: #associated-item

‎src/special-types-and-traits.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ It also ensures that [`static` items] which have a type with interior mutability
5151
r[lang-types.phantom-data]
5252
## `PhantomData<T>`
5353

54-
[`std::marker::PhantomData<T>`] is a [zero-sized], minimum alignment, type that is considered to own a `T` for the purposes of [variance], [drop check], and [auto traits](#auto-traits).
54+
[`std::marker::PhantomData<T>`] is a [zero-sized], [1-aligned] type that is considered to own a `T` for the purposes of [variance], [drop check], and [auto traits](#auto-traits).
5555

5656
r[lang-types.va-list]
5757
## `VaList<'_>`
@@ -195,6 +195,7 @@ These implicit `Sized` bounds may be relaxed by using the special `?Sized` bound
195195
[`UnwindSafe`]: std::panic::UnwindSafe
196196
[`Unpin`]: std::marker::Unpin
197197

198+
[1-aligned]: layout.properties.align
198199
[Arrays]: types/array.md
199200
[associated types]: items/associated-items.md#associated-types
200201
[call expressions]: expressions/call-expr.md

0 commit comments

Comments
 (0)