Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
- [Function items] (see [type.fn-item.intro]).
- The constructors of [tuple-like structs] (see [type.fn-item.intro]).
- The constructors of [tuple-like enum variants] (see [type.fn-item.intro]).
- `repr(Rust)` [structs] with no fields or where all fields are zero sized (see [layout.repr.rust.struct-zst]).
- `repr(C)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.c.struct.size-field-offset]).
- `repr(transparent)` [structs] with no fields or where all fields are zero-sized (see [layout.repr.transparent.layout-abi]).
- `repr(Rust)` [enums] (without a [primitive representation] specified) with a single [field-struct-like variant], a single [unit-struct-like variant], or a single [tuple-struct-like variant] and where the struct-like thing has no fields or where all of the fields are zero sized (see [layout.repr.rust.enum-struct-like-zst]).
- [Arrays] of zero-sized types (see [layout.array]).
- [Arrays] of length zero (see [layout.array]).
- [Unions] of zero-sized types (see [items.union.common-storage]).
Expand All @@ -231,6 +233,13 @@ A type is zero sized (a ZST) if its size is 0. Such types have at most one possi
fn f() {}
struct S(u8);
enum E { V(u8) }
struct UnitLike;
struct NoFields {}
struct OnlyZST {
f1: (),
f2: [(); 10],
f3: [u8; 0],
}
#[repr(C)]
struct C1 {}
#[repr(C)]
Expand All @@ -253,17 +262,48 @@ union U {
f2: [(); 10],
f3: [u8; 0],
}
# /// An enum with a single field-struct-like variant with all fields
# /// being ZSTs.
enum E2 {
V1 { f1: (), f2: [(); 10] },
}
# /// An enum with a single field-struct-like variant with no fields.
enum E3 {
V1 {},
}
# /// An enum with a single unit-struct-like variant.
enum E4 {
V1,
}
# /// An enum with a single tuple-struct-like variant with all fields
# /// being ZSTs.
enum E5 {
V1 ((), [(); 10]),
}
# /// An enum with a single tuple-struct-like variant with no fields.
enum E6 {
V1 (),
}

assert_eq!(0, size_of::<()>());
assert_eq!(0, size_of_val(&f));
assert_eq!(0, size_of_val(&S));
assert_eq!(0, size_of_val(&E::V));
assert_eq!(0, size_of::<UnitLike>());
assert_eq!(0, size_of::<NoFields>());
assert_eq!(0, size_of::<OnlyZST>());
assert_eq!(0, size_of::<C1>());
assert_eq!(0, size_of::<C2>());
assert_eq!(0, size_of::<T1>());
assert_eq!(0, size_of::<T2>());
assert_eq!(0, size_of::<[(); 10]>());
assert_eq!(0, size_of::<[u8; 0]>());
assert_eq!(0, size_of::<U>());
assert_eq!(0, size_of::<E2>());
assert_eq!(0, size_of::<E3>());
assert_eq!(0, size_of::<E4>());
assert_eq!(0, size_of::<E5>());
assert_eq!(0, size_of::<E6>());
```

[`extern` blocks]: items.extern
Expand All @@ -276,6 +316,7 @@ assert_eq!(0, size_of::<U>());
[crate]: crates-and-source-files.md
[dyn compatibility]: items/traits.md#dyn-compatibility
[enums]: items/enumerations.md
[field-struct-like variant]: EnumVariantStruct
[fields]: expressions/field-expr.md
[free item]: #free-item
[function items]: type.fn-item
Expand All @@ -300,10 +341,12 @@ assert_eq!(0, size_of::<U>());
[never type]: types/never.md
[*path*]: paths.md
[Paths]: paths.md
[primitive representation]: layout.repr.primitive
[*scope*]: names/scopes.md
[structs]: items/structs.md
[tuple-like enum variants]: items.enum.constructor-namespace
[tuple-like structs]: items.struct.tuple
[tuple-struct-like variant]: EnumVariantTuple
[trait object types]: types/trait-object.md
[traits]: items/traits.md
[turbofish test]: https://github.com/rust-lang/rust/blob/1.58.0/src/test/ui/parser/bastion-of-the-turbofish.rs
Expand All @@ -312,5 +355,6 @@ assert_eq!(0, size_of::<U>());
[undefined-behavior]: behavior-considered-undefined.md
[unions]: items/unions.md
[unit type]: type.tuple.unit
[unit-struct-like variant]: EnumVariant
[variable bindings]: patterns.md
[visibility rules]: visibility-and-privacy.md
12 changes: 12 additions & 0 deletions src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ For [structs], it is further guaranteed that the fields do not overlap. That is,

Be aware that this guarantee does not imply that the fields have distinct addresses: [zero-sized types] may have the same address as other fields in the same struct.

r[layout.repr.rust.struct-zst]
For [structs] with no fields or where all fields are [zero sized], it is further guaranteed that the structs are themselves [zero sized].

r[layout.repr.rust.enum-struct-like-zst]
For [enums] (without a [primitive representation] specified) with a single [field-struct-like variant], a single [unit-struct-like variant], or a single [tuple-struct-like variant] and where the struct-like thing has no fields or where all of the fields are [zero sized], the enums themselves are [zero sized].

r[layout.repr.rust.unspecified]
There are no other guarantees of data layout made by this representation.

Expand Down Expand Up @@ -556,18 +562,24 @@ Because this representation delegates type layout to another type, it cannot be
[`Sized`]: std::marker::Sized
[`Copy`]: std::marker::Copy
[dynamically sized types]: dynamically-sized-types.md
[enums]: items/enumerations.md
[field-less enums]: items/enumerations.md#field-less-enum
[field-struct-like variant]: EnumVariantStruct
[fn-abi-compatibility]: ../core/primitive.fn.md#abi-compatibility
[enumerations]: items/enumerations.md
[zero-variant enums]: items/enumerations.md#zero-variant-enums
[undefined behavior]: behavior-considered-undefined.md
[zero sized]: glossary.zst
[zero-sized]: glossary.zst
[zero-sized type]: glossary.zst
[zero-sized types]: glossary.zst
[`PhantomData<T>`]: special-types-and-traits.md#phantomdatat
[`Rust`]: #the-rust-representation
[`C`]: #the-c-representation
[primitive representation]: #primitive-representations
[primitive representations]: #primitive-representations
[structs]: items/structs.md
[`transparent`]: #the-transparent-representation
[tuple-struct-like variant]: EnumVariantTuple
[unit-struct-like variant]: EnumVariant
[`Layout`]: std::alloc::Layout
Loading