I cannot decode the cryptic error message from the compiler. I can deduce it is related to the &DbConn
. If I use DbConn
instead of &DbConn
then it works.
#![allow(unused_variables, dead_code)]
#![deny(elided_lifetimes_in_paths)]
struct DbConn;
// Generic fetcher for different types
async fn fetch_connection<F, Fut>(db: &DbConn, fetcher: F)
where
F: FnOnce(&DbConn, u64, u64) -> Fut,
Fut: Future<Output = anyhow::Result<Vec<Dummy>>>,
{
}
struct Dummy;
// Fetch all dummies
impl Dummy {
async fn fetch_all(db: &DbConn, limit: u64, offset: u64) -> anyhow::Result<Vec<Dummy>> {
Ok(Vec::new())
}
}
// Actual work
async fn work() {
let db = DbConn;
fetch_connection(&db, async |db, limit, offset| {
Dummy::fetch_all(db, limit, offset).await
})
.await;
}
error[E0308]: mismatched types
--> src/lib.rs:26:5
|
26 | / fetch_connection(&db, async |db, limit, offset| {
27 | | Dummy::fetch_all(db, limit, offset).await
28 | | })
| |______^ one type is more general than the other
|
= note: expected `async` closure body `{async closure body@src/lib.rs:26:53: 28:6}`
found `async` closure body `{async closure body@src/lib.rs:26:53: 28:6}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
note: the lifetime requirement is introduced here
--> src/lib.rs:9:37
|
9 | F: FnOnce(&DbConn, u64, u64) -> Fut,
| ^^^
error[E0308]: mismatched types
--> src/lib.rs:26:5
|
26 | / fetch_connection(&db, async |db, limit, offset| {
27 | | Dummy::fetch_all(db, limit, offset).await
28 | | })
29 | | .await;
| |__________^ one type is more general than the other
|
= note: expected `async` closure body `{async closure body@src/lib.rs:26:53: 28:6}`
found `async` closure body `{async closure body@src/lib.rs:26:53: 28:6}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
note: the lifetime requirement is introduced here
--> src/lib.rs:9:37
|
9 | F: FnOnce(&DbConn, u64, u64) -> Fut,
| ^^^
error[E0308]: mismatched types
--> src/lib.rs:29:6
|
26 | fetch_connection(&db, async |db, limit, offset| {
| _____________________________________________________-
27 | | Dummy::fetch_all(db, limit, offset).await
28 | | })
| | -
| | |
| |_____the expected `async` closure body
| the found `async` closure body
29 | .await;
| ^^^^^ one type is more general than the other
|
= note: expected `async` closure body `{async closure body@src/lib.rs:26:53: 28:6}`
found `async` closure body `{async closure body@src/lib.rs:26:53: 28:6}`
= note: no two async blocks, even if identical, have the same type
= help: consider pinning your async block and casting it to a trait object
note: the lifetime requirement is introduced here
--> src/lib.rs:9:37
|
9 | F: FnOnce(&DbConn, u64, u64) -> Fut,
| ^^^