commit 7364e965f473147a86cc62ccf47feac4878a15de (tree)
parent d2e4aafd64184017dc1f152a4b46eeafca94bbd8
Author: Benjamin Feng <benjamin.feng@glassdoor.com>
Date: Sat, 29 Feb 2020 15:24:03 -0600
Force error coercion of custom formatters
Diffstat:
1 file changed, 4 insertions(+), 12 deletions(-)
diff --git a/lib/std/fmtstream.zig b/lib/std/fmtstream.zig
@@ -325,6 +325,10 @@ pub fn formatType(
}
const T = @TypeOf(value);
+ if (comptime std.meta.trait.hasFn("format")(T)) {
+ return try value.format(fmt, options, out_stream);
+ }
+
switch (@typeInfo(T)) {
.ComptimeInt, .Int, .Float => {
return formatValue(value, fmt, options, out_stream);
@@ -354,10 +358,6 @@ pub fn formatType(
return out_stream.writeAll(@errorName(value));
},
.Enum => |enumInfo| {
- if (comptime std.meta.trait.hasFn("format")(T)) {
- return value.format(fmt, options, out_stream);
- }
-
try out_stream.writeAll(@typeName(T));
if (enumInfo.is_exhaustive) {
try out_stream.writeAll(".");
@@ -370,10 +370,6 @@ pub fn formatType(
}
},
.Union => {
- if (comptime std.meta.trait.hasFn("format")(T)) {
- return value.format(fmt, options, out_stream);
- }
-
try out_stream.writeAll(@typeName(T));
if (max_depth == 0) {
return out_stream.writeAll("{ ... }");
@@ -394,10 +390,6 @@ pub fn formatType(
}
},
.Struct => |StructT| {
- if (comptime std.meta.trait.hasFn("format")(T)) {
- return value.format(fmt, options, out_stream);
- }
-
try out_stream.writeAll(@typeName(T));
if (max_depth == 0) {
return out_stream.writeAll("{ ... }");