Skip to content

Commit a43bc10

Browse files
feat(anytype): make format working
1 parent 15e0e5b commit a43bc10

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

any+/anytype.zig

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const std = @import("std");
22
const Self = @This();
33

44
// TODO: use any-writer when it exists
5-
const PointerFormat = *const fn (*Self, options: std.fmt.FormatOptions, *std.io.FixedBufferStream([]u8)) anyerror!void;
5+
const PointerFormat = *const fn (*const Self, options: std.fmt.FormatOptions, *std.io.FixedBufferStream([]u8)) error{ NoSpaceLeft, InvalidCast }!void;
66

77
type: []const u8,
88
size: usize = 0,
@@ -18,8 +18,8 @@ pub inline fn init(value: anytype) Self {
1818
pub inline fn initExplicit(comptime T: type, value: T) Self {
1919
var size: usize = @sizeOf(T);
2020
var ptrFormat: PointerFormat = (struct {
21-
fn func(t: *Self, options: std.fmt.FormatOptions, stream: *std.io.FixedBufferStream([]u8)) !void {
22-
const self: T = try t.cast(T);
21+
fn func(t: *const Self, options: std.fmt.FormatOptions, stream: *std.io.FixedBufferStream([]u8)) !void {
22+
const self: T = t.cast(T) catch return error.NoSpaceLeft;
2323
return std.fmt.formatType(self, "", options, stream.writer(), 3);
2424
}
2525
}).func;
@@ -30,8 +30,8 @@ pub inline fn initExplicit(comptime T: type, value: T) Self {
3030
.Enum => @ptrFromInt(@intFromEnum(value)),
3131
.Struct, .Union => blk: {
3232
ptrFormat = (struct {
33-
fn func(t: *Self, options: std.fmt.FormatOptions, stream: *std.io.FixedBufferStream([]u8)) !void {
34-
const self: T = try t.cast(T);
33+
fn func(t: *const Self, options: std.fmt.FormatOptions, stream: *std.io.FixedBufferStream([]u8)) !void {
34+
const self: T = t.cast(T) catch return error.NoSpaceLeft;
3535
return if (@hasDecl(T, "format")) self.format("", options, stream.writer()) else std.fmt.formatType(self, "", options, stream.writer(), 3);
3636
}
3737
}).func;
@@ -90,31 +90,24 @@ pub inline fn len(self: Self, comptime T: type) usize {
9090
return @divExact(self.size, size);
9191
}
9292

93-
pub inline fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
94-
if (self.ptrFormat) |ptrFormat| {
95-
const size = comptime if (std.mem.indexOf(u8, fmt, "%")) |sizeStart| std.fmt.parseInt(comptime_int, fmt[sizeStart..]) else 0x1000;
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;
9695

97-
const trunc_msg = "(msg truncated)";
98-
var buf: [size + trunc_msg.len]u8 = undefined;
96+
const trunc_msg = "(msg truncated)";
97+
var buf: [size + trunc_msg.len]u8 = undefined;
98+
@memset(&buf, 0);
9999

100-
var stream = std.io.fixedBufferStream(buf[0..size]);
101-
ptrFormat(self, options, &stream) catch |err| switch (err) {
102-
error.NoSpaceLeft => blk: {
103-
@memcpy(buf[size..], trunc_msg);
104-
break :blk &buf;
105-
},
106-
else => return err,
107-
};
100+
var stream = std.io.fixedBufferStream(buf[0..size]);
101+
const result = self.ptrFormat(self, options, &stream);
108102

109-
try writer.writeAll(buf);
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});
110108
} else {
111-
try writer.writeAll(@typeName(Self));
112-
try writer.print("{{ .size = {}, .len = {}, .type = \"{s}\", .ptr = {*} }}", .{
113-
self.size,
114-
self.len(),
115-
self.type,
116-
self.ptr,
117-
});
109+
const end = std.mem.indexOf(u8, &buf, &[_]u8{0}) orelse buf.len;
110+
try writer.writeAll(buf[0..end]);
118111
}
119112
}
120113

@@ -162,3 +155,8 @@ test "Invalid casts" {
162155
try std.testing.expectError(error.InvalidCast, initExplicit(f32, 123.456).cast(u8));
163156
try std.testing.expectError(error.InvalidCast, initExplicit(f32, 123.456).cast(f128));
164157
}
158+
159+
test "Anytype formatting" {
160+
try std.testing.expectFmt("255", "{}", .{initExplicit(u8, 255)});
161+
try std.testing.expectFmt("123.456", "{}", .{initExplicit(f32, 123.456)});
162+
}

0 commit comments

Comments
 (0)