aboutsummaryrefslogtreecommitdiff
path: root/lib/std/meta.zig
diff options
context:
space:
mode:
authorFelix "xq" Queißner <xq@random-projects.net>2022-08-24 09:08:30 +0200
committerAndrew Kelley <andrew@ziglang.org>2022-08-24 03:13:44 -0400
commit5696cc8ab65370e3c8d63a7d26fd5346db9cc25f (patch)
treeb9d0e1aef4aaab99feed5ecfed2a816230526f34 /lib/std/meta.zig
parent5db80a051fea684e1d28fadb66f04932b090521c (diff)
downloadzig-5696cc8ab65370e3c8d63a7d26fd5346db9cc25f.tar.gz
zig-5696cc8ab65370e3c8d63a7d26fd5346db9cc25f.zip
Adds std.meta.FnPtr for easier stage1/stage2 compatibility
Diffstat (limited to 'lib/std/meta.zig')
-rw-r--r--lib/std/meta.zig24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index d5006bf81f..937b1998ff 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -1149,3 +1149,27 @@ test "isError" {
try std.testing.expect(isError(math.absInt(@as(i8, -128))));
try std.testing.expect(!isError(math.absInt(@as(i8, -127))));
}
+
+/// This function returns a function pointer for a given function signature.
+/// It's a helper to make code compatible to both stage1 and stage2.
+///
+/// **WARNING:** This function is deprecated and will be removed together with stage1.
+pub fn FnPtr(comptime Fn: type) type {
+ return if (@import("builtin").zig_backend != .stage1)
+ *const Fn
+ else
+ Fn;
+}
+
+test "FnPtr" {
+ var func: FnPtr(fn () i64) = undefined;
+
+ // verify that we can perform runtime exchange
+ // and not have a function body in stage2:
+
+ func = std.time.timestamp;
+ _ = func();
+
+ func = std.time.milliTimestamp;
+ _ = func();
+}