Skip to content

Commit 61eff7b

Browse files
mochalinsandrewrk
authored andcommitted
std: Fix Io.Reader.Limited and add test
1 parent 2cda4cf commit 61eff7b

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

lib/std/Io.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ pub fn PollFiles(comptime StreamEnum: type) type {
868868

869869
test {
870870
_ = Reader;
871+
_ = Reader.Limited;
871872
_ = Writer;
872873
_ = @import("Io/bit_reader.zig");
873874
_ = @import("Io/bit_writer.zig");

lib/std/Io/Reader.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub const ShortError = error{
9999

100100
pub const failing: Reader = .{
101101
.vtable = &.{
102-
.read = failingStream,
102+
.stream = failingStream,
103103
.discard = failingDiscard,
104104
},
105105
.buffer = &.{},

lib/std/Io/Reader/Limited.zig

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,34 @@ pub fn init(reader: *Reader, limit: Limit, buffer: []u8) Limited {
2525
};
2626
}
2727

28-
fn stream(context: ?*anyopaque, w: *Writer, limit: Limit) Reader.StreamError!usize {
29-
const l: *Limited = @alignCast(@ptrCast(context));
28+
fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize {
29+
const l: *Limited = @fieldParentPtr("interface", r);
3030
const combined_limit = limit.min(l.remaining);
31-
const n = try l.unlimited_reader.read(w, combined_limit);
31+
const n = try l.unlimited.stream(w, combined_limit);
3232
l.remaining = l.remaining.subtract(n).?;
3333
return n;
3434
}
3535

36-
fn discard(context: ?*anyopaque, limit: Limit) Reader.Error!usize {
37-
const l: *Limited = @alignCast(@ptrCast(context));
36+
test stream {
37+
var orig_buf: [10]u8 = undefined;
38+
@memcpy(&orig_buf, "test bytes");
39+
var fixed: std.Io.Reader = .fixed(&orig_buf);
40+
41+
var limit_buf: [1]u8 = undefined;
42+
var limited: std.Io.Reader.Limited = .init(&fixed, @enumFromInt(4), &limit_buf);
43+
44+
var result_buf: [10]u8 = undefined;
45+
var fixed_writer: std.Io.Writer = .fixed(&result_buf);
46+
const streamed = try limited.interface.stream(&fixed_writer, @enumFromInt(7));
47+
48+
try std.testing.expect(streamed == 4);
49+
try std.testing.expectEqualStrings("test", result_buf[0..streamed]);
50+
}
51+
52+
fn discard(r: *Reader, limit: Limit) Reader.Error!usize {
53+
const l: *Limited = @fieldParentPtr("interface", r);
3854
const combined_limit = limit.min(l.remaining);
39-
const n = try l.unlimited_reader.discard(combined_limit);
55+
const n = try l.unlimited.discard(combined_limit);
4056
l.remaining = l.remaining.subtract(n).?;
4157
return n;
4258
}

0 commit comments

Comments
 (0)