authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 21:10:44 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-24 21:10:44 -07:00
log751cbb34ca5562ec8c148b4d77707c9c011ad5aa
tree98b6238736919f353c8fd1e0b17c90a2e0f02ff5
parentce531ba7a65f4b5eb037e893270cd07c42e88802

make the self param configurable


3 files changed, 13 insertions(+), 7 deletions(-)

AnyReadable.zig+1-1
...@@ -11,4 +11,4 @@ pub fn read(r: *AnyReadable, buffer: []u8) !usize {...@@ -11,4 +11,4 @@ pub fn read(r: *AnyReadable, buffer: []u8) !usize {
11}11}
1212
13pub const ReadError = anyerror;13pub const ReadError = anyerror;
14pub usingnamespace nio.Readable(@This());14pub usingnamespace nio.Readable(@This(), ._var);
fixed_buffer_stream.zig+1-1
...@@ -18,7 +18,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type {...@@ -18,7 +18,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
18 }18 }
1919
20 pub const ReadError = error{};20 pub const ReadError = error{};
21 pub usingnamespace nio.Readable(@This());21 pub usingnamespace nio.Readable(@This(), ._var);
22 pub fn read(self: *Self, dest: []u8) ReadError!usize {22 pub fn read(self: *Self, dest: []u8) ReadError!usize {
23 const size = @min(dest.len, self.buffer.len - self.pos);23 const size = @min(dest.len, self.buffer.len - self.pos);
24 const end = self.pos + size;24 const end = self.pos + size;
nio.zig+11-5
...@@ -1,19 +1,25 @@...@@ -1,19 +1,25 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn Readable(T: type) type {3pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type {
4 return struct {4 return struct {
5 const Error = T.ReadError;5 const Error = T.ReadError;
66
7 const Self = switch (this_kind) {
8 ._var => *T,
9 ._const => *const T,
10 ._bare => T,
11 };
12
7 /// Returns the number of bytes read. It may be less than buffer.len.13 /// Returns the number of bytes read. It may be less than buffer.len.
8 /// If the number of bytes read is 0, it means end of stream.14 /// If the number of bytes read is 0, it means end of stream.
9 /// End of stream is not an error condition.15 /// End of stream is not an error condition.
10 // pub fn read(self: *T, buffer: []u8) Error!usize {16 // pub fn read(self: Self, buffer: []u8) Error!usize {
11 // }17 // }
1218
13 /// Returns the number of bytes read. If the number read is smaller than `buffer.len`, it19 /// Returns the number of bytes read. If the number read is smaller than `buffer.len`, it
14 /// means the stream reached the end. Reaching the end of a stream is not an error20 /// means the stream reached the end. Reaching the end of a stream is not an error
15 /// condition.21 /// condition.
16 pub fn readAll(self: *T, buffer: []u8) Error!usize {22 pub fn readAll(self: Self, buffer: []u8) Error!usize {
17 return self.readAtLeast(buffer, buffer.len);23 return self.readAtLeast(buffer, buffer.len);
18 }24 }
1925
...@@ -22,7 +28,7 @@ pub fn Readable(T: type) type {...@@ -22,7 +28,7 @@ pub fn Readable(T: type) type {
22 /// `len` bytes filled. If the number read is less than `len` it means28 /// `len` bytes filled. If the number read is less than `len` it means
23 /// the stream reached the end. Reaching the end of the stream is not29 /// the stream reached the end. Reaching the end of the stream is not
24 /// an error condition.30 /// an error condition.
25 pub fn readAtLeast(self: *T, buffer: []u8, len: usize) Error!usize {31 pub fn readAtLeast(self: Self, buffer: []u8, len: usize) Error!usize {
26 std.debug.assert(len <= buffer.len);32 std.debug.assert(len <= buffer.len);
27 var index: usize = 0;33 var index: usize = 0;
28 while (index < len) {34 while (index < len) {
...@@ -34,7 +40,7 @@ pub fn Readable(T: type) type {...@@ -34,7 +40,7 @@ pub fn Readable(T: type) type {
34 }40 }
3541
36 /// If the number read would be smaller than `buf.len`, `error.EndOfStream` is returned instead.42 /// If the number read would be smaller than `buf.len`, `error.EndOfStream` is returned instead.
37 pub fn readNoEof(self: *T, buf: []u8) (Error || error{EndOfStream})!void {43 pub fn readNoEof(self: Self, buf: []u8) (Error || error{EndOfStream})!void {
38 const amt_read = try self.readAll(buf);44 const amt_read = try self.readAll(buf);
39 if (amt_read < buf.len) return error.EndOfStream;45 if (amt_read < buf.len) return error.EndOfStream;
40 }46 }