diff options
| author | LemonBoy <thatlemon@gmail.com> | 2021-04-25 16:49:21 +0200 |
|---|---|---|
| committer | LemonBoy <thatlemon@gmail.com> | 2021-04-25 16:49:21 +0200 |
| commit | 19cec0db1e8a9fefd8295c81edb03d631c624e62 (patch) | |
| tree | fa82d3cf52aca9481616b127587776904d5a4c6c /lib/std/meta.zig | |
| parent | 50a8124f45cd0994b52af3fd166dd3bda48f4b99 (diff) | |
| download | zig-19cec0db1e8a9fefd8295c81edb03d631c624e62.tar.gz zig-19cec0db1e8a9fefd8295c81edb03d631c624e62.zip | |
std: Make met.alignment work on more types
Make it return the correct value for function types and optional
pointers.
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index c6f800ca2c..9bb02013e6 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -117,10 +117,21 @@ test "std.meta.bitCount" { testing.expect(bitCount(f32) == 32); } +/// Returns the alignment of type T. +/// Note that if T is a pointer or function type the result is different than +/// the one returned by @alignOf(T). +/// If T is a pointer type the alignment of the type it points to is returned. +/// If T is a function type the alignment a target-dependent value is returned. pub fn alignment(comptime T: type) comptime_int { - //@alignOf works on non-pointer types - const P = if (comptime trait.is(.Pointer)(T)) T else *T; - return @typeInfo(P).Pointer.alignment; + return switch (@typeInfo(T)) { + .Optional => |info| switch (@typeInfo(info.child)) { + .Pointer, .Fn => alignment(info.child), + else => @alignOf(T), + }, + .Pointer => |info| info.alignment, + .Fn => |info| info.alignment, + else => @alignOf(T), + }; } test "std.meta.alignment" { @@ -129,6 +140,8 @@ test "std.meta.alignment" { testing.expect(alignment(*align(2) u8) == 2); testing.expect(alignment([]align(1) u8) == 1); testing.expect(alignment([]align(2) u8) == 2); + testing.expect(alignment(fn () void) > 0); + testing.expect(alignment(fn () align(128) void) == 128); } pub fn Child(comptime T: type) type { |
