| ... | ... | @@ -117,10 +117,21 @@ test "std.meta.bitCount" { |
| 117 | 117 | testing.expect(bitCount(f32) == 32); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | /// Returns the alignment of type T. |
| 121 | /// Note that if T is a pointer or function type the result is different than |
| 122 | /// the one returned by @alignOf(T). |
| 123 | /// If T is a pointer type the alignment of the type it points to is returned. |
| 124 | /// If T is a function type the alignment a target-dependent value is returned. |
| 120 | 125 | pub fn alignment(comptime T: type) comptime_int { |
| 121 | | //@alignOf works on non-pointer types |
| 122 | | const P = if (comptime trait.is(.Pointer)(T)) T else *T; |
| 123 | | return @typeInfo(P).Pointer.alignment; |
| 126 | return switch (@typeInfo(T)) { |
| 127 | .Optional => |info| switch (@typeInfo(info.child)) { |
| 128 | .Pointer, .Fn => alignment(info.child), |
| 129 | else => @alignOf(T), |
| 130 | }, |
| 131 | .Pointer => |info| info.alignment, |
| 132 | .Fn => |info| info.alignment, |
| 133 | else => @alignOf(T), |
| 134 | }; |
| 124 | 135 | } |
| 125 | 136 | |
| 126 | 137 | test "std.meta.alignment" { |
| ... | ... | @@ -129,6 +140,8 @@ test "std.meta.alignment" { |
| 129 | 140 | testing.expect(alignment(*align(2) u8) == 2); |
| 130 | 141 | testing.expect(alignment([]align(1) u8) == 1); |
| 131 | 142 | testing.expect(alignment([]align(2) u8) == 2); |
| 143 | testing.expect(alignment(fn () void) > 0); |
| 144 | testing.expect(alignment(fn () align(128) void) == 128); |
| 132 | 145 | } |
| 133 | 146 | |
| 134 | 147 | pub fn Child(comptime T: type) type { |