authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-02-29 15:24:03-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-12 10:26:28-05:00
log7364e965f473147a86cc62ccf47feac4878a15de
tree84348cc838c739b5c474c445e478e616566e0661
parentd2e4aafd64184017dc1f152a4b46eeafca94bbd8

Force error coercion of custom formatters


1 files changed, 4 insertions(+), 12 deletions(-)

lib/std/fmtstream.zig+4-12
......@@ -325,6 +325,10 @@ pub fn formatType(
325325 }
326326
327327 const T = @TypeOf(value);
328 if (comptime std.meta.trait.hasFn("format")(T)) {
329 return try value.format(fmt, options, out_stream);
330 }
331
328332 switch (@typeInfo(T)) {
329333 .ComptimeInt, .Int, .Float => {
330334 return formatValue(value, fmt, options, out_stream);
......@@ -354,10 +358,6 @@ pub fn formatType(
354358 return out_stream.writeAll(@errorName(value));
355359 },
356360 .Enum => |enumInfo| {
357 if (comptime std.meta.trait.hasFn("format")(T)) {
358 return value.format(fmt, options, out_stream);
359 }
360
361361 try out_stream.writeAll(@typeName(T));
362362 if (enumInfo.is_exhaustive) {
363363 try out_stream.writeAll(".");
......@@ -370,10 +370,6 @@ pub fn formatType(
370370 }
371371 },
372372 .Union => {
373 if (comptime std.meta.trait.hasFn("format")(T)) {
374 return value.format(fmt, options, out_stream);
375 }
376
377373 try out_stream.writeAll(@typeName(T));
378374 if (max_depth == 0) {
379375 return out_stream.writeAll("{ ... }");
......@@ -394,10 +390,6 @@ pub fn formatType(
394390 }
395391 },
396392 .Struct => |StructT| {
397 if (comptime std.meta.trait.hasFn("format")(T)) {
398 return value.format(fmt, options, out_stream);
399 }
400
401393 try out_stream.writeAll(@typeName(T));
402394 if (max_depth == 0) {
403395 return out_stream.writeAll("{ ... }");