aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-04-25 19:12:06 -0400
committerGitHub <noreply@github.com>2021-04-25 19:12:06 -0400
commit2fc6b347ec66650cd1702c63104fc45148658b15 (patch)
treefe102bc1693efb2d3254192ca4f0f10313ed6fd5 /lib/std
parent6f61594692af846226880e5903ad26a922014d55 (diff)
parent82f1d592fae021fcfc737e3cb6c107b325fcf1ee (diff)
downloadzig-2fc6b347ec66650cd1702c63104fc45148658b15.tar.gz
zig-2fc6b347ec66650cd1702c63104fc45148658b15.zip
Merge pull request #8616 from LemonBoy/fn-align
Function pointer alignment
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/meta.zig19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index dd6f500076..600f3d3c5d 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 {