Skip to content

Instantly share code, notes, and snippets.

@komuw
Created May 15, 2019 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komuw/ed7392120c1b80381641ef763857ece6 to your computer and use it in GitHub Desktop.
Save komuw/ed7392120c1b80381641ef763857ece6 to your computer and use it in GitHub Desktop.
we can print a buffer but not ArrayList
const std = @import("std");
const warn = std.debug.warn;
pub fn main() !void {
// 1. This works
var buf = try std.Buffer.init(std.debug.global_allocator, "");
defer buf.deinit();
try buf.append("hello");
warn("buffer:\n\t {}", buf);
// 2. But this does NOT work
var list = std.ArrayList(i32).init(std.debug.global_allocator);
defer list.deinit();
try list.append(1);
warn("list:\n\t {}", list);
}
@komuw
Copy link
Author

komuw commented May 15, 2019

ie
warn("buffer:\n\t {}", buf); prints

buffer:
	 Buffer{ .list = std.array_list.AlignedArrayList(u8,1){ .items = hello��, .len = 6, .allocator = Allocator{ .reallocFn = fn(*std.mem.Allocator, []u8, u29, usize, u29) std.mem.Error![]u8@10b9addd0, .shrinkFn = fn(*std.mem.Allocator, []u8, u29, usize, u29) []u8@10b9ae010 } } }

however, warn("list:\n\t {}", list); results in;

/usr/local/Cellar/zig/HEAD-057a5d4/lib/zig/std/fmt.zig:239:50: error: expected type '[]const u8', found '[]i32'
                const casted_value = ([]const u8)(value);
                                                 ^
/usr/local/Cellar/zig/HEAD-057a5d4/lib/zig/std/fmt.zig:186:39: note: called from here
                        try formatType(@field(value, @memberName(T, field_i)), "", context, Errors, output, max_depth - 1);
                                      ^
/usr/local/Cellar/zig/HEAD-057a5d4/lib/zig/std/fmt.zig:54:35: note: called from here
                    try formatType(args[next_arg], fmt[0..0], context, Errors, output, default_max_depth);
                                  ^
/usr/local/Cellar/zig/HEAD-057a5d4/lib/zig/std/io.zig:224:34: note: called from here
            return std.fmt.format(self, Error, self.writeFn, format, args);
                                 ^
/usr/local/Cellar/zig/HEAD-057a5d4/lib/zig/std/debug.zig:48:17: note: called from here
    stderr.print(fmt, args) catch return;
                ^
/Users/komuw/mystuff/wijisqs/cool.zig:15:9: note: called from here
    warn("list:\n\t {}", list);
        ^
/usr/local/Cellar/zig/HEAD-057a5d4/lib/zig/std/fmt.zig:239:50: note: slice type child 'i32' cannot cast into slice type child 'u8'
                const casted_value = ([]const u8)(value);

@komuw
Copy link
Author

komuw commented May 15, 2019

Initially I thought that std.Buffer is able to be printed because maybe it declares a custom format method but it doesn't;
https://github.com/ziglang/zig/blob/master/std/buffer.zig

@komuw
Copy link
Author

komuw commented May 15, 2019

Answer from IRC:
the std.Buffer contains a std.ArrayList(u8) while the second bit uses std.ArrayList(i32), bytes are printable

@komuw
Copy link
Author

komuw commented May 15, 2019

if we were to create a custom format method for std.ArrayList it would do almost what we want;

pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
    return struct {
        const Self = @This();

        // Custom format method
        pub fn format(
            self: Self,
            comptime fmt: []const u8,
            context: var,
            comptime Errors: type,
            output: fn (@typeOf(context), []const u8) Errors!void,
        ) Errors!void {
            return std.fmt.format(context, Errors, output, "ArrayList: .capacity: {}, .len: {}, .allocator: {}", self.capacity(), self.len, self.allocator);
        }
    ...
    ...
pub fn main() !void {
    var list = ArrayList(i32).init(std.debug.global_allocator); 
    defer list.deinit();
    try list.append(131);
    try list.append(12424);
    try list.append(12);

    warn("\n\nlist:\n\t {}", list);
}

zig fmt . && zig build-exe main.zig && ./main

list:
	 ArrayList: .capacity: 8, .len: 3, .allocator: Allocator{ .reallocFn = fn(*std.mem.Allocator, []u8, u29, usize, u29) std.mem.Error![]u8@1065f8fd0, .shrinkFn = fn(*std.mem.Allocator, []u8, u29, usize, u29) []u8@1065f9210 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment