| ... | @@ -1,19 +1,25 @@ | ... | @@ -1,19 +1,25 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | | 2 | |
| 3 | pub fn Readable(T: type) type { | 3 | pub 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; |
| 6 | | 6 | |
| | 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 | // } |
| 12 | | 18 | |
| 13 | /// Returns the number of bytes read. If the number read is smaller than `buffer.len`, it | 19 | /// 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 error | 20 | /// 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 | } |
| 19 | | 25 | |
| ... | @@ -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 means | 28 | /// `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 not | 29 | /// 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 | } |
| 35 | | 41 | |
| 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 | } |