| ... | @@ -312,6 +312,32 @@ pub fn GenericReader( | ... | @@ -312,6 +312,32 @@ pub fn GenericReader( |
| 312 | const ptr: *const Context = @alignCast(@ptrCast(context)); | 312 | const ptr: *const Context = @alignCast(@ptrCast(context)); |
| 313 | return readFn(ptr.*, buffer); | 313 | return readFn(ptr.*, buffer); |
| 314 | } | 314 | } |
| | 315 | |
| | 316 | /// Helper for bridging to the new `Reader` API while upgrading. |
| | 317 | pub fn adaptToNewApi(self: *const Self) Adapter { |
| | 318 | return .{ |
| | 319 | .derp_reader = self.*, |
| | 320 | .new_interface = .{ |
| | 321 | .buffer = &.{}, |
| | 322 | .vtable = &.{ .stream = Adapter.stream }, |
| | 323 | }, |
| | 324 | }; |
| | 325 | } |
| | 326 | |
| | 327 | pub const Adapter = struct { |
| | 328 | derp_reader: Self, |
| | 329 | new_interface: Reader, |
| | 330 | err: ?Error = null, |
| | 331 | |
| | 332 | fn stream(r: *Reader, w: *Writer, limit: Limit) Reader.StreamError!usize { |
| | 333 | const a: *@This() = @fieldParentPtr("new_interface", r); |
| | 334 | const buf = limit.slice(try w.writableSliceGreedy(1)); |
| | 335 | return a.derp_reader.read(buf) catch |err| { |
| | 336 | a.err = err; |
| | 337 | return error.ReadFailed; |
| | 338 | }; |
| | 339 | } |
| | 340 | }; |
| 315 | }; | 341 | }; |
| 316 | } | 342 | } |
| 317 | | 343 | |