@@ -383,6 +383,11 @@ pub fn formatType(
383383 }
384384
385385 const T = @TypeOf (value );
386+
387+ if (comptime std .meta .trait .hasFn ("format" )(T )) {
388+ return value .format (fmt , options , generator );
389+ }
390+
386391 switch (@typeInfo (T )) {
387392 .ComptimeInt , .Int , .Float = > {
388393 return formatValue (value , fmt , options , generator );
@@ -416,17 +421,11 @@ pub fn formatType(
416421 return ;
417422 },
418423 .Enum = > {
419- // if (comptime std.meta.trait.hasFn("format")(T)) {
420- // return value.format(fmt, options, context, Errors, output);
421- // }
422424 suspend generator .yield (@frame (), @typeName (T ) ++ "." );
423425 suspend generator .yield (@frame (), @tagName (value ));
424426 return ;
425427 },
426428 .Union = > | union_info | {
427- // if (comptime std.meta.trait.hasFn("format")(T)) {
428- // return value.format(fmt, options, context, Errors, output);
429- // }
430429 if (union_info .tag_type ) | UnionTagType | {
431430 suspend generator .yield (@frame (), @typeName (T ));
432431 if (max_depth == 0 ) {
@@ -447,9 +446,6 @@ pub fn formatType(
447446 }
448447 },
449448 .Struct = > | struct_info | {
450- // if (comptime std.meta.trait.hasFn("format")(T)) {
451- // return value.format(fmt, options, context, Errors, output);
452- // }
453449 suspend generator .yield (@frame (), @typeName (T ));
454450 if (max_depth == 0 ) {
455451 suspend generator .yield (@frame (), "{ ... }" );
@@ -1440,42 +1436,40 @@ test "float.libc.sanity" {
14401436 try testFmt ("f64: 18014400656965630.00000" , "f64: {d:.5}" , .{@as (f64 , @bitCast (f32 , @as (u32 , 1518338049 )))});
14411437}
14421438
1443- // test "custom" {
1444- // const Vec2 = struct {
1445- // const SelfType = @This();
1446- // x: f32,
1447- // y: f32,
1448-
1449- // pub fn format(
1450- // self: SelfType,
1451- // comptime fmt: []const u8,
1452- // options: FormatOptions,
1453- // context: var,
1454- // comptime Errors: type,
1455- // output: fn (@TypeOf(context), []const u8) Errors!void,
1456- // ) Errors!void {
1457- // if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "p")) {
1458- // return std.fmt.format(context, Errors, output, "({d:.3},{d:.3})", .{ self.x, self.y });
1459- // } else if (comptime std.mem.eql(u8, fmt, "d")) {
1460- // return std.fmt.format(context, Errors, output, "{d:.3}x{d:.3}", .{ self.x, self.y });
1461- // } else {
1462- // @compileError("Unknown format character: '" ++ fmt ++ "'");
1463- // }
1464- // }
1465- // };
1439+ test "custom" {
1440+ const Vec2 = struct {
1441+ const SelfType = @This ();
1442+ x : f32 ,
1443+ y : f32 ,
1444+
1445+ pub fn format (
1446+ self : SelfType ,
1447+ comptime fmt : []const u8 ,
1448+ options : FormatOptions ,
1449+ generator : * Generator ([]const u8 ),
1450+ ) void {
1451+ if (fmt .len == 0 or comptime std .mem .eql (u8 , fmt , "p" )) {
1452+ return std .fmtgen .format (generator , "({d:.3},{d:.3})" , .{ self .x , self .y });
1453+ } else if (comptime std .mem .eql (u8 , fmt , "d" )) {
1454+ return std .fmtgen .format (generator , "{d:.3}x{d:.3}" , .{ self .x , self .y });
1455+ } else {
1456+ @compileError ("Unknown format character: '" ++ fmt ++ "'" );
1457+ }
1458+ }
1459+ };
14661460
1467- // var buf1: [32]u8 = undefined;
1468- // var value = Vec2{
1469- // .x = 10.2,
1470- // .y = 2.22,
1471- // };
1472- // try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{&value});
1473- // try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{&value});
1461+ var buf1 : [32 ]u8 = undefined ;
1462+ var value = Vec2 {
1463+ .x = 10.2 ,
1464+ .y = 2.22 ,
1465+ };
1466+ try testFmt ("point: (10.200,2.220)\n " , "point: {}\n " , .{& value });
1467+ try testFmt ("dim: 10.200x2.220\n " , "dim: {d}\n " , .{& value });
14741468
1475- // // same thing but not passing a pointer
1476- // try testFmt("point: (10.200,2.220)\n", "point: {}\n", .{value});
1477- // try testFmt("dim: 10.200x2.220\n", "dim: {d}\n", .{value});
1478- // }
1469+ // same thing but not passing a pointer
1470+ try testFmt ("point: (10.200,2.220)\n " , "point: {}\n " , .{value });
1471+ try testFmt ("dim: 10.200x2.220\n " , "dim: {d}\n " , .{value });
1472+ }
14791473
14801474test "struct" {
14811475 const S = struct {
0 commit comments