| ... | @@ -946,18 +946,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ | ... | @@ -946,18 +946,20 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ |
| 946 | return m.len; | 946 | return m.len; |
| 947 | } | 947 | } |
| 948 | | 948 | |
| 949 | pub const WriterAssumeCapacity = std.io.Writer(*Self, error{}, appendWriteAssumeCapacity); | 949 | pub const FixedWriter = std.io.Writer(*Self, Allocator.Error, appendWriteFixed); |
| 950 | | 950 | |
| 951 | /// Initializes a Writer which will append to the list, asserting the | 951 | /// Initializes a Writer which will append to the list but will return |
| 952 | /// list can hold the additional bytes. | 952 | /// `error.OutOfMemory` rather than increasing capacity. |
| 953 | pub fn writerAssumeCapacity(self: *Self) WriterAssumeCapacity { | 953 | pub fn fixedWriter(self: *Self) FixedWriter { |
| 954 | return .{ .context = self }; | 954 | return .{ .context = self }; |
| 955 | } | 955 | } |
| 956 | | 956 | |
| 957 | /// Same as `appendSliceAssumeCapacity` except it returns the number of bytes written, | 957 | /// The purpose of this function existing is to match `std.io.Writer` API. |
| 958 | /// which is always the same as `m.len`. The purpose of this function | 958 | fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize { |
| 959 | /// existing is to match `std.io.Writer` API. | 959 | const available_capacity = self.capacity - self.items.len; |
| 960 | fn appendWriteAssumeCapacity(self: *Self, m: []const u8) error{}!usize { | 960 | if (m.len > available_capacity) |
| | 961 | return error.OutOfMemory; |
| | 962 | |
| 961 | self.appendSliceAssumeCapacity(m); | 963 | self.appendSliceAssumeCapacity(m); |
| 962 | return m.len; | 964 | return m.len; |
| 963 | } | 965 | } |