From 79ad1d96108fc25fea6a2fbdb5c9bb8b2093a6b5 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 7 Feb 2018 22:18:48 +0100 Subject: format struct pointers as "@
" (#752) --- std/fmt/index.zig | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'std') 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 -- cgit v1.2.3