From 9129fb28dcc3c94a709cefd8040f41b9125693ee Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 20 Feb 2024 03:16:02 -0700 Subject: std.ArrayList: add writerAssumeCapacity Useful when you want to use an ArrayList to operate on a static buffer. --- lib/std/array_list.zig | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'lib/std/array_list.zig') 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. -- cgit v1.2.3 From 17291e072b86b7f2bcb775907cf0984e281662a2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 21 Feb 2024 17:37:41 -0700 Subject: std.ArrayList: fixedWriter A writer that appends to the list, returning error.OutOfMemory rather than attempting to increase capacity. --- lib/std/array_list.zig | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/std/array_list.zig') diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index e5fcb8f4fa..1926f627f3 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -946,18 +946,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ return m.len; } - pub const WriterAssumeCapacity = std.io.Writer(*Self, error{}, appendWriteAssumeCapacity); + pub const FixedWriter = std.io.Writer(*Self, Allocator.Error, appendWriteFixed); - /// Initializes a Writer which will append to the list, asserting the - /// list can hold the additional bytes. - pub fn writerAssumeCapacity(self: *Self) WriterAssumeCapacity { + /// Initializes a Writer which will append to the list but will return + /// `error.OutOfMemory` rather than increasing capacity. + pub fn fixedWriter(self: *Self) FixedWriter { 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 { + /// The purpose of this function existing is to match `std.io.Writer` API. + fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize { + const available_capacity = self.capacity - self.items.len; + if (m.len > available_capacity) + return error.OutOfMemory; + self.appendSliceAssumeCapacity(m); return m.len; } -- cgit v1.2.3