diff options
| author | Ben Noordhuis <info@bnoordhuis.nl> | 2018-02-07 22:18:48 +0100 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-02-07 16:18:48 -0500 |
| commit | 79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5 (patch) | |
| tree | 03aee28d441bdef135727d65615dcc7210a82c6d /std | |
| parent | 0090c2d70b89cc6b5ab02bdc02aa49c4caf71a5a (diff) | |
| download | zig-79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5.tar.gz zig-79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5.zip | |
format struct pointers as "<typename>@<address>" (#752)
Diffstat (limited to 'std')
| -rw-r--r-- | std/fmt/index.zig | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/std/fmt/index.zig b/std/fmt/index.zig index b7ae018bdc..a32a6e0295 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -228,7 +228,7 @@ pub fn formatValue(value: var, context: var, output: fn(@typeOf(context), []cons if (@typeId(T.Child) == builtin.TypeId.Array and T.Child.Child == u8) { return output(context, (*value)[0..]); } else { - @compileError("Unable to format type '" ++ @typeName(T) ++ "'"); + return format(context, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value)); } }, else => if (@canImplicitCast([]const u8, value)) { @@ -546,6 +546,12 @@ test "parse unsigned comptime" { } } +// Dummy field because of https://github.com/zig-lang/zig/issues/557. +// At top level because of https://github.com/zig-lang/zig/issues/675. +const Struct = struct { + unused: u8, +}; + test "fmt.format" { { var buf1: [32]u8 = undefined; @@ -577,6 +583,14 @@ test "fmt.format" { const result = try bufPrint(buf1[0..], "u3: {}\n", value); assert(mem.eql(u8, result, "u3: 5\n")); } + { + var buf1: [32]u8 = undefined; + const value = Struct { + .unused = 42, + }; + const result = try bufPrint(buf1[0..], "pointer: {}\n", &value); + assert(mem.startsWith(u8, result, "pointer: Struct@")); + } // TODO get these tests passing in release modes // https://github.com/zig-lang/zig/issues/564 |
