title: How do you call future::select on a Future stored in a struct from a method that takes &mut self?
body: <p>I want to store a future that we occasionally await in a struct. My use-case is to have a signal to tell my network packet handler to shut down gracefully. A minimal example could look like this, with a dependency on futures 0.3:</p>
  
  <pre><code>use futures::{
      executor::block_on,
      future::{pending, select, Either, Future},
  }; // 0.3.4
  
  struct Foo&lt;F: Future + Unpin&gt; {
      fut: F,
      fut_opt: Option&lt;F&gt;,
  }
  
  impl&lt;F: Future + Unpin&gt; Foo&lt;F&gt; {
      async fn wait(self: &amp;mut Self) {
          let bar = pending::&lt;&amp;str&gt;();
          match select(self.fut, bar).await {
              Either::Left(_) =&gt; println!("foo"),
              Either::Right(_) =&gt; println!("bar"),
          }
      }
  
      async fn wait_optional(self: &amp;mut Self) {
          let bar = pending::&lt;&amp;str&gt;();
          if let Some(foo) = self.fut_opt.take() {
              match select(foo, bar).await {
                  Either::Left(_) =&gt; println!("foo"),
                  Either::Right((_, foo_fut)) =&gt; {
                      self.fut_opt.replace(foo_fut);
                      println!("bar")
                  }
              }
          }
      }
  }
  
  fn main() {
      let mut foo = Foo {
          fut: pending::&lt;()&gt;(),
          fut_opt: Option::from(pending::&lt;()&gt;()),
      };
  
      block_on(foo.wait())
  }
  </code></pre>
  
  <p>The problem is that <code>select</code> wants to move the value in the <code>fn wait(..)</code> version, so I'm getting a compile error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0507]: cannot move out of `self.fut` which is behind a mutable reference
    --&gt; src/main.rs:14:22
     |
  14 |         match select(self.fut, bar).await {
     |                      ^^^^^^^^ move occurs because `self.fut` has type `F`, which does not implement the `Copy` trait
  </code></pre>
  
  <p>A workaround I came up with can be seen in <code>fn wait_optional</code>: I'm (ab)using an <code>Option</code> to store the future, take it out when needed and then put it back in as <code>select</code> returns the unawaited future in <code>Either</code>. This compiles and seems to work just fine - but it feels hacky. Is there a "proper" way for achieving this?</p>