#how to allow for different arguments in my dispatcher

1 messages · Page 1 of 1 (latest)

dim eagle
#

I currently have something like that, but I would like to basically allow for custom arguments, and then when dispatching I would try to give them what they are asking for or fail.
Basically something like axum in rust did if you are familiar with that framework.

pub const dispatcher: std.StaticStringMap(*const fn (
        self: *UserContext,
        arena: std.mem.Allocator,
        rest: std.StringHashMap([]const u8),
    ) anyerror!void
) = .initComptime(.{
    .{ "addon", handleAddon },
    .{ "init", inits.handler },
    .{ "emo", emotion.handler },

For example handleAddon should only have 1 argument - *UserContext

lunar knot
#

I thinks you can use and *anyopaque (be careful of the lifetime or allocate it in the heap)
And then you can do a @ptrcast

#

The problem is you lose the type cheking

#

you may use different function to add the args and then ptrcast to anyopaque.

rain rivet
dim eagle
#

Thank you all for your input, I will take a look and implement something for myself.

dim eagle
#

@rain rivet your solution is exactly what I needed, thank you again. If you are familiar with axum in rust, is this basically the same way they achieved dependency injection?

rain rivet
#

not sure how they do it in axum, but at least this is how bevy does it. But of course it's lot more unreadable in rust