4

Here is a working sample to implement Runnable for Fn() (so that we can directly pass a &closure to the run_the_runnable function):

trait Runnable {
    fn run(&self);
}

impl<F> Runnable for F where F: Fn() {
    fn run(&self) {
        self();
    }
}

fn run_the_runnable(runnable: &Runnable) {
    runnable.run();
}

fn main() {
    // runnable without parameters
    struct MyRunnable;
    impl Runnable for MyRunnable {
        fn run(&self) {
            println!("Hello from MyRunnable");
        }
    }

    // from struct instance (WORKS)
    run_the_runnable(&MyRunnable);

    // from closure (WORKS)
    run_the_runnable(&|| {
        println!("Hello from run() closure");
    });
}

(Rust playground)

Now, let's change the runnable run() method to accept a reference parameter (&i32), and implement Fn(&i32):

trait Runnable {
    fn run(&self, x: &i32);
}

impl<F> Runnable for F where F: Fn(&i32) {
    fn run(&self, x: &i32) {
        self(x);
    }
}

fn run_the_runnable(runnable: &Runnable, x: &i32) {
    runnable.run(x);
}

fn main() {
    // runnable without parameters
    struct MyRunnable;
    impl Runnable for MyRunnable {
        fn run(&self, x: &i32) {
            println!("Hello from MyRunnable {}", x);
        }
    }

    let x = 42;

    // from struct instance (WORKS)
    run_the_runnable(&MyRunnable, &x);

    // from closure (DOES NOT WORK)
    run_the_runnable(&|x| {
        println!("Hello from run(&i32) closure {}", x);
    }, &x);
}

(Rust playground)

Passing the closure does not work anymore:

error[E0271]: type mismatch resolving `for<'r> <[closure@<anon>:30:27: 32:10] as std::ops::FnOnce<(&'r i32,)>>::Output == ()`
  --> <anon>:30:26
   |
30 |           run_the_runnable(&|x| {
   |  __________________________^ starting here...
31 | |             println!("Hello from run(&i32) closure {}", x);
32 | |         }, &x);
   | |_________^ ...ending here: expected bound lifetime parameter , found concrete lifetime
   |
   = note: concrete lifetime that was found is lifetime '_#63r
   = note: required because of the requirements on the impl of `Runnable` for `[closure@<anon>:30:27: 32:10]`
   = note: required for the cast to the object type `Runnable`

error[E0281]: type mismatch: the type `[closure@<anon>:30:27: 32:10]` implements the trait `std::ops::Fn<(_,)>`, but the trait `for<'r> std::ops::Fn<(&'r i32,)>` is required (expected concrete lifetime, found bound lifetime parameter )
  --> <anon>:30:26
   |
30 |           run_the_runnable(&|x| {
   |  __________________________^ starting here...
31 | |             println!("Hello from run(&i32) closure {}", x);
32 | |         }, &x);
   | |_________^ ...ending here
   |
   = note: required because of the requirements on the impl of `Runnable` for `[closure@<anon>:30:27: 32:10]`
   = note: required for the cast to the object type `Runnable`

There are many questions about this error:

But I am still not able to resolve this specific lifetime problem.

Community
  • 1
  • 1
rom1v
  • 2,752
  • 3
  • 21
  • 47
  • 2
    Adding explicit type annotations help. For example, saying `&|x: &i32| { ... }` already solves the problem. Rust seems to have problems inferring types in these situations :/ at least that's what *I* think the problem is. – Lukas Kalbertodt May 16 '17 at 14:14

1 Answers1

2

As suggested in a comment, giving the explicit type in the closure parameters fixes the issue:

@@ -26,8 +26,8 @@ fn main() {
     // from struct instance (WORKS)
     run_the_runnable(&MyRunnable, &x);

-    // from closure (DOES NOT WORK)
-    run_the_runnable(&|x| {
+    // from closure with annotated type (WORKS)
+    run_the_runnable(&|x: &i32| {
         println!("Hello from run(&i32) closure {}", x);
     }, &x);
 }

(Rust playground)

rom1v
  • 2,752
  • 3
  • 21
  • 47