| ... | @@ -377,6 +377,32 @@ test "Type.Enum" { | ... | @@ -377,6 +377,32 @@ test "Type.Enum" { |
| 377 | try testing.expectEqual(@as(u32, 1), @intFromEnum(Bar.a)); | 377 | try testing.expectEqual(@as(u32, 1), @intFromEnum(Bar.a)); |
| 378 | try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b)); | 378 | try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b)); |
| 379 | try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6)))); | 379 | try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6)))); |
| | 380 | |
| | 381 | { // from https://github.com/ziglang/zig/issues/19985 |
| | 382 | { // enum with single field can be initialized. |
| | 383 | const E = @Type(.{ .@"enum" = .{ |
| | 384 | .tag_type = u0, |
| | 385 | .is_exhaustive = true, |
| | 386 | .fields = &.{.{ .name = "foo", .value = 0 }}, |
| | 387 | .decls = &.{}, |
| | 388 | } }); |
| | 389 | const s: struct { E } = .{.foo}; |
| | 390 | try testing.expectEqual(.foo, s[0]); |
| | 391 | } |
| | 392 | |
| | 393 | { // meta.FieldEnum() with single field |
| | 394 | const S = struct { foo: u8 }; |
| | 395 | const Fe = std.meta.FieldEnum(S); |
| | 396 | var s: S = undefined; |
| | 397 | const fe = std.meta.stringToEnum(Fe, "foo") orelse return error.InvalidField; |
| | 398 | switch (fe) { |
| | 399 | inline else => |tag| { |
| | 400 | @field(s, @tagName(tag)) = 42; |
| | 401 | }, |
| | 402 | } |
| | 403 | try testing.expectEqual(42, s.foo); |
| | 404 | } |
| | 405 | } |
| 380 | } | 406 | } |
| 381 | | 407 | |
| 382 | test "Type.Union" { | 408 | test "Type.Union" { |