| ... | ... | @@ -383,6 +383,21 @@ pub fn LinearFifo( |
| 383 | 383 | self.discard(try dest_writer.write(self.readableSlice(0))); |
| 384 | 384 | } |
| 385 | 385 | } |
| 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 | } |
| 386 | 401 | }; |
| 387 | 402 | } |
| 388 | 403 | |