| ... | @@ -189,6 +189,22 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { | ... | @@ -189,6 +189,22 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { |
| 189 | self.len += items.len; | 189 | self.len += items.len; |
| 190 | } | 190 | } |
| 191 | | 191 | |
| | 192 | pub usingnamespace if (T == u8) |
| | 193 | struct { |
| | 194 | /// Same as `append` except it returns the number of bytes written, which is always the same |
| | 195 | /// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API. |
| | 196 | fn appendWrite(self: *Self, m: []const u8) !usize { |
| | 197 | try self.appendSlice(m); |
| | 198 | return m.len; |
| | 199 | } |
| | 200 | |
| | 201 | pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) { |
| | 202 | return .{ .context = self }; |
| | 203 | } |
| | 204 | } |
| | 205 | else |
| | 206 | struct {}; |
| | 207 | |
| 192 | /// Append a value to the list `n` times. Allocates more memory | 208 | /// Append a value to the list `n` times. Allocates more memory |
| 193 | /// as necessary. | 209 | /// as necessary. |
| 194 | pub fn appendNTimes(self: *Self, value: T, n: usize) !void { | 210 | pub fn appendNTimes(self: *Self, value: T, n: usize) !void { |
| ... | @@ -479,3 +495,14 @@ test "std.ArrayList: ArrayList(T) of struct T" { | ... | @@ -479,3 +495,14 @@ test "std.ArrayList: ArrayList(T) of struct T" { |
| 479 | try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) }); | 495 | try root.sub_items.append(Item{ .integer = 42, .sub_items = ArrayList(Item).init(testing.allocator) }); |
| 480 | testing.expect(root.sub_items.items[0].integer == 42); | 496 | testing.expect(root.sub_items.items[0].integer == 42); |
| 481 | } | 497 | } |
| | 498 | |
| | 499 | test "std.ArrayList(u8) implements outStream" { |
| | 500 | var buffer = ArrayList(u8).init(std.testing.allocator); |
| | 501 | defer buffer.deinit(); |
| | 502 | |
| | 503 | const x: i32 = 42; |
| | 504 | const y: i32 = 1234; |
| | 505 | try buffer.outStream().print("x: {}\ny: {}\n", .{ x, y }); |
| | 506 | |
| | 507 | testing.expectEqualSlices(u8, "x: 42\ny: 1234\n", buffer.span()); |
| | 508 | } |