std.Io: Fix GenericReader.adaptToNewApi; add DeprecatedReader.adaptToNewApi (#24484)

This commit is contained in:
John Benediktsson 2025-07-17 04:29:22 -07:00 committed by GitHub
parent c82403020d
commit 6e86910e19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -320,6 +320,8 @@ pub fn GenericReader(
.new_interface = .{ .new_interface = .{
.buffer = &.{}, .buffer = &.{},
.vtable = &.{ .stream = Adapter.stream }, .vtable = &.{ .stream = Adapter.stream },
.seek = 0,
.end = 0,
}, },
}; };
} }

View File

@ -372,6 +372,34 @@ pub fn discard(self: Self) anyerror!u64 {
} }
} }
/// 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 },
.seek = 0,
.end = 0,
},
};
}
pub const Adapter = struct {
derp_reader: Self,
new_interface: std.io.Reader,
err: ?Error = null,
fn stream(r: *std.io.Reader, w: *std.io.Writer, limit: std.io.Limit) std.io.Reader.StreamError!usize {
const a: *@This() = @alignCast(@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;
};
}
};
const std = @import("../std.zig"); const std = @import("../std.zig");
const Self = @This(); const Self = @This();
const math = std.math; const math = std.math;