zigzap / zap

blazingly fast backends in zig
MIT License
1.98k stars 71 forks source link

RequestHandler doesn't work with multiple invocations where self is the same type #83

Closed efjimm closed 3 months ago

efjimm commented 3 months ago

RequestHandler creates a closure over the passed in parameters by defining a new type and assigning to decls in that type. This partially works because Zig creates a new instantiation of the function for each different type passed as the anytype argument. However, when the function has already been instantiated for the type passed, the type declaration inside the function is re-used, and the decls get overwritten. This affects the simple_router example in the repo, where /geta, /getb, and /inca all share the same self type and thus share the same request handler.

zap.Router will have to find another way to pass the self argument to the routing functions.

renerocksai commented 3 months ago

Try to run the simple_router example and you'll see that it works. Why do you open an issue when you haven't even tried?

Your assumptions are wrong. When you look at the code, you will see that the opaque type is created per invocation and its init function is called.

efjimm commented 3 months ago

My apologies - this issue only exists on the 0.12 branch. I didn't believe the compilers handling of this had changed between versions so I didn't try 0.11. Given that the 0.12 release of Zig imminent this will still need to be addressed eventually.

renerocksai commented 3 months ago

Ah, I see, thanks for pointing that out. Looking at the code, I think it's a good thing it has to be changed, exactly because of the ambiguity about the memory of the opaqe type's vars. I also found other things in the Router that are worth rewriting.