| ... | @@ -20,7 +20,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -20,7 +20,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 20 | /// 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 |
| 21 | /// condition. | 21 | /// condition. |
| 22 | pub fn readAll(self: Self, buffer: []u8) Error!usize { | 22 | pub fn readAll(self: Self, buffer: []u8) Error!usize { |
| 23 | return self.readAtLeast(buffer, buffer.len); | 23 | return readAtLeast(self, buffer, buffer.len); |
| 24 | } | 24 | } |
| 25 | | 25 | |
| 26 | /// Returns the number of bytes read, calling the underlying read | 26 | /// Returns the number of bytes read, calling the underlying read |
| ... | @@ -41,14 +41,14 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -41,14 +41,14 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 41 | | 41 | |
| 42 | /// 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. |
| 43 | pub fn readNoEof(self: Self, buf: []u8) (Error || error{EndOfStream})!void { | 43 | pub fn readNoEof(self: Self, buf: []u8) (Error || error{EndOfStream})!void { |
| 44 | const amt_read = try self.readAll(buf); | 44 | const amt_read = try readAll(self, buf); |
| 45 | if (amt_read < buf.len) return error.EndOfStream; | 45 | if (amt_read < buf.len) return error.EndOfStream; |
| 46 | } | 46 | } |
| 47 | | 47 | |
| 48 | /// Appends to the `std.ArrayList` contents by reading from the stream until end of stream is found. | 48 | /// Appends to the `std.ArrayList` contents by reading from the stream until end of stream is found. |
| 49 | /// If the number of bytes appended would exceed `max_append_size`, `error.StreamTooLong` is returned and the `std.ArrayList` has exactly `max_append_size` bytes appended. | 49 | /// If the number of bytes appended would exceed `max_append_size`, `error.StreamTooLong` is returned and the `std.ArrayList` has exactly `max_append_size` bytes appended. |
| 50 | fn readAllArrayList(self: Self, array_list: *std.ArrayList(u8), max_append_size: usize) !void { | 50 | fn readAllArrayList(self: Self, array_list: *std.ArrayList(u8), max_append_size: usize) !void { |
| 51 | return self.readAllArrayListAligned(null, array_list, max_append_size); | 51 | return readAllArrayListAligned(self, null, array_list, max_append_size); |
| 52 | } | 52 | } |
| 53 | | 53 | |
| 54 | fn readAllArrayListAligned(self: Self, comptime alignment: ?u29, array_list: *std.ArrayListAligned(u8, alignment), max_append_size: usize) !void { | 54 | fn readAllArrayListAligned(self: Self, comptime alignment: ?u29, array_list: *std.ArrayListAligned(u8, alignment), max_append_size: usize) !void { |
| ... | @@ -58,7 +58,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -58,7 +58,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 58 | while (true) { | 58 | while (true) { |
| 59 | array_list.expandToCapacity(); | 59 | array_list.expandToCapacity(); |
| 60 | const dest_slice = array_list.items[start_index..]; | 60 | const dest_slice = array_list.items[start_index..]; |
| 61 | const bytes_read = try self.readAll(dest_slice); | 61 | const bytes_read = try readAll(self, dest_slice); |
| 62 | start_index += bytes_read; | 62 | start_index += bytes_read; |
| 63 | | 63 | |
| 64 | if (start_index - original_len > max_append_size) { | 64 | if (start_index - original_len > max_append_size) { |
| ... | @@ -83,7 +83,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -83,7 +83,7 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 83 | pub fn readAllAlloc(self: Self, allocator: std.mem.Allocator, max_size: usize) ![]u8 { | 83 | pub fn readAllAlloc(self: Self, allocator: std.mem.Allocator, max_size: usize) ![]u8 { |
| 84 | var array_list = std.ArrayList(u8).init(allocator); | 84 | var array_list = std.ArrayList(u8).init(allocator); |
| 85 | defer array_list.deinit(); | 85 | defer array_list.deinit(); |
| 86 | try self.readAllArrayList(&array_list, max_size); | 86 | try readAllArrayList(self, &array_list, max_size); |
| 87 | return try array_list.toOwnedSlice(); | 87 | return try array_list.toOwnedSlice(); |
| 88 | } | 88 | } |
| 89 | }; | 89 | }; |
| ... | @@ -115,7 +115,7 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -115,7 +115,7 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 115 | var remaining: usize = n; | 115 | var remaining: usize = n; |
| 116 | while (remaining > 0) { | 116 | while (remaining > 0) { |
| 117 | const to_write = @min(remaining, bytes.len); | 117 | const to_write = @min(remaining, bytes.len); |
| 118 | try self.writeAll(bytes[0..to_write]); | 118 | try writeAll(self, bytes[0..to_write]); |
| 119 | remaining -= to_write; | 119 | remaining -= to_write; |
| 120 | } | 120 | } |
| 121 | } | 121 | } |
| ... | @@ -123,13 +123,13 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -123,13 +123,13 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 123 | pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void { | 123 | pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void { |
| 124 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).Int.bits) + 7) / 8))]u8 = undefined; | 124 | var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).Int.bits) + 7) / 8))]u8 = undefined; |
| 125 | std.mem.writeInt(std.math.ByteAlignedInt(I), &bytes, value, endian); | 125 | std.mem.writeInt(std.math.ByteAlignedInt(I), &bytes, value, endian); |
| 126 | return self.writeAll(&bytes); | 126 | return writeAll(self, &bytes); |
| 127 | } | 127 | } |
| 128 | | 128 | |
| 129 | pub fn writeStruct(self: Self, value: anytype) Error!void { | 129 | pub fn writeStruct(self: Self, value: anytype) Error!void { |
| 130 | // Only extern and packed structs have defined in-memory layout. | 130 | // Only extern and packed structs have defined in-memory layout. |
| 131 | comptime std.debug.assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto); | 131 | comptime std.debug.assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto); |
| 132 | return self.writeAll(std.mem.asBytes(&value)); | 132 | return writeAll(self, std.mem.asBytes(&value)); |
| 133 | } | 133 | } |
| 134 | }; | 134 | }; |
| 135 | } | 135 | } |