diff options
Diffstat (limited to 'src/http/handler/Interface.zig')
| -rw-r--r-- | src/http/handler/Interface.zig | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/http/handler/Interface.zig b/src/http/handler/Interface.zig new file mode 100644 index 0000000..2238424 --- /dev/null +++ b/src/http/handler/Interface.zig @@ -0,0 +1,44 @@ +const std = @import("std"); +const Response = @import("../Response.zig"); +const Route = @import("../Route.zig"); + +route: Route, +base: *const anyopaque, +handler: *const fn ( + *const anyopaque, + std.mem.Allocator, + std.mem.Allocator, + std.Io, + *std.http.Server.Request, +) Response, + +pub fn from(instance: anytype, route: Route) @This() { + const PtrT = @TypeOf(instance); + const func = struct { + pub fn handle( + base: *const anyopaque, + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + io: std.Io, + request: *std.http.Server.Request, + ) Response { + return @as(PtrT, @ptrCast(@alignCast(base))).handle(gpa, arena, io, request); + } + }.handle; + + return .{ + .base = @ptrCast(instance), + .handler = func, + .route = route, + }; +} + +pub fn handle( + self: *const @This(), + gpa: std.mem.Allocator, + arena: std.mem.Allocator, + io: std.Io, + request: *std.http.Server.Request, +) Response { + return self.handler(self.base, gpa, arena, io, request); +} |