I'm trying to make an abstract http client where one wraps am axum::Router
.
I can't use a &mut self
in the Client::get
method as axum::Router
implements Send
and !Sync
.
Does anyone have a way that I can use &mut self
in an async fn
when the struct is Send + !Sync
?
#[derive(Debug)]
pub struct RouterClient {
router: Arc<Router>,
}
#[async_trait]
pub trait Client {
async fn get(&mut self);
}
#[async_trait]
impl Client for RouterClient {
async fn get(&mut self) {
todo!();
}
}
error: future cannot be sent between threads safely
--> http_server/tests/http_server/main.rs:208:29
|
208 | async fn get(&mut self) {
| _____________________________^
209 | | todo!();
210 | | }
| |_____^ future created by async block is not `Send`
|
= help: the trait `Sync` is not implemented for `(dyn tower::util::boxed_clone::CloneService<Request<Body>, Error = Infallible, Response = Response<http_body::combinators::box_body::UnsyncBoxBody<axum::body::Bytes, axum::Error>>, Future = Pin<Box<(dyn std::future::Future<Output = Result<Response<http_body::combinators::box_body::UnsyncBoxBody<axum::body::Bytes, axum::Error>>, Infallible>> + Send + 'static)>>> + Send + 'static)`
note: captured value is not `Send` because `&` references cannot be sent unless their referent is `Sync`
--> http_server/tests/http_server/main.rs:208:23
|
208 | async fn get(&mut self) {
| ^^^^ has type `&mut RouterClient` which is not `Send`, because `RouterClient` is not `Sync`
= note: required for the cast to the object type `dyn std::future::Future<Output = ()> + Send`