authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-01-02 19:10:51 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-01-02 19:10:51 -08:00
logedc2669d035d6c8d9ccef329a9232a6d9e41d7fd
tree0d78e0908e42e23faf8a2c4edcd6dbf7879569c0
parent4896f0e2a57c715d9db27c2500d138899674a972

add AnyWritable


2 files changed, 33 insertions(+), 0 deletions(-)

AnyWritable.zig created+31
......@@ -0,0 +1,31 @@
1const std = @import("std");
2
3const nio = @import("./nio.zig");
4const AnyWritable = @This();
5
6vtable: *const struct {
7 write: *const fn (*allowzero anyopaque, []const u8) anyerror!usize,
8},
9state: *allowzero anyopaque,
10
11pub const WriteError = anyerror;
12pub usingnamespace nio.Writable(@This(), ._var);
13pub fn write(r: *AnyWritable, buffer: []u8) !usize {
14 return r.vtable.write(r.state, buffer);
15}
16pub fn anyWritable(r: AnyWritable) AnyWritable {
17 return r;
18}
19
20pub 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 {
136136
137137pub const AnyReadable = @import("./AnyReadable.zig");
138138
139pub const AnyWritable = @import("./AnyWritable.zig");
140
139141pub const FixedBufferStream = @import("./fixed_buffer_stream.zig").FixedBufferStream;