mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.ArrayList: fixedWriter
A writer that appends to the list, returning error.OutOfMemory rather than attempting to increase capacity.
This commit is contained in:
parent
2e7d8062ca
commit
17291e072b
1 changed files with 10 additions and 8 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue