11const std = @import ("std" );
22const Self = @This ();
3+ const Anywriter = @import ("anywriter.zig" );
34
45// TODO: use any-writer when it exists
5- const PointerFormat = * const fn (* const Self , options : std.fmt.FormatOptions , * std.io.FixedBufferStream ([] u8 )) error { NoSpaceLeft , InvalidCast } ! void ;
6+ const PointerFormat = * const fn (* const Self , options : std.fmt.FormatOptions , Anywriter.Writer ) anyerror ! void ;
67
78type : []const u8 ,
89size : usize = 0 ,
@@ -18,9 +19,9 @@ pub inline fn init(value: anytype) Self {
1819pub inline fn initExplicit (comptime T : type , value : T ) Self {
1920 var size : usize = @sizeOf (T );
2021 var ptrFormat : PointerFormat = (struct {
21- fn func (t : * const Self , options : std.fmt.FormatOptions , stream : * std . io . FixedBufferStream ([] u8 ) ) ! void {
22+ fn func (t : * const Self , options : std.fmt.FormatOptions , writer : Anywriter.Writer ) ! void {
2223 const self : T = t .cast (T ) catch return error .NoSpaceLeft ;
23- return std .fmt .formatType (self , "" , options , stream . writer () , 3 );
24+ return std .fmt .formatType (self , "" , options , writer , 3 );
2425 }
2526 }).func ;
2627
@@ -30,9 +31,9 @@ pub inline fn initExplicit(comptime T: type, value: T) Self {
3031 .Enum = > @ptrFromInt (@intFromEnum (value )),
3132 .Struct , .Union = > blk : {
3233 ptrFormat = (struct {
33- fn func (t : * const Self , options : std.fmt.FormatOptions , stream : * std . io . FixedBufferStream ([] u8 ) ) ! void {
34+ fn func (t : * const Self , options : std.fmt.FormatOptions , writer : Anywriter.Writer ) ! void {
3435 const self : T = t .cast (T ) catch return error .NoSpaceLeft ;
35- return if (@hasDecl (T , "format" )) self .format ("" , options , stream . writer ()) else std .fmt .formatType (self , "" , options , stream . writer () , 3 );
36+ return if (@hasDecl (T , "format" )) self .format ("" , options , writer ) else std .fmt .formatType (self , "" , options , writer , 3 );
3637 }
3738 }).func ;
3839 break :blk @constCast (& value );
@@ -90,25 +91,8 @@ pub inline fn len(self: Self, comptime T: type) usize {
9091 return @divExact (self .size , size );
9192}
9293
93- pub inline fn format (self : * const Self , comptime fmt : []const u8 , options : std.fmt.FormatOptions , writer : anytype ) ! void {
94- const size = comptime if (std .mem .indexOf (u8 , fmt , "%" )) | sizeStart | std .fmt .parseInt (comptime_int , fmt [sizeStart .. ]) else 0x1000 ;
95-
96- const trunc_msg = "(msg truncated)" ;
97- var buf : [size + trunc_msg .len ]u8 = undefined ;
98- @memset (& buf , 0 );
99-
100- var stream = std .io .fixedBufferStream (buf [0.. size ]);
101- const result = self .ptrFormat (self , options , & stream );
102-
103- if (result == error .NoSpaceLeft ) {
104- @memcpy (buf [size .. ], trunc_msg );
105- try writer .writeAll (& buf );
106- } else if (result == error .InvalidCast ) {
107- std .debug .panic ("Failed to cast {s}" , .{self .type });
108- } else {
109- const end = std .mem .indexOf (u8 , & buf , &[_ ]u8 {0 }) orelse buf .len ;
110- try writer .writeAll (buf [0.. end ]);
111- }
94+ pub inline fn format (self : * const Self , comptime _ : []const u8 , options : std.fmt.FormatOptions , writer : anytype ) ! void {
95+ return self .ptrFormat (self , options , Anywriter .init (writer ).writer ()) catch | err | std .debug .panic ("Anywriter failed: {s}" , .{@errorName (err )});
11296}
11397
11498test "Casting integers and floats" {
0 commit comments