Skip to content

Should enum Foo { SingleVariant } be zero-size? #37649

@SimonSapin

Description

@SimonSapin

This prints 1 in rustc 1.14.0-nightly (cae6ab1 2016-11-05):

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

I’m guessing this one byte is the enum’s discriminant. But since there’s a single variant, that discriminant is useless. Could such a type be zero-size? For what it’s worth, an empty enum Bar {} is already zero-size.

This type could be written struct Foo; which is zero-size, but a single-variant enum could come up if it’s generated by a macro that can generate any number of variants depending on its arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions