I am trying make a small user authentication web-service as an exercise in learning tower::Service and axum.
I have abstracted away user credential storage via Fetch
trait as follows
pub(crate) trait Fetch<T>: Debug + Send + Sync {
type UserId;
fn fetch(&self, user_id: Self::UserId) -> self::error::StoreResult<T>;
}
so I'd be able de-couple services from the underlying storage engine.
A UserFetch
struct contains a concrete impl and a way to ID the spawned service
#[derive(Debug)]
pub(crate) struct UserFetch {
id: Uuid,
user_fetch: Arc<dyn store::Fetch<User, UserId = UserId>>,
}
StoreResult
is just a util enum
for Result<T, StoreError>
.
This is how I am trying to impl tower::Service
trait following their example in Service in tower - Rust
impl tower::Service<UserId> for UserFetch {
type Response = User;
type Error = service::error::ServiceError;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: UserId) -> Self::Future {
Box::pin(async { Ok(self.user_fetch.fetch(req)?) })
}
}
?
to convert StoreError
to ServiceError
and Ok
to re-wrap.
This is the error I get
|
30 | fn call(&mut self, req: UserId) -> Self::Future {
| - let's call the lifetime of this reference `'1`
31 | Box::pin(async { Ok(self.user_fetch.fetch(req)?) })
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
I don't quite understand what the compiler is trying to say. Is it saying UserFetch
the struct may not live as long as the async
closure its method returns? What am I doing wrong here?