authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-18 16:34:27 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-18 16:34:27 -08:00
log75b8d9e0c41c178da7f92f13760c4966037302fb
tree4b43d040a3726b4a3b42236c86f854d74ede06b3
parent8dec0da7fbbe80b15c24482125142e172c9a4cf4

add writev methods


4 files changed, 78 insertions(+), 1 deletions(-)

fixed_buffer_stream.zig+17
......@@ -1,4 +1,12 @@
11const std = @import("std");
2const builtin = @import("builtin");
3const extras = @import("extras");
4const sys_linux = @import("sys-linux");
5
6const sys = switch (builtin.target.os.tag) {
7 .linux => sys_linux,
8 else => unreachable,
9};
210
311const nio = @import("./nio.zig");
412
......@@ -53,6 +61,15 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
5361 if (n == 0) return error.NoSpaceLeft;
5462 return n;
5563 }
64 pub fn writev(self: *Self, iovec: []const sys.struct_iovec) WriteError!usize {
65 var count: usize = 0;
66 for (iovec) |vec| {
67 const actual = try write(self, vec.base[0..vec.len]);
68 count += actual;
69 if (actual < vec.len) break;
70 }
71 return count;
72 }
5673
5774 pub fn anyWritable(self: *Self) nio.AnyWritable {
5875 const S = struct {
licenses.txt+7
......@@ -1,3 +1,10 @@
11MPL-2.0:
22= https://spdx.org/licenses/MPL-2.0
33- This
4
5MIT:
6= https://spdx.org/licenses/MIT
7- git https://github.com/nektro/zig-sys-linux
8
9Unspecified:
10- system_lib c
nio.zig+53
......@@ -1,4 +1,12 @@
11const std = @import("std");
2const builtin = @import("builtin");
3const extras = @import("extras");
4const sys_linux = @import("sys-linux");
5
6const sys = switch (builtin.target.os.tag) {
7 .linux => sys_linux,
8 else => unreachable,
9};
210
311pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {
412 return struct {
......@@ -132,6 +140,9 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {
132140 // pub fn write(self: Self, bytes: []const u8) Error!usize {
133141 // }
134142
143 // pub fn writev(self: Self, iovec: []const sys.struct_iovec) Error!usize {
144 // }
145
135146 pub fn writeAll(self: Self, bytes: []const u8) Error!void {
136147 var index: usize = 0;
137148 while (index != bytes.len) {
......@@ -139,6 +150,38 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {
139150 }
140151 }
141152
153 pub fn writevAll(self: Self, bytes: []const []const u8) Error!void {
154 var iovec: [1024]sys.struct_iovec = undefined;
155 for (bytes, 0..) |slice, i| iovec[i] = .{ .base = @constCast(slice.ptr), .len = slice.len };
156 var left: usize = 0;
157 for (bytes) |item| left += item.len;
158
159 while (left > 0) {
160 var written: usize = try self.writev(&iovec);
161 left -= written;
162 for (iovec[0..bytes.len], 0..) |vec, i| {
163 switch (std.math.order(written, vec.len)) {
164 .gt => {
165 written -= iovec[i].len;
166 iovec[i].len = 0;
167 continue;
168 },
169 .eq => {
170 written -= iovec[i].len;
171 iovec[i].len = 0;
172 break;
173 },
174 .lt => {
175 iovec[i].base += written;
176 iovec[i].len -= written;
177 written -= written;
178 },
179 }
180 }
181 std.debug.assert(written == 0);
182 }
183 }
184
142185 pub fn writeByteNTimes(self: Self, byte: u8, n: usize) Error!void {
143186 var bytes: [1024]u8 = undefined;
144187 @memset(bytes[0..], byte);
......@@ -150,6 +193,16 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type {
150193 }
151194 }
152195
196 pub fn writeNTimes(self: Self, input: []const u8, n: usize) Error!void {
197 var bytes: [1024][]const u8 = @splat(input);
198 var remaining: usize = n;
199 while (remaining > 0) {
200 const to_write = @min(remaining, bytes.len);
201 try writevAll(self, bytes[0..n]);
202 remaining -= to_write;
203 }
204 }
205
153206 pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void {
154207 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).Int.bits) + 7) / 8))]u8 = undefined;
155208 std.mem.writeInt(std.math.ByteAlignedInt(I), &bytes, value, endian);
zigmod.yml+1-1
......@@ -4,5 +4,5 @@ main: nio.zig
44license: MPL-2.0
55description: Nektro's IO, an alternative to std.io
66min_zig_version: 0.14.0
7min_zigmod_version: r96
87dependencies:
8 - src: git https://github.com/nektro/zig-sys-linux