{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreidzddxhxihawvm7xvfyw4are2d4ltzl5hmd7pbwadtjp6yyk2qagu",
    "uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mnqbp7lta332"
  },
  "path": "/t/for-structs-that-have-fields-that-have-only-one-possible-representation-enums-should-use-that-field-as-the-discriminant/24382#post_1",
  "publishedAt": "2026-06-07T22:53:09.000Z",
  "site": "https://internals.rust-lang.org",
  "textContent": "\n    #[repr(u8)]\n    #[derive(Clone, Copy)]\n    enum FooDiscriminant {\n        Disc = 0x01,\n    }\n\n    #[repr(u8)]\n    #[derive(Clone, Copy)]\n    enum BarDiscriminant {\n        Disc = 0x02,\n    }\n\n    #[repr(C)]\n    struct Foo {\n        x: u8,\n        y: u8,\n        z: u8,\n        disc: FooDiscriminant,\n    }\n\n    #[repr(C)]\n    struct Bar {\n        x: u8,\n        y: u8,\n        z: u8,\n        disc: BarDiscriminant,\n    }\n\n    enum FooBar {\n        Foo(Foo),\n        Bar(Bar),\n    }\n\n\nIn this example, the disc field of both the Foo and Bar types are at the same offset. Both types have the same size and alignment. But contrary to what you might imagine the compiler might optimize this code into, it turns out that the size of FooBar is 5 instead of 4. But the compiler could easily see through the structure of these inner types and choose to use those fields for the discriminants, and as a result the size of FooBar would be 4.",
  "title": "For structs that have fields that have only one possible representation, enums should use that field as the discriminant"
}