Io.Writer.Allocating: .init vs .fromArrayList

I’m trying to update tools/doctest to use the new Io.Writer interface.

Here is an example of the current code: zig/tools/doctest.zig at master · ziglang/zig · GitHub

The problem is that I’m not sure what I should use between .init and .fromArrayList.

The documentation is not very useful.

Thanks.

Its very simple, do you need to use the Arraylist before turning it into a writer? Then use fromArrayList if not, use init preferably initCapacity if you have at least a reasonable assumption required space.

1 Like

Here’s how I’d update that test code:

var buffer = std.array_list.Managed(u8).init(test_allocator);
defer buffer.deinit();

try printShell(buffer.writer(), shell_out, false);
try testing.expectEqualSlices(u8, expected, buffer.items);

:down_arrow:

var aw: std.Io.Writer.Allocating = .init(test_allocator);
defer aw.deinit();

try printShell(&aw.writer, shell_out, false);
try testing.expectEqualSlices(u8, expected, aw.written());
1 Like

This is the same code that I’m currently used.

        var a: Io.Writer.Allocating = .init(test_allocator);
        defer a.deinit();
        const writer = &a.writer;

I got the code from test Allocating test.

Thanks for the confirmation.

Just to be sure, are both .init and .fromArrayList have the same performance?

Is .fromArrayList primary used for ArrayList.print?

It just uses the existing allocation in the array list.

It also sets the array list to .empty so you can’t step on the writer’s toes.

But, checking the code, Allocating.sendFile creates a new ArrayList each time it is called, so it seems to me that .init is less efficient compared to .fromArrayList.

Update: this is incorrect, since the ArrayList is replaced with empty. Still not sure when you need to use .fromArrayList or .empty.

Thanks

I’m having some problems with updating the code in zig/tools/doctest.zig at master · ziglang/zig · GitHub.

Here is the diff with the new code using Io.Writer.Allocating.
The doctest.zig is from the current master.

diff --git a/tools/doctest.zig b/tools/doctest.zig
index a762b8d0b9..266aa2143f 100644
--- a/tools/doctest.zig
+++ b/tools/doctest.zig
@@ -1011,10 +1011,16 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
     const supported_sgr_colors = [_]u8{ 31, 32, 36 };
     const supported_sgr_numbers = [_]u8{ 0, 1, 2 };
 
-    var buf = std.array_list.Managed(u8).init(allocator);
-    defer buf.deinit();
+    //var buf = std.array_list.Managed(u8).init(allocator);
+    //defer buf.deinit();
+    //
+    //var out = buf.writer();
+
+    var buf: std.ArrayList(u8) = .empty;
+    var a: std.Io.Writer.Allocating = .fromArrayList(allocator, &buf);
+    defer a.deinit();
+    const out = &a.writer;
 
-    var out = buf.writer();
     var sgr_param_start_index: usize = undefined;
     var sgr_num: u8 = undefined;
     var sgr_color: u8 = undefined;
@@ -1118,7 +1124,7 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
             },
         }
     }
-    return try buf.toOwnedSlice();
+    return try buf.toOwnedSlice(allocator);
 }
 
 // Returns true if number is in slice.

I used .fromArrayList due to the code constraint.
When running zig test tool/doctest.zig, the test fails because the termColor function returns an empty string.

Probably I’m doing something wrong, but I have no idea about
how to fix it.

Update: I fixed it, using a.writer.end in

and

Thanks