From 0ee2462a3116020013f8c55e830182df0fa369f2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 1 Mar 2020 19:13:26 +1100 Subject: [PATCH] std: add std.ArrayList(u8).outStream() --- lib/std/array_list.zig | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index cbbec0b4f3..87bf3c51df 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -189,6 +189,22 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { self.len += items.len; } + pub usingnamespace if (T == u8) + struct { + /// Same as `append` except it returns the number of bytes written, which is always the same + /// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API. + fn appendWrite(self: *Self, m: []const u8) !usize { + try self.appendSlice(m); + return m.len; + } + + pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) { + return .{ .context = self }; + } + } + else + struct {}; + /// Append a value to the list `n` times. Allocates more memory /// as necessary. pub fn appendNTimes(self: *Self, value: T, n: usize) !void { @@ -479,3 +495,14 @@ test "std.ArrayList: ArrayList(T) of struct T" { try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) }); testing.expect(root.sub_items.items[0].integer == 42); } + +test "std.ArrayList(u8) implements outStream" { + var buffer = ArrayList(u8).init(std.testing.allocator); + defer buffer.deinit(); + + const x: i32 = 42; + const y: i32 = 1234; + try buffer.outStream().print("x: {}\ny: {}\n", .{ x, y }); + + testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span()); +}