| ... | @@ -372,6 +372,34 @@ pub fn discard(self: Self) anyerror!u64 { | ... | @@ -372,6 +372,34 @@ pub fn discard(self: Self) anyerror!u64 { |
| 372 | } | 372 | } |
| 373 | } | 373 | } |
| 374 | | 374 | |
| | 375 | /// Helper for bridging to the new `Reader` API while upgrading. |
| | 376 | pub fn adaptToNewApi(self: *const Self) Adapter { |
| | 377 | return .{ |
| | 378 | .derp_reader = self.*, |
| | 379 | .new_interface = .{ |
| | 380 | .buffer = &.{}, |
| | 381 | .vtable = &.{ .stream = Adapter.stream }, |
| | 382 | .seek = 0, |
| | 383 | .end = 0, |
| | 384 | }, |
| | 385 | }; |
| | 386 | } |
| | 387 | |
| | 388 | pub const Adapter = struct { |
| | 389 | derp_reader: Self, |
| | 390 | new_interface: std.io.Reader, |
| | 391 | err: ?Error = null, |
| | 392 | |
| | 393 | fn stream(r: *std.io.Reader, w: *std.io.Writer, limit: std.io.Limit) std.io.Reader.StreamError!usize { |
| | 394 | const a: *@This() = @alignCast(@fieldParentPtr("new_interface", r)); |
| | 395 | const buf = limit.slice(try w.writableSliceGreedy(1)); |
| | 396 | return a.derp_reader.read(buf) catch |err| { |
| | 397 | a.err = err; |
| | 398 | return error.ReadFailed; |
| | 399 | }; |
| | 400 | } |
| | 401 | }; |
| | 402 | |
| 375 | const std = @import("../std.zig"); | 403 | const std = @import("../std.zig"); |
| 376 | const Self = @This(); | 404 | const Self = @This(); |
| 377 | const math = std.math; | 405 | const math = std.math; |