-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ? in routes #431
Comments
I believe what you can do to work around this limitation is something like this: fn homepage(state: &State) -> search::Result<Response<Body>> {
let a = expr()?;
return a;
}
fn wrap_handler<F>(callback: F, state: State) -> Pin<Box<HandlerFuture>>
where
F : FnOnce(&State) -> Response<Body>
{
let res = match callback(&state) {
Ok(res) => future::ok((state, res)).boxed(),
Err(err) => future::err((state, res.into_handler_error())).boxed()
};
}
fn make_router() -> search::Result<Router> {
let state = IndexState::new()?;
let middleware = StateMiddleware::new(state);
let pipeline = single_middleware(middleware);
let (chain, pipelines) = single_pipeline(pipeline);
Ok(build_router(chain, pipelines, |route| {
route.get("/").to(|state| wrap_handler(homepage, state));
}))
} This allows you some usage of the I am not one of the maintainers but I still believe that there is a good reason for gotham to require the Also you might want to take a look at the |
Historically, the future combinators were not able to express borrowing very well. The solution of passing things by move worked out pretty well in that era. The language is now capable of expressing lifetimes of things borrowed across await points, so there are a bunch of wrappers/macros floating about in the ecosystem that allow you to receive When I was implementing I've now stumbled across this problem in a second project, so I'm going to try fixing it. This will probably involve patches to the compiler and take many months though. |
@alsuren I also tried to fix it but failed. What I attempted so far while writing gotham-restful were two things:
As of right now I believe there is no good solution, so I generate a compile-time error if the user code tries to use async together with the state. However, I'd definitely appreciate a better solution for this. |
I am wondering how does actix handle this situation. Or doest it at all? In any case using ? operator would be convenient. Maybe it is impossible in all cases, but I think we can have few wrappers. |
Hi, I was playing around with this. I want to write handlers in the following fashion: pub async fn handler(state: &mut State) -> Result<&'static str, HandlerError> {
let b = body::to_bytes(Body::take_from(state)).await.map_err(bad_request)?;
let s = std::str::from_utf8(&b).map_err(bad_request)?;
match s.find("text") {
Some(_) => Ok("Found"),
None => Ok("Bye!"),
}
} So I can use It turns out, that this is possible to achieve with the following code, when using lambdas route.get("/example").to(|mut st: State| {
async move {
let result = handler(&mut st).await;
to_handler_result(st, result)
}
.boxed()
}); So far I failed to write |
Thanks to the solution on the rust forum I am finally able to have this work also for The trick is to define helper trait. Then the code above will work like this route.get("/example").to(SimpleAsyncHandler(handler)); So I think you can do @alsuren, can you explain what was the problem with |
@technic Can you share the code for self.to_new_handler(move || Ok(move |s: State| {
async move {
let res = handler(&s).await;
to_handler_result(s, res)
}.boxed()
})); gives me the following error:
So the compiler is asking for |
For anybody referring to this issue, currently a handler wrapper would be required to handle this. I think moving forward, as the Rust ecosystem has been developing, it would be worth discussing |
Ah, didn't realize those were close. This could be rolled into 0.5 then, looking at those PRs. Would you agree? |
Yes, I think this could be included in 0.5 |
Hi, I want to write code like this:
But the
IntoHandlerFuture
trait does not get auto-impld forhomepage
. Therefore, I cant use?
in my function body.I tried adding the following into gotham:
But I cant
impl IntoHandlerFuture
for my Error type:But there was no way to get the state to return in
into_handler_future
.So I ask, can the API be changed to allow such code? Thank you.
The text was updated successfully, but these errors were encountered: