Future returned by `Serve::serve()` is not `Send`
- Dominant language
- Rust
- Stars
- 3.7k
- Forks
- 230
- PR merge metrics
- No merged PRs in 30d
Description
I'm trying to write a function that starts a server with a clonable `Serve`.
```
async fn start_tarpc_server(
addr: SocketAddr,
serve: ServeFn,
) -> std::io::Result<()>
where
ServeFn:
tarpc::server::Serve + Send + 'static + Clone,
ServeFn::Req: Send + 'static + serde::de::DeserializeOwned,
ServeFn::Resp: Send + 'static + serde::ser::Serialize,
{
tokio::spawn(serve.serve());
Ok(())
}
```
That turns out to be impossible because I cannot call `tokio::spawn()` on the future returned by `Serve::serve`. The future is not guaranteed to be `Send`. Same applies to any future that wraps a `serve()` future.
This is a well-known problem caused by [using `aysnc fn` in public traits](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#where-the-gaps-lie). There is a solution described [in the same article](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#why-do-i-need-trait_variantmake-and-send-bounds). Can we apply the solution here?
Contributor guide
Assessment
This issue has not been assessed yet.