| ... | @@ -338,86 +338,6 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty | ... | @@ -338,86 +338,6 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 338 | @memcpy(self.items[old_len..][0..items.len], items); | 338 | @memcpy(self.items[old_len..][0..items.len], items); |
| 339 | } | 339 | } |
| 340 | | 340 | |
| 341 | /// Initializes a `std.io.Writer` which will append to the list. | | |
| 342 | pub fn writer(self: *Self) std.io.Writer { | | |
| 343 | comptime assert(T == u8); | | |
| 344 | return .{ | | |
| 345 | .context = self, | | |
| 346 | .vtable = &.{ | | |
| 347 | .writev = expanding_writev, | | |
| 348 | .writeFile = expanding_writeFile, | | |
| 349 | }, | | |
| 350 | }; | | |
| 351 | } | | |
| 352 | | | |
| 353 | fn expanding_writev(context: *anyopaque, data: []const []const u8) anyerror!usize { | | |
| 354 | const self: *Self = @alignCast(@ptrCast(context)); | | |
| 355 | const original_len = self.items.len; | | |
| 356 | var new_capacity: usize = self.capacity; | | |
| 357 | for (data) |bytes| new_capacity += bytes.len; | | |
| 358 | try self.ensureTotalCapacity(new_capacity); | | |
| 359 | for (data) |bytes| self.appendSliceAssumeCapacity(bytes); | | |
| 360 | return self.items.len - original_len; | | |
| 361 | } | | |
| 362 | | | |
| 363 | fn expanding_writeFile( | | |
| 364 | context: *anyopaque, | | |
| 365 | file: std.fs.File, | | |
| 366 | offset: u64, | | |
| 367 | len: std.io.Writer.VTable.FileLen, | | |
| 368 | headers_and_trailers: []const []const u8, | | |
| 369 | headers_len: usize, | | |
| 370 | ) anyerror!usize { | | |
| 371 | const self: *Self = @alignCast(@ptrCast(context)); | | |
| 372 | const trailers = headers_and_trailers[headers_len..]; | | |
| 373 | const original_len = self.items.len; | | |
| 374 | if (len == .entire_file) { | | |
| 375 | var new_capacity: usize = self.capacity + std.atomic.cache_line; | | |
| 376 | for (headers_and_trailers) |bytes| new_capacity += bytes.len; | | |
| 377 | try self.ensureTotalCapacity(new_capacity); | | |
| 378 | for (headers_and_trailers[0..headers_len]) |bytes| self.appendSliceAssumeCapacity(bytes); | | |
| 379 | const dest = self.items.ptr[self.items.len..self.capacity]; | | |
| 380 | const n = try file.pread(dest, offset); | | |
| 381 | if (n == 0) { | | |
| 382 | new_capacity = self.capacity; | | |
| 383 | for (trailers) |bytes| new_capacity += bytes.len; | | |
| 384 | try self.ensureTotalCapacity(new_capacity); | | |
| 385 | for (trailers) |bytes| self.appendSliceAssumeCapacity(bytes); | | |
| 386 | return self.items.len - original_len; | | |
| 387 | } | | |
| 388 | self.items.len += n; | | |
| 389 | return self.items.len - original_len; | | |
| 390 | } | | |
| 391 | var new_capacity: usize = self.capacity + len.int(); | | |
| 392 | for (headers_and_trailers) |bytes| new_capacity += bytes.len; | | |
| 393 | try self.ensureTotalCapacity(new_capacity); | | |
| 394 | for (headers_and_trailers[0..headers_len]) |bytes| self.appendSliceAssumeCapacity(bytes); | | |
| 395 | const dest = self.items.ptr[self.items.len..][0..len.int()]; | | |
| 396 | const n = try file.pread(dest, offset); | | |
| 397 | self.items.len += n; | | |
| 398 | if (n < dest.len) return self.items.len - original_len; | | |
| 399 | for (trailers) |bytes| self.appendSliceAssumeCapacity(bytes); | | |
| 400 | return self.items.len - original_len; | | |
| 401 | } | | |
| 402 | | | |
| 403 | pub const FixedWriter = std.io.Writer(*Self, Allocator.Error, appendWriteFixed); | | |
| 404 | | | |
| 405 | /// Initializes a Writer which will append to the list but will return | | |
| 406 | /// `error.OutOfMemory` rather than increasing capacity. | | |
| 407 | pub fn fixedWriter(self: *Self) FixedWriter { | | |
| 408 | return .{ .context = self }; | | |
| 409 | } | | |
| 410 | | | |
| 411 | /// The purpose of this function existing is to match `std.io.Writer` API. | | |
| 412 | fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize { | | |
| 413 | const available_capacity = self.capacity - self.items.len; | | |
| 414 | if (m.len > available_capacity) | | |
| 415 | return error.OutOfMemory; | | |
| 416 | | | |
| 417 | self.appendSliceAssumeCapacity(m); | | |
| 418 | return m.len; | | |
| 419 | } | | |
| 420 | | | |
| 421 | /// Append a value to the list `n` times. | 341 | /// Append a value to the list `n` times. |
| 422 | /// Allocates more memory as necessary. | 342 | /// Allocates more memory as necessary. |
| 423 | /// Invalidates element pointers if additional memory is needed. | 343 | /// Invalidates element pointers if additional memory is needed. |
| ... | @@ -985,24 +905,6 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig | ... | @@ -985,24 +905,6 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 985 | bw.print(fmt, args) catch return error.OutOfMemory; | 905 | bw.print(fmt, args) catch return error.OutOfMemory; |
| 986 | } | 906 | } |
| 987 | | 907 | |
| 988 | pub const FixedWriter = std.io.Writer(*Self, Allocator.Error, appendWriteFixed); | | |
| 989 | | | |
| 990 | /// Initializes a Writer which will append to the list but will return | | |
| 991 | /// `error.OutOfMemory` rather than increasing capacity. | | |
| 992 | pub fn fixedWriter(self: *Self) FixedWriter { | | |
| 993 | return .{ .context = self }; | | |
| 994 | } | | |
| 995 | | | |
| 996 | /// The purpose of this function existing is to match `std.io.Writer` API. | | |
| 997 | fn appendWriteFixed(self: *Self, m: []const u8) error{OutOfMemory}!usize { | | |
| 998 | const available_capacity = self.capacity - self.items.len; | | |
| 999 | if (m.len > available_capacity) | | |
| 1000 | return error.OutOfMemory; | | |
| 1001 | | | |
| 1002 | self.appendSliceAssumeCapacity(m); | | |
| 1003 | return m.len; | | |
| 1004 | } | | |
| 1005 | | | |
| 1006 | /// Append a value to the list `n` times. | 908 | /// Append a value to the list `n` times. |
| 1007 | /// Allocates more memory as necessary. | 909 | /// Allocates more memory as necessary. |
| 1008 | /// Invalidates element pointers if additional memory is needed. | 910 | /// Invalidates element pointers if additional memory is needed. |