mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-07 22:34:28 +00:00
add adaptToNewApi to std.Io.Reader as well
This commit is contained in:
parent
96a4e9b866
commit
093fe02b75
1 changed files with 26 additions and 0 deletions
|
|
@ -312,6 +312,32 @@ pub fn GenericReader(
|
||||||
const ptr: *const Context = @alignCast(@ptrCast(context));
|
const ptr: *const Context = @alignCast(@ptrCast(context));
|
||||||
return readFn(ptr.*, buffer);
|
return readFn(ptr.*, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper for bridging to the new `Reader` API while upgrading.
|
||||||
|
pub fn adaptToNewApi(self: *const Self) Adapter {
|
||||||
|
return .{
|
||||||
|
.derp_reader = self.*,
|
||||||
|
.new_interface = .{
|
||||||
|
.buffer = &.{},
|
||||||
|
.vtable = &.{ .stream = Adapter.stream },
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Adapter = struct {
|
||||||
|
derp_reader: Self,
|
||||||
|
new_interface: Reader,
|
||||||
|
err: ?Error = null,
|
||||||
|
|
||||||
|
fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize {
|
||||||
|
const a: *@This() = @fieldParentPtr("new_interface", r);
|
||||||
|
const buf = limit.slice(try w.writableSliceGreedy(1));
|
||||||
|
return a.derp_reader.read(buf) catch |err| {
|
||||||
|
a.err = err;
|
||||||
|
return error.ReadFailed;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue