authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-11 23:35:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
log2dc6ddd7e8cfb83f5ffd57b8b40f33dde9de892d
tree7d19676be4468c859bef8a2f7540e221dc142501
parentc8b983c59a91441a6cfa134764c6413195e824ad

std.Io.Writer: add toArrayList/fromArrayList


1 files changed, 28 insertions(+), 5 deletions(-)

lib/std/Io/Writer.zig+28-5
......@@ -8,6 +8,7 @@ const Limit = std.Io.Limit;
88const File = std.fs.File;
99const testing = std.testing;
1010const Allocator = std.mem.Allocator;
11const ArrayList = std.ArrayList;
1112
1213vtable: *const VTable,
1314/// 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
23742375 unreachable;
23752376}
23762377
2378pub fn fromArrayList(array_list: *ArrayList(u8)) Writer {
2379 defer array_list.* = .empty;
2380 return .{
2381 .vtable = &.{
2382 .drain = fixedDrain,
2383 .flush = noopFlush,
2384 },
2385 .buffer = array_list.allocatedSlice(),
2386 .end = array_list.items.len,
2387 };
2388}
2389
2390pub fn toArrayList(w: *Writer) ArrayList(u8) {
2391 const result: ArrayList(u8) = .{
2392 .items = w.buffer[0..w.end],
2393 .capacity = w.buffer.len,
2394 };
2395 w.buffer = &.{};
2396 w.end = 0;
2397 return result;
2398}
2399
23772400/// Provides a `Writer` implementation based on calling `Hasher.update`, sending
23782401/// all data also to an underlying `Writer`.
23792402///
......@@ -2546,7 +2569,7 @@ pub const Allocating = struct {
25462569 }
25472570
25482571 /// Replaces `array_list` with empty, taking ownership of the memory.
2549 pub fn fromArrayList(allocator: Allocator, array_list: *std.ArrayListUnmanaged(u8)) Allocating {
2572 pub fn fromArrayList(allocator: Allocator, array_list: *ArrayList(u8)) Allocating {
25502573 defer array_list.* = .empty;
25512574 return .{
25522575 .allocator = allocator,
......@@ -2572,9 +2595,9 @@ pub const Allocating = struct {
25722595
25732596 /// Returns an array list that takes ownership of the allocated memory.
25742597 /// Resets the `Allocating` to an empty state.
2575 pub fn toArrayList(a: *Allocating) std.ArrayListUnmanaged(u8) {
2598 pub fn toArrayList(a: *Allocating) ArrayList(u8) {
25762599 const w = &a.writer;
2577 const result: std.ArrayListUnmanaged(u8) = .{
2600 const result: ArrayList(u8) = .{
25782601 .items = w.buffer[0..w.end],
25792602 .capacity = w.buffer.len,
25802603 };
......@@ -2603,7 +2626,7 @@ pub const Allocating = struct {
26032626
26042627 pub fn toOwnedSliceSentinel(a: *Allocating, comptime sentinel: u8) error{OutOfMemory}![:sentinel]u8 {
26052628 const gpa = a.allocator;
2606 var list = toArrayList(a);
2629 var list = @This().toArrayList(a);
26072630 defer a.setArrayList(list);
26082631 return list.toOwnedSliceSentinel(gpa, sentinel);
26092632 }
......@@ -2670,7 +2693,7 @@ pub const Allocating = struct {
26702693 list.ensureUnusedCapacity(gpa, minimum_len) catch return error.WriteFailed;
26712694 }
26722695
2673 fn setArrayList(a: *Allocating, list: std.ArrayListUnmanaged(u8)) void {
2696 fn setArrayList(a: *Allocating, list: ArrayList(u8)) void {
26742697 a.writer.buffer = list.allocatedSlice();
26752698 a.writer.end = list.items.len;
26762699 }