Skip to content

No niche optimization for enum {One(Enum), Two(Enum, Enum)} #93739

Closed
@lyphyser

Description

@lyphyser

Rust seems to fail to take advantage of niches for very simple cases where it seems like it would be trivial to do so.

#[repr(u8)]
pub enum Bar {A = 0, B = 1}

enum Foo {
    Two(Bar, Bar),
    One(Bar)
}

fn main() {
    println!("{}", std::mem::size_of::<Foo>());
}

This code gives a size of 3 for Foo while obviously it can be stored in 2 bytes, representing the Two variant as two consecutive byte-sized Bar values, and the One variant as a byte with any value other than 0 or 1 followed by a byte-size Bar value.

Using an array of two values for the Two variant, specifying non-zero discriminants for Bar and and using NonZeroU8 all seem to have no effect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions