| author | |
| committer | |
| log | 8dd737801355427c558876bbcb299926d47b8269 |
| tree | b54aa4604b6b164b56483efcfbcd4c2bd0ebb4ce |
| parent | 329a359974c29d62931c32ca85aa42aa9e4847f7 |
No need for any such thing. Instead, provide an integer tag type for the
enum.2 files changed, 7 insertions(+), 26 deletions(-)
doc/langref.html.in+1-20| ... | ... | @@ -2588,7 +2588,7 @@ test "default struct initialization fields" { |
| 2588 | 2588 | exactly their bit width. |
| 2589 | 2589 | </li> |
| 2590 | 2590 | <li>{#syntax#}bool{#endsyntax#} fields use exactly 1 bit.</li> |
| 2591 | <li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li> | |
| 2591 | <li>An {#link|enum#} field uses exactly the bit width of its integer tag type.</li> | |
| 2592 | 2592 | <li>A {#link|packed union#} field uses exactly the bit width of the union field with |
| 2593 | 2593 | the largest bit width.</li> |
| 2594 | 2594 | <li>Non-ABI-aligned fields are packed into the smallest possible |
| ... | ... | @@ -2983,25 +2983,6 @@ export fn entry(foo: Foo) void { } |
| 2983 | 2983 | {#code_end#} |
| 2984 | 2984 | {#header_close#} |
| 2985 | 2985 | |
| 2986 | {#header_open|packed enum#} | |
| 2987 | <p>By default, the size of enums is not guaranteed.</p> | |
| 2988 | <p>{#syntax#}packed enum{#endsyntax#} causes the size of the enum to be the same as the size of the | |
| 2989 | integer tag type of the enum:</p> | |
| 2990 | {#code_begin|test#} | |
| 2991 | const std = @import("std"); | |
| 2992 | ||
| 2993 | test "packed enum" { | |
| 2994 | const Number = packed enum(u8) { | |
| 2995 | one, | |
| 2996 | two, | |
| 2997 | three, | |
| 2998 | }; | |
| 2999 | std.testing.expect(@sizeOf(Number) == @sizeOf(u8)); | |
| 3000 | } | |
| 3001 | {#code_end#} | |
| 3002 | <p>This makes the enum eligible to be in a {#link|packed struct#}.</p> | |
| 3003 | {#header_close#} | |
| 3004 | ||
| 3005 | 2986 | {#header_open|Enum Literals#} |
| 3006 | 2987 | <p> |
| 3007 | 2988 | Enum literals allow specifying the name of an enum field without specifying the enum type: |
lib/std/os/linux/bpf.zig+6-6| ... | ... | @@ -398,10 +398,10 @@ pub const Insn = packed struct { |
| 398 | 398 | |
| 399 | 399 | /// r0 - r9 are general purpose 64-bit registers, r10 points to the stack |
| 400 | 400 | /// frame |
| 401 | pub const Reg = packed enum(u4) { r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10 }; | |
| 402 | const Source = packed enum(u1) { reg, imm }; | |
| 401 | pub const Reg = enum(u4) { r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10 }; | |
| 402 | const Source = enum(u1) { reg, imm }; | |
| 403 | 403 | |
| 404 | const Mode = packed enum(u8) { | |
| 404 | const Mode = enum(u8) { | |
| 405 | 405 | imm = IMM, |
| 406 | 406 | abs = ABS, |
| 407 | 407 | ind = IND, |
| ... | ... | @@ -410,7 +410,7 @@ pub const Insn = packed struct { |
| 410 | 410 | msh = MSH, |
| 411 | 411 | }; |
| 412 | 412 | |
| 413 | const AluOp = packed enum(u8) { | |
| 413 | const AluOp = enum(u8) { | |
| 414 | 414 | add = ADD, |
| 415 | 415 | sub = SUB, |
| 416 | 416 | mul = MUL, |
| ... | ... | @@ -426,14 +426,14 @@ pub const Insn = packed struct { |
| 426 | 426 | arsh = ARSH, |
| 427 | 427 | }; |
| 428 | 428 | |
| 429 | pub const Size = packed enum(u8) { | |
| 429 | pub const Size = enum(u8) { | |
| 430 | 430 | byte = B, |
| 431 | 431 | half_word = H, |
| 432 | 432 | word = W, |
| 433 | 433 | double_word = DW, |
| 434 | 434 | }; |
| 435 | 435 | |
| 436 | const JmpOp = packed enum(u8) { | |
| 436 | const JmpOp = enum(u8) { | |
| 437 | 437 | ja = JA, |
| 438 | 438 | jeq = JEQ, |
| 439 | 439 | jgt = JGT, |