| ... | ... | @@ -0,0 +1,31 @@ |
| 1 | const std = @import("std"); |
| 2 | |
| 3 | const nio = @import("./nio.zig"); |
| 4 | const AnyWritable = @This(); |
| 5 | |
| 6 | vtable: *const struct { |
| 7 | write: *const fn (*allowzero anyopaque, []const u8) anyerror!usize, |
| 8 | }, |
| 9 | state: *allowzero anyopaque, |
| 10 | |
| 11 | pub const WriteError = anyerror; |
| 12 | pub usingnamespace nio.Writable(@This(), ._var); |
| 13 | pub fn write(r: *AnyWritable, buffer: []u8) !usize { |
| 14 | return r.vtable.write(r.state, buffer); |
| 15 | } |
| 16 | pub fn anyWritable(r: AnyWritable) AnyWritable { |
| 17 | return r; |
| 18 | } |
| 19 | |
| 20 | pub fn fromStd(writer_ptr: anytype) AnyWritable { |
| 21 | const S = struct { |
| 22 | fn _write(s: *allowzero anyopaque, buffer: []const u8) !usize { |
| 23 | const r: @TypeOf(writer_ptr) = @ptrCast(@alignCast(s)); |
| 24 | return r.write(buffer); |
| 25 | } |
| 26 | }; |
| 27 | return .{ |
| 28 | .vtable = &.{ .write = &S._write }, |
| 29 | .state = @constCast(@ptrCast(writer_ptr)), |
| 30 | }; |
| 31 | } |