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(...@@ -325,6 +325,10 @@ pub fn formatType(
325 }325 }
326326
327 const T = @TypeOf(value);327 const T = @TypeOf(value);
328 if (comptime std.meta.trait.hasFn("format")(T)) {
329 return try value.format(fmt, options, out_stream);
330 }
331
328 switch (@typeInfo(T)) {332 switch (@typeInfo(T)) {
329 .ComptimeInt, .Int, .Float => {333 .ComptimeInt, .Int, .Float => {
330 return formatValue(value, fmt, options, out_stream);334 return formatValue(value, fmt, options, out_stream);
...@@ -354,10 +358,6 @@ pub fn formatType(...@@ -354,10 +358,6 @@ pub fn formatType(
354 return out_stream.writeAll(@errorName(value));358 return out_stream.writeAll(@errorName(value));
355 },359 },
356 .Enum => |enumInfo| {360 .Enum => |enumInfo| {
357 if (comptime std.meta.trait.hasFn("format")(T)) {
358 return value.format(fmt, options, out_stream);
359 }
360
361 try out_stream.writeAll(@typeName(T));361 try out_stream.writeAll(@typeName(T));
362 if (enumInfo.is_exhaustive) {362 if (enumInfo.is_exhaustive) {
363 try out_stream.writeAll(".");363 try out_stream.writeAll(".");
...@@ -370,10 +370,6 @@ pub fn formatType(...@@ -370,10 +370,6 @@ pub fn formatType(
370 }370 }
371 },371 },
372 .Union => {372 .Union => {
373 if (comptime std.meta.trait.hasFn("format")(T)) {
374 return value.format(fmt, options, out_stream);
375 }
376
377 try out_stream.writeAll(@typeName(T));373 try out_stream.writeAll(@typeName(T));
378 if (max_depth == 0) {374 if (max_depth == 0) {
379 return out_stream.writeAll("{ ... }");375 return out_stream.writeAll("{ ... }");
...@@ -394,10 +390,6 @@ pub fn formatType(...@@ -394,10 +390,6 @@ pub fn formatType(
394 }390 }
395 },391 },
396 .Struct => |StructT| {392 .Struct => |StructT| {
397 if (comptime std.meta.trait.hasFn("format")(T)) {
398 return value.format(fmt, options, out_stream);
399 }
400
401 try out_stream.writeAll(@typeName(T));393 try out_stream.writeAll(@typeName(T));
402 if (max_depth == 0) {394 if (max_depth == 0) {
403 return out_stream.writeAll("{ ... }");395 return out_stream.writeAll("{ ... }");