Should handlers be functions only? #2321
-
I'm tying do a sort of handlers in an struct, how I can this with actix-web? Ex: struct A;
impl A {
async fn foo // omit
}
let a = A();
.route("/foo", web::get().to(a.foo) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
You want the handler to store the data of .to(|| async move { a.foo().await }) |
Beta Was this translation helpful? Give feedback.
-
Sorry, I made a mistake. This should work for async: .to(move || a.foo()) For non-async: .to(move || {
let response = a.foo();
async { response }
}) |
Beta Was this translation helpful? Give feedback.
-
See that the handler trait includes a In effect, it is pretty difficult to use struct methods as handlers in a useful way since you need to move the struct into the closure to use it. This is why all the teaching materials use functions; the framework is designed around it. So yes, handlers should be functions/closures. |
Beta Was this translation helpful? Give feedback.
Sorry, I made a mistake. This should work for async:
For non-async: