| ... | ... | @@ -34,3 +34,19 @@ fn testReinterpretBytesAsExternStruct() void { |
| 34 | 34 | var val = ptr.c; |
| 35 | 35 | assertOrPanic(val == 5); |
| 36 | 36 | } |
| 37 | |
| 38 | test "reinterpret struct field at comptime" { |
| 39 | const numLittle = comptime Bytes.init(0x12345678); |
| 40 | assertOrPanic(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes)); |
| 41 | } |
| 42 | |
| 43 | const Bytes = struct { |
| 44 | bytes: [4]u8, |
| 45 | |
| 46 | pub fn init(v: u32) Bytes { |
| 47 | var res: Bytes = undefined; |
| 48 | @ptrCast(*align(1) u32, &res.bytes).* = v; |
| 49 | |
| 50 | return res; |
| 51 | } |
| 52 | }; |