authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-28 12:04:07 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-08-28 12:04:07 -07:00
log5fe04e96ca6e24364f87aff9a0d348bf2ccac768
treeedec1e521e9c7e5e226032eec64a69c584b259e4
parentc95791d223efb5113f17568fc9536d4e19a03900

AnyReadable: move readFn to vtable


2 files changed, 9 insertions(+), 7 deletions(-)

AnyReadable.zig+5-5
...@@ -3,12 +3,12 @@ const std = @import("std");...@@ -3,12 +3,12 @@ const std = @import("std");
3const nio = @import("./nio.zig");3const nio = @import("./nio.zig");
4const AnyReadable = @This();4const AnyReadable = @This();
55
6readFn: *const fn (*allowzero anyopaque, []u8) anyerror!usize,6vtable: *const struct {
7},
7state: *allowzero anyopaque,8state: *allowzero anyopaque,
89
9pub fn read(r: *AnyReadable, buffer: []u8) !usize {
10 return r.readFn(r.state, buffer);
11}
12
13pub const ReadError = anyerror;10pub const ReadError = anyerror;
14pub usingnamespace nio.Readable(@This(), ._var);11pub usingnamespace nio.Readable(@This(), ._var);
12pub fn read(r: *AnyReadable, buffer: []u8) !usize {
13 return r.vtable.read(r.state, buffer);
14}
fixed_buffer_stream.zig+4-2
...@@ -29,13 +29,15 @@ pub fn FixedBufferStream(comptime Buffer: type) type {...@@ -29,13 +29,15 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
2929
30 pub fn anyReadable(self: *Self) nio.AnyReadable {30 pub fn anyReadable(self: *Self) nio.AnyReadable {
31 const S = struct {31 const S = struct {
32 fn foo(s: *allowzero anyopaque, buffer: []u8) anyerror!usize {32 fn read(s: *allowzero anyopaque, buffer: []u8) anyerror!usize {
33 const fbs: *Self = @ptrCast(@alignCast(s));33 const fbs: *Self = @ptrCast(@alignCast(s));
34 return fbs.read(buffer);34 return fbs.read(buffer);
35 }35 }
36 };36 };
37 return .{37 return .{
38 .readFn = S.foo,38 .vtable = &.{
39 .read = S.read,
40 },
39 .state = @ptrCast(self),41 .state = @ptrCast(self),
40 };42 };
41 }43 }