Borrowing through Axum's `FromRef` trait?
⚓ Rust 📅 2025-10-31 👤 surdeus 👁️ 10Axum provides a trait, FromRef, that it uses to allow handlers to accept state values that can be derived from the router's primary state type, rather than having to accept only values of that specific type. It's defined as
pub trait FromRef<T> {
fn from_ref(input: &T) -> Self;
}
One example might be:
impl FromRef<AppState> for DatabasePool {
fn from_ref(app_state: &AppState) -> DatabasePool {
app_state.database_pool.clone()
}
}
My question is, can I derive structs that contain references borrowed from T? For example, is there a way to implement FromRef such that I can get a DatabasePoolRef<'a> from &'a AppState? I wasn't able to get something working myself, and I half suspect that the design of the trait effectively means that the answer is no, but I thought I'd ask in case I've missed something.
Here's an example that doesn't work:
struct Foo<'a> {
db: &'a db,
}
impl<'a> FromRef<App> for Foo<'a> {
fn from_ref(app: &'a App) -> Self {
Self { db: &app.db }
}
}
This gives
error[E0308]: method not compatible with trait
--> src/app.rs:85:5
|
85 | fn from_ref(app: &'a App) -> Self {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected signature `fn(&App) -> Foo<'_>`
found signature `fn(&'a App) -> Foo<'_>`
note: the anonymous lifetime as defined here...
--> /Users/owen/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/axum-core-0.5.0/src/extract/from_ref.rs:15:24
|
15 | fn from_ref(input: &T) -> Self;
| ^
note: ...does not necessarily outlive the lifetime `'a` as defined here
--> src/app.rs:84:6
|
84 | impl<'a> FromRef<App> for Foo<'a> {
| ^^
3 posts - 2 participants
🏷️ Rust_feed