aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_list.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-02-20 03:16:02 -0700
committerAndrew Kelley <andrew@ziglang.org>2024-02-23 02:37:11 -0700
commit9129fb28dcc3c94a709cefd8040f41b9125693ee (patch)
treeb42589f8b298bde542e8caf807949e773b5a2408 /lib/std/array_list.zig
parent2df3de1e209076cb27d6573a0cdd2bed075d88b3 (diff)
downloadzig-9129fb28dcc3c94a709cefd8040f41b9125693ee.tar.gz
zig-9129fb28dcc3c94a709cefd8040f41b9125693ee.zip
std.ArrayList: add writerAssumeCapacity
Useful when you want to use an ArrayList to operate on a static buffer.
Diffstat (limited to 'lib/std/array_list.zig')
-rw-r--r--lib/std/array_list.zig21
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index 79ed5c192c..e5fcb8f4fa 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -937,14 +937,31 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
return .{ .context = .{ .self = self, .allocator = allocator } };
}
- /// 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.Writer` API.
+ /// 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.Writer` API.
/// Invalidates element pointers if additional memory is needed.
fn appendWrite(context: WriterContext, m: []const u8) Allocator.Error!usize {
try context.self.appendSlice(context.allocator, m);
return m.len;
}
+ pub const WriterAssumeCapacity = std.io.Writer(*Self, error{}, appendWriteAssumeCapacity);
+
+ /// Initializes a Writer which will append to the list, asserting the
+ /// list can hold the additional bytes.
+ pub fn writerAssumeCapacity(self: *Self) WriterAssumeCapacity {
+ return .{ .context = self };
+ }
+
+ /// Same as `appendSliceAssumeCapacity` 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.Writer` API.
+ fn appendWriteAssumeCapacity(self: *Self, m: []const u8) error{}!usize {
+ self.appendSliceAssumeCapacity(m);
+ return m.len;
+ }
+
/// Append a value to the list `n` times.
/// Allocates more memory as necessary.
/// Invalidates element pointers if additional memory is needed.