Make std.debug.print() output less verbose

Current, where you use std.debug.print() to print out a struct, the function outputs the type name of the struct along with its contents. This name can be really long sometimes when the struct is defined through a function, as arguments passed to it appears in the name. Example:

const std = @import("std");

const Cow = struct {};

const Hello = struct {
    fn World(_: i32, _: *const Cow) type {
        return struct {
            number1: i32 = 123,
            number2: i32 = 456,
        };
    }
};

pub fn main() void {
    const s = Hello.World(1234, @ptrFromInt(0xdead_beef_baad_f00d)){};
    std.debug.print("{any}\n", .{s});
}
test.Hello.World(1234,@as(*const test.Cow, @ptrFromInt(deadbeefbaadf00d)).*){ .number1 = 123, .number2 = 456 }

Long type names make it difficult to find the information you need while debugging. And they’re generally unhelpful. If I’m dumping something to the screen, I know what it is already. It’s perfectly reasonable to replace the name with a dot.

1 Like

I think that if you implement a custom format function it gets called automatically when you specify any, at least I’m pretty sure I did that in some of my code at some point. It works a bit like C++ std::ostream &operator<<(std::ostream &os, foo const &self)

I think the way to do it is to make a function like this

        pub fn format(
            self: Self,
            fmt: []const u8,
            _: std.fmt.FormatOptions,
            writer: anytype,
        ) !void {
            _ = try writer.print(fmt,.{self.my_stuff});
        }

I don’t remember if that solves your problem entirely, but I know that when I was porting one of my game to Zig, I was using that everywhere for nice debugging. There’s probably better way to do it, but I’m pretty sure there is a way.

3 Likes

Thanks for the suggestion. Didn’t know about the format function. In my case it doesn’t help since I’d still need to write print logic.

To better illustrate the issue, here’s what my code produces when it does a std.debug.print("{any}", ...) on a struct:

fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 5, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 12, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_ax_imm, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = 3134975728, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_rm_r, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 0, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_ax_imm, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = 3135168848, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 6, .mod = 1 }, .sib = null, .disp8 = 4, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 6, .mod = 0 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 44, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 44, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 44, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 44, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 44, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 44, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 0, .reg = 2, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 0, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 44, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.ret, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.push_si, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 5, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 8, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_ax_imm, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = 3134975728, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_rm_r, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 6, .reg = 2, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_rm_r, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 2, .reg = 1, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_rm_r, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 0, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_ax_imm, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = 3135168848, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_r_m, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 1, .mod = 0 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 5, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 12, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 40, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 40, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 40, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 40, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.push_si, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 0, .reg = 2, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 0, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 40, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.pop_si, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.ret, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 5, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 12, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_ax_imm, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = 3134975728, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_rm_r, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 0, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mov_ax_imm, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = 3135168848, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 6, .mod = 1 }, .sib = null, .disp8 = 4, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 1, .reg = 6, .mod = 0 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 6, .mod = 1 }, .sib = fn-binding.Instruction.Op__struct_2766.SIB{ .base = 4, .index = 4, .scale = 0 }, .disp8 = 92, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.mux_rm, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 0, .reg = 2, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.add_rm_imm8, .mod_rm = fn-binding.Instruction.Op__struct_2766.ModRM{ .rm = 4, .reg = 0, .mod = 3 }, .sib = null, .disp8 = null, .disp32 = null, .imm8 = 92, .imm32 = null, .imm64 = null }
fn-binding.Instruction.Op__struct_2766{ .prefix = null, .rex = null, .code = fn-binding.Instruction.Op__struct_2766.Code.ret, .mod_rm = null, .sib = null, .disp8 = null, .disp32 = null, .imm8 = null, .imm32 = null, .imm64 = null }

It’s a complete unreadable mess.

Try std.json.fmt.

Example:

std.debug.print("{s}\n", .{std.json.fmt(s, .{ .whitespace = .indent_2 })});

Output:

{
  "number1": 123,
  "number2": 456
}

If json.fmt does not suite your need, you can make your format method or formatting adapter starting from json.fmt source code.

2 Likes

Maybe this library could help you ? Pretty, it does pretty printing for any kind of struct, so maybe this can be useful

1 Like

Ah, that’s a good idea! I’ll be using that as a workaround for the time being.

The issue with long type names need to be dealt with though. Even types defined at the top level can easily have names longer than 20 characters due to the inclusion of the filename. Names with 100+ character aren’t at all uncommon. I’ve seen names breaking 1000 (typically involving anonymous structs/tuples). Including the type name in the print out is just not practical.