mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 14:25:16 +00:00
std: Fix Io.Reader.Limited and add test
This commit is contained in:
parent
2cda4cfb39
commit
61eff7b6dd
@ -868,6 +868,7 @@ pub fn PollFiles(comptime StreamEnum: type) type {
|
||||
|
||||
test {
|
||||
_ = Reader;
|
||||
_ = Reader.Limited;
|
||||
_ = Writer;
|
||||
_ = @import("Io/bit_reader.zig");
|
||||
_ = @import("Io/bit_writer.zig");
|
||||
|
||||
@ -99,7 +99,7 @@ pub const ShortError = error{
|
||||
|
||||
pub const failing: Reader = .{
|
||||
.vtable = &.{
|
||||
.read = failingStream,
|
||||
.stream = failingStream,
|
||||
.discard = failingDiscard,
|
||||
},
|
||||
.buffer = &.{},
|
||||
|
||||
@ -25,18 +25,34 @@ pub fn init(reader: *Reader, limit: Limit, buffer: []u8) Limited {
|
||||
};
|
||||
}
|
||||
|
||||
fn stream(context: ?*anyopaque, w: *Writer, limit: Limit) Reader.StreamError!usize {
|
||||
const l: *Limited = @alignCast(@ptrCast(context));
|
||||
fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize {
|
||||
const l: *Limited = @fieldParentPtr("interface", r);
|
||||
const combined_limit = limit.min(l.remaining);
|
||||
const n = try l.unlimited_reader.read(w, combined_limit);
|
||||
const n = try l.unlimited.stream(w, combined_limit);
|
||||
l.remaining = l.remaining.subtract(n).?;
|
||||
return n;
|
||||
}
|
||||
|
||||
fn discard(context: ?*anyopaque, limit: Limit) Reader.Error!usize {
|
||||
const l: *Limited = @alignCast(@ptrCast(context));
|
||||
test stream {
|
||||
var orig_buf: [10]u8 = undefined;
|
||||
@memcpy(&orig_buf, "test bytes");
|
||||
var fixed: std.Io.Reader = .fixed(&orig_buf);
|
||||
|
||||
var limit_buf: [1]u8 = undefined;
|
||||
var limited: std.Io.Reader.Limited = .init(&fixed, @enumFromInt(4), &limit_buf);
|
||||
|
||||
var result_buf: [10]u8 = undefined;
|
||||
var fixed_writer: std.Io.Writer = .fixed(&result_buf);
|
||||
const streamed = try limited.interface.stream(&fixed_writer, @enumFromInt(7));
|
||||
|
||||
try std.testing.expect(streamed == 4);
|
||||
try std.testing.expectEqualStrings("test", result_buf[0..streamed]);
|
||||
}
|
||||
|
||||
fn discard(r: *Reader, limit: Limit) Reader.Error!usize {
|
||||
const l: *Limited = @fieldParentPtr("interface", r);
|
||||
const combined_limit = limit.min(l.remaining);
|
||||
const n = try l.unlimited_reader.discard(combined_limit);
|
||||
const n = try l.unlimited.discard(combined_limit);
|
||||
l.remaining = l.remaining.subtract(n).?;
|
||||
return n;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user