Skip to content

Commit

Permalink
document test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdesjardins committed Jul 17, 2020
1 parent 2e431c6 commit 3924672
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/test/ui/type-sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ enum Option2<A, B> {
None
}

// Two layouts are considered for `CanBeNicheFilledButShouldnt`:
// Niche-filling:
// { u32 (4 bytes), NonZeroU8 + tag in niche (1 byte), padding (3 bytes) }
// Tagged:
// { tag (1 byte), NonZeroU8 (1 byte), padding (2 bytes), u32 (4 bytes) }
// Both are the same size (due to padding),
// but the tagged layout is better as the tag creates a niche with 254 invalid values,
// allowing types like `Option<Option<CanBeNicheFilledButShouldnt>>` to fit into 8 bytes.
pub enum CanBeNicheFilledButShouldnt {
A(NonZeroU8, u32),
B
}
pub enum AlwaysTagged {
pub enum AlwaysTaggedBecauseItHasNoNiche {
A(u8, u32),
B
}
Expand Down Expand Up @@ -159,7 +167,7 @@ pub fn main() {
assert_eq!(size_of::<CanBeNicheFilledButShouldnt>(), 8);
assert_eq!(size_of::<Option<CanBeNicheFilledButShouldnt>>(), 8);
assert_eq!(size_of::<Option<Option<CanBeNicheFilledButShouldnt>>>(), 8);
assert_eq!(size_of::<AlwaysTagged>(), 8);
assert_eq!(size_of::<Option<AlwaysTagged>>(), 8);
assert_eq!(size_of::<Option<Option<AlwaysTagged>>>(), 8);
assert_eq!(size_of::<AlwaysTaggedBecauseItHasNoNiche>(), 8);
assert_eq!(size_of::<Option<AlwaysTaggedBecauseItHasNoNiche>>(), 8);
assert_eq!(size_of::<Option<Option<AlwaysTaggedBecauseItHasNoNiche>>>(), 8);
}

0 comments on commit 3924672

Please sign in to comment.