| ... | @@ -27,6 +27,7 @@ pub fn init(reader: *Reader, limit: Limit, buffer: []u8) Limited { | ... | @@ -27,6 +27,7 @@ pub fn init(reader: *Reader, limit: Limit, buffer: []u8) Limited { |
| 27 | | 27 | |
| 28 | fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize { | 28 | fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize { |
| 29 | const l: *Limited = @fieldParentPtr("interface", r); | 29 | const l: *Limited = @fieldParentPtr("interface", r); |
| | 30 | if (l.remaining == .nothing) return error.EndOfStream; |
| 30 | const combined_limit = limit.min(l.remaining); | 31 | const combined_limit = limit.min(l.remaining); |
| 31 | const n = try l.unlimited.stream(w, combined_limit); | 32 | const n = try l.unlimited.stream(w, combined_limit); |
| 32 | l.remaining = l.remaining.subtract(n).?; | 33 | l.remaining = l.remaining.subtract(n).?; |
| ... | @@ -51,8 +52,51 @@ test stream { | ... | @@ -51,8 +52,51 @@ test stream { |
| 51 | | 52 | |
| 52 | fn discard(r: *Reader, limit: Limit) Reader.Error!usize { | 53 | fn discard(r: *Reader, limit: Limit) Reader.Error!usize { |
| 53 | const l: *Limited = @fieldParentPtr("interface", r); | 54 | const l: *Limited = @fieldParentPtr("interface", r); |
| | 55 | if (l.remaining == .nothing) return error.EndOfStream; |
| 54 | const combined_limit = limit.min(l.remaining); | 56 | const combined_limit = limit.min(l.remaining); |
| 55 | const n = try l.unlimited.discard(combined_limit); | 57 | const n = try l.unlimited.discard(combined_limit); |
| 56 | l.remaining = l.remaining.subtract(n).?; | 58 | l.remaining = l.remaining.subtract(n).?; |
| 57 | return n; | 59 | return n; |
| 58 | } | 60 | } |
| | 61 | |
| | 62 | test "end of stream, read, hit limit exactly" { |
| | 63 | var f: Reader = .fixed("i'm dying"); |
| | 64 | var l = f.limited(.limited(4), &.{}); |
| | 65 | const r = &l.interface; |
| | 66 | |
| | 67 | var buf: [2]u8 = undefined; |
| | 68 | try r.readSliceAll(&buf); |
| | 69 | try r.readSliceAll(&buf); |
| | 70 | try std.testing.expectError(error.EndOfStream, l.interface.readSliceAll(&buf)); |
| | 71 | } |
| | 72 | |
| | 73 | test "end of stream, read, hit limit after partial read" { |
| | 74 | var f: Reader = .fixed("i'm dying"); |
| | 75 | var l = f.limited(.limited(5), &.{}); |
| | 76 | const r = &l.interface; |
| | 77 | |
| | 78 | var buf: [2]u8 = undefined; |
| | 79 | try r.readSliceAll(&buf); |
| | 80 | try r.readSliceAll(&buf); |
| | 81 | try std.testing.expectError(error.EndOfStream, l.interface.readSliceAll(&buf)); |
| | 82 | } |
| | 83 | |
| | 84 | test "end of stream, discard, hit limit exactly" { |
| | 85 | var f: Reader = .fixed("i'm dying"); |
| | 86 | var l = f.limited(.limited(4), &.{}); |
| | 87 | const r = &l.interface; |
| | 88 | |
| | 89 | try r.discardAll(2); |
| | 90 | try r.discardAll(2); |
| | 91 | try std.testing.expectError(error.EndOfStream, l.interface.discardAll(2)); |
| | 92 | } |
| | 93 | |
| | 94 | test "end of stream, discard, hit limit after partial read" { |
| | 95 | var f: Reader = .fixed("i'm dying"); |
| | 96 | var l = f.limited(.limited(5), &.{}); |
| | 97 | const r = &l.interface; |
| | 98 | |
| | 99 | try r.discardAll(2); |
| | 100 | try r.discardAll(2); |
| | 101 | try std.testing.expectError(error.EndOfStream, l.interface.discardAll(2)); |
| | 102 | } |