authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-28 16:49:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
logc583d140135fe5a57d055d9c0b8bdf59698f29e1
tree33a50b710b4e732ba760ae5262598ac12b34d03b
parenta42888e145ced152e6437c3241b799fd39c0da6a

std.fifo: add toOwnedSlice method


1 files changed, 15 insertions(+), 0 deletions(-)

lib/std/fifo.zig+15
......@@ -383,6 +383,21 @@ pub fn LinearFifo(
383383 self.discard(try dest_writer.write(self.readableSlice(0)));
384384 }
385385 }
386
387 pub fn toOwnedSlice(self: *Self) Allocator.Error![]T {
388 assert(self.head == 0);
389 assert(self.count <= self.buf.len);
390 const allocator = self.allocator;
391 if (allocator.resize(self.buf, self.count)) {
392 const result = self.buf[0..self.count];
393 self.* = Self.init(allocator);
394 return result;
395 }
396 const new_memory = try allocator.dupe(T, self.buf[0..self.count]);
397 allocator.free(self.buf);
398 self.* = Self.init(allocator);
399 return new_memory;
400 }
386401 };
387402}
388403