aboutsummaryrefslogtreecommitdiff
path: root/lib/std/meta.zig
diff options
context:
space:
mode:
authorKhang Nguyen Duy <40488299+iceghost@users.noreply.github.com>2023-07-09 22:18:52 +0700
committerAndrew Kelley <andrew@ziglang.org>2024-01-23 01:03:45 -0800
commit993a83081a975464d1201597cf6f4cb7f6735284 (patch)
tree08c07b7ad6c2d8b8e24d8e1ac2fe4ba1603b31ed /lib/std/meta.zig
parentaef1da1634ae12d90e9d59fc0184dadaea38830c (diff)
downloadzig-993a83081a975464d1201597cf6f4cb7f6735284.tar.gz
zig-993a83081a975464d1201597cf6f4cb7f6735284.zip
std.fmt: fix unecessary deref on user-defined format function
When formatting a pointer to user type, currently it needs to be dereferenced first, then call `formatType` on the child type. Fix the problem by checking for "format" function on not only the type itself, but also the struct it points to. Add hasMethod to std.meta.
Diffstat (limited to 'lib/std/meta.zig')
-rw-r--r--lib/std/meta.zig9
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 94d5f962bc..f77ed6d344 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -1129,6 +1129,15 @@ pub inline fn hasFn(comptime T: type, comptime name: []const u8) bool {
return @typeInfo(@TypeOf(@field(T, name))) == .Fn;
}
+/// Returns true if a type has a `name` method; `false` otherwise.
+/// Result is always comptime-known.
+pub inline fn hasMethod(comptime T: type, comptime name: []const u8) bool {
+ return switch (@typeInfo(T)) {
+ .Pointer => |P| hasFn(P.child, name),
+ else => hasFn(T, name),
+ };
+}
+
/// True if every value of the type `T` has a unique bit pattern representing it.
/// In other words, `T` has no unused bits and no padding.
/// Result is always comptime-known.