aboutsummaryrefslogtreecommitdiff
path: root/lib/std/array_list.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-07-10 12:04:27 +0200
committerGitHub <noreply@github.com>2025-07-10 12:04:27 +0200
commit1a998886c863a1829d649f196093f1058cd9cf13 (patch)
treedb1b3b0a043a113cfb6544e8103cb64f8e4e4d8f /lib/std/array_list.zig
parent5b4b033236a7bed19c90faf7fefbc1990911cfef (diff)
parent10d6db5d7d1fb62ee2915a7ad2c7feb771ea3bbb (diff)
downloadzig-1a998886c863a1829d649f196093f1058cd9cf13.tar.gz
zig-1a998886c863a1829d649f196093f1058cd9cf13.zip
Merge pull request #24329 from ziglang/writergate
Deprecates all existing std.io readers and writers in favor of the newly provided std.io.Reader and std.io.Writer which are non-generic and have the buffer above the vtable - in other words the buffer is in the interface, not the implementation. This means that although Reader and Writer are no longer generic, they are still transparent to optimization; all of the interface functions have a concrete hot path operating on the buffer, and only make vtable calls when the buffer is full.
Diffstat (limited to 'lib/std/array_list.zig')
-rw-r--r--lib/std/array_list.zig55
1 files changed, 37 insertions, 18 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig
index 2a9159aeac..c3fade794f 100644
--- a/lib/std/array_list.zig
+++ b/lib/std/array_list.zig
@@ -338,11 +338,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty
@memcpy(self.items[old_len..][0..items.len], items);
}
- pub const Writer = if (T != u8)
- @compileError("The Writer interface is only defined for ArrayList(u8) " ++
- "but the given type is ArrayList(" ++ @typeName(T) ++ ")")
- else
- std.io.Writer(*Self, Allocator.Error, appendWrite);
+ pub fn print(self: *Self, comptime fmt: []const u8, args: anytype) error{OutOfMemory}!void {
+ const gpa = self.allocator;
+ var unmanaged = self.moveToUnmanaged();
+ defer self.* = unmanaged.toManaged(gpa);
+ try unmanaged.print(gpa, fmt, args);
+ }
+
+ pub const Writer = if (T != u8) void else std.io.GenericWriter(*Self, Allocator.Error, appendWrite);
/// Initializes a Writer which will append to the list.
pub fn writer(self: *Self) Writer {
@@ -350,14 +353,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty
}
/// 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.
+ /// as `m.len`. The purpose of this function existing is to match `std.io.GenericWriter` API.
/// Invalidates element pointers if additional memory is needed.
fn appendWrite(self: *Self, m: []const u8) Allocator.Error!usize {
try self.appendSlice(m);
return m.len;
}
- pub const FixedWriter = std.io.Writer(*Self, Allocator.Error, appendWriteFixed);
+ pub const FixedWriter = std.io.GenericWriter(*Self, Allocator.Error, appendWriteFixed);
/// Initializes a Writer which will append to the list but will return
/// `error.OutOfMemory` rather than increasing capacity.
@@ -365,7 +368,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty
return .{ .context = self };
}
- /// The purpose of this function existing is to match `std.io.Writer` API.
+ /// The purpose of this function existing is to match `std.io.GenericWriter` API.
fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize {
const available_capacity = self.capacity - self.items.len;
if (m.len > available_capacity)
@@ -933,40 +936,56 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig
@memcpy(self.items[old_len..][0..items.len], items);
}
+ pub fn print(self: *Self, gpa: Allocator, comptime fmt: []const u8, args: anytype) error{OutOfMemory}!void {
+ comptime assert(T == u8);
+ try self.ensureUnusedCapacity(gpa, fmt.len);
+ var aw: std.io.Writer.Allocating = .fromArrayList(gpa, self);
+ defer self.* = aw.toArrayList();
+ return aw.writer.print(fmt, args) catch |err| switch (err) {
+ error.WriteFailed => return error.OutOfMemory,
+ };
+ }
+
+ pub fn printAssumeCapacity(self: *Self, comptime fmt: []const u8, args: anytype) void {
+ comptime assert(T == u8);
+ var w: std.io.Writer = .fixed(self.unusedCapacitySlice());
+ w.print(fmt, args) catch unreachable;
+ self.items.len += w.end;
+ }
+
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub const WriterContext = struct {
self: *Self,
allocator: Allocator,
};
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub const Writer = if (T != u8)
@compileError("The Writer interface is only defined for ArrayList(u8) " ++
"but the given type is ArrayList(" ++ @typeName(T) ++ ")")
else
- std.io.Writer(WriterContext, Allocator.Error, appendWrite);
+ std.io.GenericWriter(WriterContext, Allocator.Error, appendWrite);
- /// Initializes a Writer which will append to the list.
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub fn writer(self: *Self, gpa: Allocator) Writer {
return .{ .context = .{ .self = self, .allocator = gpa } };
}
- /// 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.
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
fn appendWrite(context: WriterContext, m: []const u8) Allocator.Error!usize {
try context.self.appendSlice(context.allocator, m);
return m.len;
}
- pub const FixedWriter = std.io.Writer(*Self, Allocator.Error, appendWriteFixed);
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
+ pub const FixedWriter = std.io.GenericWriter(*Self, Allocator.Error, appendWriteFixed);
- /// Initializes a Writer which will append to the list but will return
- /// `error.OutOfMemory` rather than increasing capacity.
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
pub fn fixedWriter(self: *Self) FixedWriter {
return .{ .context = self };
}
- /// The purpose of this function existing is to match `std.io.Writer` API.
+ /// Deprecated in favor of `print` or `std.io.Writer.Allocating`.
fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize {
const available_capacity = self.capacity - self.items.len;
if (m.len > available_capacity)