I'm trying to split my code up into multiple crates to organize things better. I'm using hyper for a local server that listens on multiple ports. When everything was in the one crate, I didn't have a problem. Now that I'm trying to split things apart, I've hit a problem trying to pass an async fn pointer to another function. Here is what I have so far:
This function doesn't compile, and the compiler suggests adding a 'static bound to the generic in addition to the Send bound (which it also suggested).
However if I add that 'static bound I then get a different error pointing to the closure passed to make_service_fn complaining that callback doesn't live long enough, and that it needs to be 'static.
I don't think I want to add the 'static bound in this case, as the requests and responses won't have static lifetimes.
Would you mind explaining your answer a little bit?
Why is the 'static bound needed?
I still don't quite understand async blocks. I do know that blocks are expressions in rust. Does making an async block just wrap whatever value the block would normally return in a Future?
The 'static bound is needed to ensure that your future is safe to move across threads without keeping track of cross-thread references.
As for async blocks, you can think of them as a lazily evaluated value. The contents of the block are executed once the block is .awaited, but not before then. If it's passed around in a variable, then the contents have not yet begun executing.