Commit 6e86910e19

John Benediktsson <mrjbq7@gmail.com>
2025-07-17 13:29:22
std.Io: Fix GenericReader.adaptToNewApi; add DeprecatedReader.adaptToNewApi (#24484)
1 parent c824030
Changed files (2)
lib/std/Io/DeprecatedReader.zig
@@ -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 Self = @This();
 const math = std.math;
lib/std/Io.zig
@@ -320,6 +320,8 @@ pub fn GenericReader(
                 .new_interface = .{
                     .buffer = &.{},
                     .vtable = &.{ .stream = Adapter.stream },
+                    .seek = 0,
+                    .end = 0,
                 },
             };
         }