title: Function parameter in recursive async fn
body: <p>In Rust it is possible to have function parameters in asynchronous functions:</p>
  
  <pre><code>use futures::executor::block_on;
  
  async fn hello_world(cb: &amp;dyn Fn() -&gt; u32) {
      println!("hello, world! {}", cb());
  }
  
  fn main() {
      let future = hello_world(&amp;|| { 42 } );
      block_on(future);
  }
  </code></pre>
  
  <p>But one limitation of these asynchronous functions is they cannot be recursive. The literature recommends using things like <code>BoxFuture</code> to work around the problem. But the simple transformation that is documented does not work if there is a function parameter:</p>
  
  <pre><code>use futures::executor::block_on;
  use futures::future::{BoxFuture, FutureExt};
  
  fn hello_world(cb: &amp;dyn Fn() -&gt; u32) -&gt; BoxFuture&lt;'static, ()&gt; {
      async move {
          println!("hello, world! {}", cb());
      }.boxed()
  }
  
  fn main() {
      let future = hello_world(&amp;|| { 42 } );
      block_on(future);
  }
  </code></pre>
  
  <pre><code>error[E0277]: `dyn std::ops::Fn() -&gt; u32` cannot be shared between threads safely
   --&gt; src/main.rs:7:7
    |
  7 |     }.boxed()
    |       ^^^^^ `dyn std::ops::Fn() -&gt; u32` cannot be shared between threads safely
    |
    = help: the trait `std::marker::Sync` is not implemented for `dyn std::ops::Fn() -&gt; u32`
    = note: required because of the requirements on the impl of `std::marker::Send` for `&amp;dyn std::ops::Fn() -&gt; u32`
    = note: required because it appears within the type `[static generator@src/main.rs:5:16: 7:6 cb:&amp;dyn std::ops::Fn() -&gt; u32 _]`
    = note: required because it appears within the type `std::future::from_generator::GenFuture&lt;[static generator@src/main.rs:5:16: 7:6 cb:&amp;dyn std::ops::Fn() -&gt; u32 _]&gt;`
    = note: required because it appears within the type `impl core::future::future::Future`
  
  error: aborting due to previous error
  </code></pre>
  
  <p>Why did the transformation with <code>BoxFuture</code> cause a compilation error?
  How to have a function parameter in a recursive asynchronous function?</p>
  
  <p>I have tried using <code>LocalBoxFuture</code> instead of <code>BoxFuture</code> but the error persists.</p>