authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2022-08-24 09:08:30+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-24 03:13:44-04:00
log5696cc8ab65370e3c8d63a7d26fd5346db9cc25f
treeb9d0e1aef4aaab99feed5ecfed2a816230526f34
parent5db80a051fea684e1d28fadb66f04932b090521c

Adds std.meta.FnPtr for easier stage1/stage2 compatibility


1 files changed, 24 insertions(+), 0 deletions(-)

lib/std/meta.zig+24
...@@ -1149,3 +1149,27 @@ test "isError" {...@@ -1149,3 +1149,27 @@ test "isError" {
1149 try std.testing.expect(isError(math.absInt(@as(i8, -128))));1149 try std.testing.expect(isError(math.absInt(@as(i8, -128))));
1150 try std.testing.expect(!isError(math.absInt(@as(i8, -127))));1150 try std.testing.expect(!isError(math.absInt(@as(i8, -127))));
1151}1151}
1152
1153/// This function returns a function pointer for a given function signature.
1154/// It's a helper to make code compatible to both stage1 and stage2.
1155///
1156/// **WARNING:** This function is deprecated and will be removed together with stage1.
1157pub fn FnPtr(comptime Fn: type) type {
1158 return if (@import("builtin").zig_backend != .stage1)
1159 *const Fn
1160 else
1161 Fn;
1162}
1163
1164test "FnPtr" {
1165 var func: FnPtr(fn () i64) = undefined;
1166
1167 // verify that we can perform runtime exchange
1168 // and not have a function body in stage2:
1169
1170 func = std.time.timestamp;
1171 _ = func();
1172
1173 func = std.time.milliTimestamp;
1174 _ = func();
1175}