Created
August 12, 2022 18:15
-
-
Save ityonemo/1d668f5dda2ffbbaacda6c7f6f67a544 to your computer and use it in GitHub Desktop.
Packed tagged union in zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const E = enum(u12) { | |
Int = 0xFFF, | |
String = 0xFFE, | |
Pointer = 0xFFD, | |
} | |
const P = packed union(?e){ | |
const this = @This(); | |
Int: packed struct { | |
number: u32, | |
_padding: u20, | |
@tag(this) : e, | |
}, | |
String: packed struct{ | |
string: [6]u8, | |
@tag(this) : e, | |
}, | |
Pointer: packed struct { | |
pointer: u48, | |
@tag(this) : e, | |
} | |
null: f64, | |
} | |
const std = @import("std"); | |
const expect = std.testing.expect; | |
fn tag_of(p: P) u12 {return @intCast(u12, (@bitCast(u64, p) | 0xFFF0_0000_0000_0000) >> 52)} | |
test "NaN-boxed float" { | |
var with_int = P{.Int = .{number = 1234}}; | |
var with_string = P{.String = .{string = "foo"}}; | |
var as_float = P{.null = 1.234}; | |
try expect(with_int.Int.number == 1234); | |
try expectEqual(with_string.String.string, "foo"); | |
try expect(as_float.null == 1.234); | |
try expect(tag_of(with_int) == 0xFFF); | |
try expect(tag_of(with_string) == 0xFFE) | |
} |
Author
ityonemo
commented
Aug 12, 2022
•
- tag location is always comptime checked. It must be in the same place as all non-null tags.
- might be possible to infer the location of the tag (first available high order locations - this would make "padding" unnecessary for "int")
- it is considered safety-checked ub if the null enum writes an enumerated value into the respective bits of the tag location
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment