#I am trying to add this function arg to the closure but this error pops and idk what to do

1 messages · Page 1 of 1 (latest)

ocean sky
#

I even made it public ...

velvet mango
#

Please post code as text instead of images, so that it can be copied, pasted, read by screen readers, etc. The forum uses markdown -- encloose code in triple-back-tick fences.

ocean sky
#
    var router = try server.router(.{});
    router.get("/api/join", join(hub.joinPlayer), .{});

    std.debug.print("Listening on port 3000\n", .{});
    // blocks
    try server.listen();
}

fn join(joinPlayer: fn(Player) anyerror!void) fn(req: *httpz.Request, res: *httpz.Response) anyerror!void {
    return fn(req: *httpz.Request, res: *httpz.Response) anyerror!void {

    };
}
#

sorry it was for you to see the error too

velvet mango
#

FWIW, Zig doesn't have closures.

#

Sorry, the images for me are unreadable.

ocean sky
#
pub const Hub = struct {
    pool: std.Thread.Pool,
    arenaAllocator: std.heap.ArenaAllocator,
    allocator: std.mem.Allocator,
    players: usize = 0,
    mutex: std.Thread.Mutex = .{},
    const Self = @This();

    pub fn init(allocator: std.mem.Allocator) !*Hub {
        var arenaAllocator = std.heap.ArenaAllocator.init(allocator);
        const alloc = arenaAllocator.allocator();

        var hub: *Hub = undefined;
        hub.arenaAllocator = arenaAllocator;
        hub.allocator = alloc;
        hub.pool = undefined;
        try hub.pool.init(.{ .allocator = alloc });
        return hub;
    }

    pub fn Run(_: Self) void {
        while (true) {
            std.debug.print("Hub running\n", .{});
            std.time.sleep(1 * std.time.ns_per_s);
        }
    }

    pub fn joinPlayer(self: *Self, _: Player) !void {
        self.mutex.lock();
        defer self.mutex.unlock();
        self.players += 1;

        std.debug.print("Player count: {}", .{self.players});
    }

    pub fn deinit(self: *Self) void {
        self.arenaAllocator.deinit();
    }
};
jaunty girder
#

you need Hub.joinPlayer