Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference can't resolve type of output of `dyn Future` inside of `impl Fn` inside of Vec #68064

Open
olegnn opened this issue Jan 9, 2020 · 0 comments

Comments

@olegnn
Copy link
Contributor

@olegnn olegnn commented Jan 9, 2020

This code (Playground)

fn get_closure_vec(length: usize) -> Vec<impl Fn() -> Box<dyn std::future::Future<Output = usize>>> {
    vec![|| Box::new(async { 5 }) as Box<_>; length]
}

gives error

error[E0271]: type mismatch resolving `<impl std::future::Future as std::future::Future>::Output == usize`
 --> src/lib.rs:2:13
  |
2 |     vec![|| Box::new(async { 5 }) as Box<_>; width]
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected usize, found i32
  |
  = note: required for the cast to the object type `dyn std::future::Future<Output = usize>`

and this code (Playground)

fn get_closure_vec(length: usize) -> Vec<impl Fn() -> Box<dyn std::future::Future<Output = usize>>> {
    vec![|| Box::new(async { 5 }); length]
}

also gives error

error[E0271]: type mismatch resolving `<[closure@src/lib.rs:2:10: 2:39] as std::ops::FnOnce<()>>::Output == std::boxed::Box<(dyn std::future::Future<Output = usize> + 'static)>`
 --> src/lib.rs:1:41
  |
1 | fn get_closure_vec(width: usize) -> Vec<impl Fn() -> Box<dyn std::future::Future<Output = usize>>> {
  |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found trait std::future::Future
  |
  = note: expected type `std::boxed::Box<impl std::future::Future>`
             found type `std::boxed::Box<(dyn std::future::Future<Output = usize> + 'static)>`
  = note: the return type of a function must have a statically known size

while this one works fine (Playground)

fn get_closure() -> impl Fn() -> Box<dyn std::future::Future<Output = usize>> {
    || Box::new(async { 5 })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.