diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-08-11 23:35:36 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-08-28 18:30:57 -0700 |
| commit | 2dc6ddd7e8cfb83f5ffd57b8b40f33dde9de892d (patch) | |
| tree | 7d19676be4468c859bef8a2f7540e221dc142501 /lib | |
| parent | c8b983c59a91441a6cfa134764c6413195e824ad (diff) | |
| download | zig-2dc6ddd7e8cfb83f5ffd57b8b40f33dde9de892d.tar.gz zig-2dc6ddd7e8cfb83f5ffd57b8b40f33dde9de892d.zip | |
std.Io.Writer: add toArrayList/fromArrayList
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/std/Io/Writer.zig | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 707ed9cb94..42f4693786 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -8,6 +8,7 @@ const Limit = std.Io.Limit; const File = std.fs.File; const testing = std.testing; const Allocator = std.mem.Allocator; +const ArrayList = std.ArrayList; vtable: *const VTable, /// If this has length zero, the writer is unbuffered, and `flush` is a no-op. @@ -2374,6 +2375,28 @@ pub fn unreachableRebase(w: *Writer, preserve: usize, capacity: usize) Error!voi unreachable; } +pub fn fromArrayList(array_list: *ArrayList(u8)) Writer { + defer array_list.* = .empty; + return .{ + .vtable = &.{ + .drain = fixedDrain, + .flush = noopFlush, + }, + .buffer = array_list.allocatedSlice(), + .end = array_list.items.len, + }; +} + +pub fn toArrayList(w: *Writer) ArrayList(u8) { + const result: ArrayList(u8) = .{ + .items = w.buffer[0..w.end], + .capacity = w.buffer.len, + }; + w.buffer = &.{}; + w.end = 0; + return result; +} + /// Provides a `Writer` implementation based on calling `Hasher.update`, sending /// all data also to an underlying `Writer`. /// @@ -2546,7 +2569,7 @@ pub const Allocating = struct { } /// Replaces `array_list` with empty, taking ownership of the memory. - pub fn fromArrayList(allocator: Allocator, array_list: *std.ArrayListUnmanaged(u8)) Allocating { + pub fn fromArrayList(allocator: Allocator, array_list: *ArrayList(u8)) Allocating { defer array_list.* = .empty; return .{ .allocator = allocator, @@ -2572,9 +2595,9 @@ pub const Allocating = struct { /// Returns an array list that takes ownership of the allocated memory. /// Resets the `Allocating` to an empty state. - pub fn toArrayList(a: *Allocating) std.ArrayListUnmanaged(u8) { + pub fn toArrayList(a: *Allocating) ArrayList(u8) { const w = &a.writer; - const result: std.ArrayListUnmanaged(u8) = .{ + const result: ArrayList(u8) = .{ .items = w.buffer[0..w.end], .capacity = w.buffer.len, }; @@ -2603,7 +2626,7 @@ pub const Allocating = struct { pub fn toOwnedSliceSentinel(a: *Allocating, comptime sentinel: u8) error{OutOfMemory}![:sentinel]u8 { const gpa = a.allocator; - var list = toArrayList(a); + var list = @This().toArrayList(a); defer a.setArrayList(list); return list.toOwnedSliceSentinel(gpa, sentinel); } @@ -2670,7 +2693,7 @@ pub const Allocating = struct { list.ensureUnusedCapacity(gpa, minimum_len) catch return error.WriteFailed; } - fn setArrayList(a: *Allocating, list: std.ArrayListUnmanaged(u8)) void { + fn setArrayList(a: *Allocating, list: ArrayList(u8)) void { a.writer.buffer = list.allocatedSlice(); a.writer.end = list.items.len; } |
