title: How can I put an async function into a map in Rust?
body: <p>I cannot handle async functions when writing an async router for <code>hyper</code>.</p>
  
  <p>This code:</p>
  
  <pre><code>use std::collections::HashMap;
  use std::future::Future;
  
  type BoxedResult&lt;T&gt; = Result&lt;T, Box&lt;dyn std::error::Error + Send + Sync&gt;&gt;;
  type CalcFn = Box&lt;dyn Fn(i32, i32) -&gt; dyn Future&lt;Output = BoxedResult&lt;i32&gt;&gt;&gt;;
  
  async fn add(a: i32, b: i32) -&gt; BoxedResult&lt;i32&gt; {
      Ok(a + b)
  }
  
  async fn sub(a: i32, b: i32) -&gt; BoxedResult&lt;i32&gt; {
      Ok(a - b)
  }
  
  fn main() {
      let mut map: HashMap&lt;&amp;str, CalcFn&gt; = Default::default();
      map.insert("add", Box::new(add));
      map.insert("sub", Box::new(sub));
  
      println!("map size: {}", map.len());
  }
  </code></pre>
  
  <p>Generates the following compiler error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0271]: type mismatch resolving `&lt;fn(i32, i32) -&gt; impl std::future::Future {add} as std::ops::FnOnce&lt;(i32, i32)&gt;&gt;::Output == dyn std::future::Future&lt;Output = std::result::Result&lt;i32, std::boxed::Box&lt;dyn std::error::Error + std::marker::Send + std::marker::Sync&gt;&gt;&gt;`
    --&gt; src/main.rs:17:23
     |
  17 |     map.insert("add", Box::new(add));
     |                       ^^^^^^^^^^^^^ expected opaque type, found trait std::future::Future
     |
     = note: expected type `impl std::future::Future`
                found type `dyn std::future::Future&lt;Output = std::result::Result&lt;i32, std::boxed::Box&lt;dyn std::error::Error + std::marker::Send + std::marker::Sync&gt;&gt;&gt;`
     = note: required for the cast to the object type `dyn std::ops::Fn(i32, i32) -&gt; dyn std::future::Future&lt;Output = std::result::Result&lt;i32, std::boxed::Box&lt;dyn std::error::Error + std::marker::Send + std::marker::Sync&gt;&gt;&gt;`
  
  error[E0271]: type mismatch resolving `&lt;fn(i32, i32) -&gt; impl std::future::Future {sub} as std::ops::FnOnce&lt;(i32, i32)&gt;&gt;::Output == dyn std::future::Future&lt;Output = std::result::Result&lt;i32, std::boxed::Box&lt;dyn std::error::Error + std::marker::Send + std::marker::Sync&gt;&gt;&gt;`
    --&gt; src/main.rs:18:23
     |
  18 |     map.insert("sub", Box::new(sub));
     |                       ^^^^^^^^^^^^^ expected opaque type, found trait std::future::Future
     |
     = note: expected type `impl std::future::Future`
                found type `dyn std::future::Future&lt;Output = std::result::Result&lt;i32, std::boxed::Box&lt;dyn std::error::Error + std::marker::Send + std::marker::Sync&gt;&gt;&gt;`
     = note: required for the cast to the object type `dyn std::ops::Fn(i32, i32) -&gt; dyn std::future::Future&lt;Output = std::result::Result&lt;i32, std::boxed::Box&lt;dyn std::error::Error + std::marker::Send + std::marker::Sync&gt;&gt;&gt;`
  </code></pre>
  
  <p>It seems there is a conflict between <code>impl Future</code> and <code>dyn Future</code>, but I have no idea how to handle it.</p>