You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The string part is straight forward, but the handler part will need to be a bit different in lunatic than in other Rust web frameworks. Axum for example uses a concept of Extractors.
We can't copy this approach, because lunatic can't capture arbitrary closures in rust, only fn functions. We still can pass in non-capturing closures and they will be correctly coerced into functions. However, we can't use the type system to implement traits for fn() functions. To implement the extractor approach we need to be able to do it for functions with different numbers of incoming arguments. E.g.:
fnmain(){// This doesn't work in rust!!!// error[E0277]: the trait bound `fn(()) {handler}: Function` is not satisfiedrouter_add("/", handler);}fnrouter_add<H>(route:&str,handler:H)whereH:Function,{// Implementation ...}fnhandler(empty_extractor:()){// Implementation}traitFunction{}implFunctionforfn(){}implFunctionforfn(_x:()){}
We can make this compile but need to change the router_add call to:
router_add("/", handler asfn(()));
What is not great. I'm not sure why the function is not casted by rust into the correct type, but I don't see any effort going on in fixing this. The general recommendation is just to use the Fn*() traits instead, that we can't because we need non-capturing guarantees. And for a bunch of other reasons, we can't determine if the closure is zero-sized (non-capturing), so there is no workaround for this.
Solution
I would just for now stick to having oneRequest argument that every handler gets, instead of this fancy extractor tricks. E.g.
fnhandler(request:Request){// Implementation}
Then the Request can be used to access all the data that would be "extracted" through the extractor.
The text was updated successfully, but these errors were encountered:
bkolobara
changed the title
The router should take a string and a function handling the request.
The router should take a string and a function handling the request
May 11, 2022
The string part is straight forward, but the handler part will need to be a bit different in lunatic than in other Rust web frameworks. Axum for example uses a concept of
Extractors
.We can't copy this approach, because lunatic can't capture arbitrary closures in rust, only
fn
functions. We still can pass in non-capturing closures and they will be correctly coerced into functions. However, we can't use the type system to implement traits forfn()
functions. To implement the extractor approach we need to be able to do it for functions with different numbers of incoming arguments. E.g.:We can make this compile but need to change the
router_add
call to:What is not great. I'm not sure why the function is not casted by rust into the correct type, but I don't see any effort going on in fixing this. The general recommendation is just to use the
Fn*()
traits instead, that we can't because we need non-capturing guarantees. And for a bunch of other reasons, we can't determine if the closure is zero-sized (non-capturing), so there is no workaround for this.Solution
I would just for now stick to having one
Request
argument that every handler gets, instead of this fancy extractor tricks. E.g.Then the
Request
can be used to access all the data that would be "extracted" through the extractor.The text was updated successfully, but these errors were encountered: