authorgravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-02-07 22:18:48+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-07 16:18:48-05:00
log79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5
tree03aee28d441bdef135727d65615dcc7210a82c6d
parent0090c2d70b89cc6b5ab02bdc02aa49c4caf71a5a

format struct pointers as "<typename>@<address>" (#752)


1 files changed, 15 insertions(+), 1 deletions(-)

std/fmt/index.zig+15-1
......@@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons
228228 if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) {
229229 return output(context, (*value)[0..]);
230230 } else {
231 @compileError("Unable to format type '" ++ @typeName(T) ++ "'");
231 return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
232232 }
233233 },
234234 else => if (@canImplicitCast([]const u8, value)) {
......@@ -546,6 +546,12 @@ test "parse unsigned comptime" {
546546 }
547547}
548548
549// Dummy field because of https://github.com/zig-lang/zig/issues/557.
550// At top level because of https://github.com/zig-lang/zig/issues/675.
551const Struct = struct {
552 unused: u8,
553};
554
549555test "fmt.format" {
550556 {
551557 var buf1: [32]u8 = undefined;
......@@ -577,6 +583,14 @@ test "fmt.format" {
577583 const result = try bufPrint(buf1[0..], "u3: {}\n", value);
578584 assert(mem.eql(u8, result, "u3: 5\n"));
579585 }
586 {
587 var buf1: [32]u8 = undefined;
588 const value = Struct {
589 .unused = 42,
590 };
591 const result = try bufPrint(buf1[0..], "pointer: {}\n", &value);
592 assert(mem.startsWith(u8, result, "pointer: Struct@"));
593 }
580594
581595 // TODO get these tests passing in release modes
582596 // https://github.com/zig-lang/zig/issues/564