diff --git a/fixed_buffer_stream.zig b/fixed_buffer_stream.zig index 5ae53985b8ca34aa3dcf846e44be12a90923614c..68f9d5c6a5b01dd9f10c626ec55b0a70296011ba 100644 --- a/fixed_buffer_stream.zig +++ b/fixed_buffer_stream.zig @@ -1,4 +1,12 @@ const std = @import("std"); +const builtin = @import("builtin"); +const extras = @import("extras"); +const sys_linux = @import("sys-linux"); + +const sys = switch (builtin.target.os.tag) { + .linux => sys_linux, + else => unreachable, +}; const nio = @import("./nio.zig"); @@ -53,6 +61,15 @@ pub fn FixedBufferStream(comptime Buffer: type) type { if (n == 0) return error.NoSpaceLeft; return n; } + pub fn writev(self: *Self, iovec: []const sys.struct_iovec) WriteError!usize { + var count: usize = 0; + for (iovec) |vec| { + const actual = try write(self, vec.base[0..vec.len]); + count += actual; + if (actual < vec.len) break; + } + return count; + } pub fn anyWritable(self: *Self) nio.AnyWritable { const S = struct { diff --git a/licenses.txt b/licenses.txt index 38f772be6082b61f8cf89b7e597e98d02d63f6b8..7d95ad5b60a9684f6c0d101dec6490261bbbecd3 100644 --- a/licenses.txt +++ b/licenses.txt @@ -1,3 +1,10 @@ MPL-2.0: = https://spdx.org/licenses/MPL-2.0 - This + +MIT: += https://spdx.org/licenses/MIT +- git https://github.com/nektro/zig-sys-linux + +Unspecified: +- system_lib c diff --git a/nio.zig b/nio.zig index 01e04e491d12ce7309cfcc6323429430e1df4051..e07fd7b338e53dda2345a20c9c0ef133c893b9a3 100644 --- a/nio.zig +++ b/nio.zig @@ -1,4 +1,12 @@ const std = @import("std"); +const builtin = @import("builtin"); +const extras = @import("extras"); +const sys_linux = @import("sys-linux"); + +const sys = switch (builtin.target.os.tag) { + .linux => sys_linux, + else => unreachable, +}; pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { return struct { @@ -132,6 +140,9 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { // pub fn write(self: Self, bytes: []const u8) Error!usize { // } + // pub fn writev(self: Self, iovec: []const sys.struct_iovec) Error!usize { + // } + pub fn writeAll(self: Self, bytes: []const u8) Error!void { var index: usize = 0; while (index != bytes.len) { @@ -139,6 +150,38 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { } } + pub fn writevAll(self: Self, bytes: []const []const u8) Error!void { + var iovec: [1024]sys.struct_iovec = undefined; + for (bytes, 0..) |slice, i| iovec[i] = .{ .base = @constCast(slice.ptr), .len = slice.len }; + var left: usize = 0; + for (bytes) |item| left += item.len; + + while (left > 0) { + var written: usize = try self.writev(&iovec); + left -= written; + for (iovec[0..bytes.len], 0..) |vec, i| { + switch (std.math.order(written, vec.len)) { + .gt => { + written -= iovec[i].len; + iovec[i].len = 0; + continue; + }, + .eq => { + written -= iovec[i].len; + iovec[i].len = 0; + break; + }, + .lt => { + iovec[i].base += written; + iovec[i].len -= written; + written -= written; + }, + } + } + std.debug.assert(written == 0); + } + } + pub fn writeByteNTimes(self: Self, byte: u8, n: usize) Error!void { var bytes: [1024]u8 = undefined; @memset(bytes[0..], byte); @@ -150,6 +193,16 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { } } + pub fn writeNTimes(self: Self, input: []const u8, n: usize) Error!void { + var bytes: [1024][]const u8 = @splat(input); + var remaining: usize = n; + while (remaining > 0) { + const to_write = @min(remaining, bytes.len); + try writevAll(self, bytes[0..n]); + remaining -= to_write; + } + } + pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void { var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).Int.bits) + 7) / 8))]u8 = undefined; std.mem.writeInt(std.math.ByteAlignedInt(I), &bytes, value, endian); diff --git a/zigmod.yml b/zigmod.yml index 72ce4c9f9d0b21b03ff8fbee2afc8220845777da..cd59202bc7164ae9212908842609ddc5b6e4b9f9 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -4,5 +4,5 @@ main: nio.zig license: MPL-2.0 description: Nektro's IO, an alternative to std.io min_zig_version: 0.14.0 -min_zigmod_version: r96 dependencies: + - src: git https://github.com/nektro/zig-sys-linux