Refere to a referecence in an associated type
⚓ Rust 📅 2026-06-26 👤 surdeus 👁️ 1I got this simple trait:
pub trait Delete<C> {
type Error;
/// Delete the row in the database.
fn delete(&self, conn: C) -> impl Future<Output = Result<(), Self::Error>>;
}
And I want to implement it directly for the corresponding pool object in SQLx:
impl<T> Delete<sqlx::SqlitePool> for T
where
T: for<'a> Delete<&'a mut sqlx::SqliteConnection>,
{
type Error = <T as Delete<&mut sqlx::SqliteConnection>>::Error;
async fn delete(&self, conn: sqlx::SqlitePool) -> Result<(), Self::Error> {
let mut conn = conn.acquire().await?;
self.delete(&mut *conn).await
}
}
However rust want me to specify the lifetime of the reference in the associated type Error... Which I don't have. And if I add a reference to the implementation, rust tell me that the variable conn doesn't live long enough due to the lifetime being referenced in the output type
error[E0597]: `conn` does not live long enough
--> src/table/delete.rs:17:27
|
15 | async fn delete(&self, conn: sqlx::SqlitePool) -> Result<(), Self::Error> {
| ----------------------- return type of async function is Result<(), <T as delete::Delete<&'1 mut SqliteConnection>>::Error>
16 | let mut conn = conn.acquire().await.unwrap();
| -------- binding `conn` declared here
17 | self.delete(&mut *conn).await
| ------------------^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `conn` is borrowed for `'1`
18 | }
| - `conn` dropped here while still borrowed
Is there any trickery I can use to fix that?
2 posts - 2 participants
🏷️ Rust_feed