| author | |
| committer | |
| log | edc2669d035d6c8d9ccef329a9232a6d9e41d7fd |
| tree | 0d78e0908e42e23faf8a2c4edcd6dbf7879569c0 |
| parent | 4896f0e2a57c715d9db27c2500d138899674a972 |
2 files changed, 33 insertions(+), 0 deletions(-)
AnyWritable.zig created+31| ... | @@ -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 | } | ||
nio.zig+2| ... | @@ -136,4 +136,6 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { | ... | @@ -136,4 +136,6 @@ pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { |
| 136 | 136 | ||
| 137 | pub const AnyReadable = @import("./AnyReadable.zig"); | 137 | pub const AnyReadable = @import("./AnyReadable.zig"); |
| 138 | 138 | ||
| 139 | pub const AnyWritable = @import("./AnyWritable.zig"); | ||
| 140 | |||
| 139 | pub const FixedBufferStream = @import("./fixed_buffer_stream.zig").FixedBufferStream; | 141 | pub const FixedBufferStream = @import("./fixed_buffer_stream.zig").FixedBufferStream; |