| ... | ... | @@ -8,6 +8,7 @@ const Limit = std.Io.Limit; |
| 8 | 8 | const File = std.fs.File; |
| 9 | 9 | const testing = std.testing; |
| 10 | 10 | const Allocator = std.mem.Allocator; |
| 11 | const ArrayList = std.ArrayList; |
| 11 | 12 | |
| 12 | 13 | vtable: *const VTable, |
| 13 | 14 | /// 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 |
| 2374 | 2375 | unreachable; |
| 2375 | 2376 | } |
| 2376 | 2377 | |
| 2378 | pub 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 | |
| 2390 | pub 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 | |
| 2377 | 2400 | /// Provides a `Writer` implementation based on calling `Hasher.update`, sending |
| 2378 | 2401 | /// all data also to an underlying `Writer`. |
| 2379 | 2402 | /// |
| ... | ... | @@ -2546,7 +2569,7 @@ pub const Allocating = struct { |
| 2546 | 2569 | } |
| 2547 | 2570 | |
| 2548 | 2571 | /// 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 { |
| 2550 | 2573 | defer array_list.* = .empty; |
| 2551 | 2574 | return .{ |
| 2552 | 2575 | .allocator = allocator, |
| ... | ... | @@ -2572,9 +2595,9 @@ pub const Allocating = struct { |
| 2572 | 2595 | |
| 2573 | 2596 | /// Returns an array list that takes ownership of the allocated memory. |
| 2574 | 2597 | /// Resets the `Allocating` to an empty state. |
| 2575 | | pub fn toArrayList(a: *Allocating) std.ArrayListUnmanaged(u8) { |
| 2598 | pub fn toArrayList(a: *Allocating) ArrayList(u8) { |
| 2576 | 2599 | const w = &a.writer; |
| 2577 | | const result: std.ArrayListUnmanaged(u8) = .{ |
| 2600 | const result: ArrayList(u8) = .{ |
| 2578 | 2601 | .items = w.buffer[0..w.end], |
| 2579 | 2602 | .capacity = w.buffer.len, |
| 2580 | 2603 | }; |
| ... | ... | @@ -2603,7 +2626,7 @@ pub const Allocating = struct { |
| 2603 | 2626 | |
| 2604 | 2627 | pub fn toOwnedSliceSentinel(a: *Allocating, comptime sentinel: u8) error{OutOfMemory}![:sentinel]u8 { |
| 2605 | 2628 | const gpa = a.allocator; |
| 2606 | | var list = toArrayList(a); |
| 2629 | var list = @This().toArrayList(a); |
| 2607 | 2630 | defer a.setArrayList(list); |
| 2608 | 2631 | return list.toOwnedSliceSentinel(gpa, sentinel); |
| 2609 | 2632 | } |
| ... | ... | @@ -2670,7 +2693,7 @@ pub const Allocating = struct { |
| 2670 | 2693 | list.ensureUnusedCapacity(gpa, minimum_len) catch return error.WriteFailed; |
| 2671 | 2694 | } |
| 2672 | 2695 | |
| 2673 | | fn setArrayList(a: *Allocating, list: std.ArrayListUnmanaged(u8)) void { |
| 2696 | fn setArrayList(a: *Allocating, list: ArrayList(u8)) void { |
| 2674 | 2697 | a.writer.buffer = list.allocatedSlice(); |
| 2675 | 2698 | a.writer.end = list.items.len; |
| 2676 | 2699 | } |