How to call a recursive closure in a new thread?

2019-08-17 08:06发布

tool is a grab-bag of useful functions for functional programming includes making recursive closure, for example:

extern crate tool;
use tool::prelude::*;

fn main() {
    let fib = fix(move |f, x| {
        if x == 0 || x == 1 {
            x
        } else {
            // `f` is `fib`
            f(x - 1) + f(x - 2)
        }
    });

    println!("{}", fib(10)); // print 55
}

Things I want to implement is here : playground

Here's the overview: I'm defining a function A which take a function B as parameter, again the function B take function C as parameter. I need to call the function C in a new thread, In this case using &dyn keyword for the inner function gives me an error:

fn a(b: impl Fn(&dyn Fn() -> ()) -> ()) -> () {
 // ...
} 
error[E0277]: `dyn std::ops::Fn() ->()` cannot be shared between threads safely

I try the syntax like below, but this gives me an another error:

fn a(b: impl Fn(impl Fn() -> ()) -> ()) -> () {
 // ...
}  
error[E0666]: nested `impl Trait` is not allowed
 --> src/main.rs:2:21
  |
2 |     fn a(b: impl Fn(impl Fn() -> ()) -> ()) -> () {
  |             --------^^^^^^^^^^^^^^^-------
  |             |       |
  |             |       nested `impl Trait` here
  |             outer `impl Trait`

标签: rust
0条回答
登录 后发表回答