I’m trying to declare an asynchronous function that takes as an argument another asynchronous function that must take an argument by immutable reference
just for an example of what I want to get here is the js code:
<code>async function wrapper(fun) {
await fun("hello");
}
async function foo(some) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(some);
}
async function main() {
await wrapper(foo)
}
main()
</code>
<code>async function wrapper(fun) {
await fun("hello");
}
async function foo(some) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(some);
}
async function main() {
await wrapper(foo)
}
main()
</code>
async function wrapper(fun) {
await fun("hello");
}
async function foo(some) {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(some);
}
async function main() {
await wrapper(foo)
}
main()
and this is what I try in rust:
<code>use std::future::Future;
use tokio;
#[tokio::main]
async fn main() {
wrapper(foo).await
}
async fn wrapper<F, Fut>(fun: F)
where
F: Fn(&str) -> Fut,
Fut: Future<Output = ()>,
{
fun("hello").await;
}
async fn foo(some: &str) {
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
println!("{}", some);
}
</code>
<code>use std::future::Future;
use tokio;
#[tokio::main]
async fn main() {
wrapper(foo).await
}
async fn wrapper<F, Fut>(fun: F)
where
F: Fn(&str) -> Fut,
Fut: Future<Output = ()>,
{
fun("hello").await;
}
async fn foo(some: &str) {
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
println!("{}", some);
}
</code>
use std::future::Future;
use tokio;
#[tokio::main]
async fn main() {
wrapper(foo).await
}
async fn wrapper<F, Fut>(fun: F)
where
F: Fn(&str) -> Fut,
Fut: Future<Output = ()>,
{
fun("hello").await;
}
async fn foo(some: &str) {
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
println!("{}", some);
}
the compiler gives this error:
<code>error[E0308]: mismatched types
--> src/main.rs:6:5
|
6 | wrapper(foo).await
| ^^^^^^^^^^^^ one type is more general than the other
|
= note: expected opaque type `impl for<'a> Future<Output = ()>`
found opaque type `impl Future<Output = ()>`
= help: consider `await`ing on both `Future`s
= note: distinct uses of `impl Trait` result in different opaque types
note: the lifetime requirement is introduced here
--> src/main.rs:11:20
|
11 | F: Fn(&str) -> Fut,
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `fnhell` (bin "fnhell") due to 1 previous error
</code>
<code>error[E0308]: mismatched types
--> src/main.rs:6:5
|
6 | wrapper(foo).await
| ^^^^^^^^^^^^ one type is more general than the other
|
= note: expected opaque type `impl for<'a> Future<Output = ()>`
found opaque type `impl Future<Output = ()>`
= help: consider `await`ing on both `Future`s
= note: distinct uses of `impl Trait` result in different opaque types
note: the lifetime requirement is introduced here
--> src/main.rs:11:20
|
11 | F: Fn(&str) -> Fut,
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `fnhell` (bin "fnhell") due to 1 previous error
</code>
error[E0308]: mismatched types
--> src/main.rs:6:5
|
6 | wrapper(foo).await
| ^^^^^^^^^^^^ one type is more general than the other
|
= note: expected opaque type `impl for<'a> Future<Output = ()>`
found opaque type `impl Future<Output = ()>`
= help: consider `await`ing on both `Future`s
= note: distinct uses of `impl Trait` result in different opaque types
note: the lifetime requirement is introduced here
--> src/main.rs:11:20
|
11 | F: Fn(&str) -> Fut,
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `fnhell` (bin "fnhell") due to 1 previous error
I have a feeling this could be easily fixed but I have absolutely no idea what he’s talking about.
New contributor
Andrey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.