From 97702988d1783db6840c642809a41d8f176bb500 Mon Sep 17 00:00:00 2001 From: Jimmi HC Date: Sun, 6 Jan 2019 17:58:00 +0100 Subject: Added test case --- test/cases/ptrcast.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/cases/ptrcast.zig b/test/cases/ptrcast.zig index 6f0e6e5946..54c3dda849 100644 --- a/test/cases/ptrcast.zig +++ b/test/cases/ptrcast.zig @@ -34,3 +34,19 @@ fn testReinterpretBytesAsExternStruct() void { var val = ptr.c; assertOrPanic(val == 5); } + +test "reinterpret struct field at comptime" { + const numLittle = comptime Bytes.init(0x12345678); + assertOrPanic(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes)); +} + +const Bytes = struct { + bytes: [4]u8, + + pub fn init(v: u32) Bytes { + var res: Bytes = undefined; + @ptrCast(*align(1) u32, &res.bytes).* = v; + + return res; + } +}; -- cgit v1.2.3