From dd568a92c67a23bd1f64051deac876ddefd1c1de Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 26 May 2026 03:04:18 -0700 Subject: [PATCH] AllocatingWriter: add appendAssumeCapacity --- allocating_writer.zig | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/allocating_writer.zig b/allocating_writer.zig index cbea9806bf0f6294637ec4698a06d028d115cb84..4f32ace5dc60b20ced239d3c34167437ba0f127d 100644 --- a/allocating_writer.zig +++ b/allocating_writer.zig @@ -55,6 +55,11 @@ pub const AllocatingWriter = struct { return self.allocatedSlice()[self.items.len..]; } + fn appendAssumeCapacity(self: *AllocatingWriter, bytes: []const u8) void { + @memcpy(self.unusedSlice()[0..bytes.len], bytes); + self.items.len += bytes.len; + } + const W = nio.Writable(@This(), ._var); pub const writeAll = W.writeAll; pub const writevAll = W.writevAll; @@ -69,15 +74,14 @@ pub const AllocatingWriter = struct { pub fn write(self: *AllocatingWriter, bytes: []const u8) WriteError!usize { try self.ensureUnusedCapacity(bytes.len); - @memcpy(self.unusedSlice()[0..bytes.len], bytes); - self.items.len += bytes.len; + self.appendAssumeCapacity(bytes); return bytes.len; } pub fn writev(self: *AllocatingWriter, iovec: []const sys.struct_iovec) WriteError!usize { var len: usize = 0; for (iovec) |vec| len += vec.len; try self.ensureUnusedCapacity(len); - for (iovec) |vec| _ = self.write(vec.base[0..vec.len]) catch unreachable; + for (iovec) |vec| self.appendAssumeCapacity(vec.base[0..vec.len]); return len; } }; -- 2.54.0