title: Using FnMut() closures in a static function
body: <p><strong>Background:</strong> I'm trying to avoid the use of <code>Mutex</code>/<code>RefCell</code>/<code>Option</code> dance in an interrupt handler for an embedded system. I do not want to use the heap (and I don't think it should be necessary -- but feel free to show me wrong). I can't use <code>std</code>. I've looked at <code>cortex-m-rtfm</code> and it's neat, but pretty invasive. And anyway, this is a bit of a learning exercise. If it works out, I would prefer to use closures to handle interrupts since it feels closer to bare Rust. I am a total Rust newbie -- I've been working with it for about a week. I have tried a <em>lot</em> of different variants of this as I've read through documentation, re-read the Rust book, blog posts, etc.,. I cannot figure what I'm doing wrong here.</p>
  
  <p>Here's the sample code. Questions to follow:</p>
  
  <pre><code>use core::cell::UnsafeCell;
  
  pub struct Handler&lt;'a&gt; {
      h: UnsafeCell&lt;&amp;'a dyn FnMut()&gt;,
  }
  
  impl&lt;'a&gt; Handler&lt;'a&gt; {
      pub fn new&lt;T: FnMut()&gt;(closure: &amp;'a dyn FnMut()) -&gt; Self {
          Handler {
              h: UnsafeCell::new(closure),
          }
      }
  
      pub fn call(&amp;self) {
          unsafe {
              // NOTE: type returned by `self.h.get()` is
              // `*mut &amp;'a (dyn std::ops::FnMut() + 'a)`
              let h: *mut FnMut() = self.h.get();
              h();
          }
      }
  }
  unsafe impl&lt;'a&gt; Sync for Handler&lt;'a&gt; {}
  
  fn default_handler() {}
  
  static HANDLER: Handler = Handler {
      h: UnsafeCell::new(&amp;default_handler),
  };
  
  #[test]
  fn call_handler() {
      let mut a: u32 = 0;
      let foo = move || a += 1;
      let mut handler = Handler::new(&amp;foo);
      handler.call();
      a += 2; // Shouldn't this cause compilation failure because `a`
              // was moved into the closure above?
      assert_eq!(a, 1);
  }
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>error[E0618]: expected function, found `*mut dyn std::ops::FnMut()`
    --&gt; src/lib.rs:19:13
     |
  18 |             let h: *mut FnMut() = self.h.get();
     |                 - `*mut dyn std::ops::FnMut()` defined here
  19 |             h();
     |             ^--
     |             |
     |             call expression requires function
  
  error[E0277]: expected a `std::ops::Fn&lt;()&gt;` closure, found `(dyn std::ops::FnMut() + 'a)`
    --&gt; src/lib.rs:18:35
     |
  18 |             let h: *mut FnMut() = self.h.get();
     |                                   ^^^^^^^^^^^^ expected an `Fn&lt;()&gt;` closure, found `(dyn std::ops::FnMut() + 'a)`
     |
     = help: the trait `std::ops::Fn&lt;()&gt;` is not implemented for `(dyn std::ops::FnMut() + 'a)`
     = note: wrap the `(dyn std::ops::FnMut() + 'a)` in a closure with no arguments: `|| { /* code */ }
     = note: required because of the requirements on the impl of `std::ops::FnMut&lt;()&gt;` for `&amp;'a (dyn std::ops::FnMut() + 'a)`
     = note: required for the cast to the object type `dyn std::ops::FnMut()`
  </code></pre>
  
  <p><strong>Explanation:</strong> Hopefully, my intentions are obvious: I'll set up the closure for <code>HANDLER</code> in <code>main</code>, before going into a busy-loop that never exits. The closure will mutably borrow the stuff the interrupt handler needs for its operation, preventing its use in other contexts. Since <code>main</code> never exits, stack-allocated variables within it are effectively <code>'static</code>, so there shouldn't be a problem with referencing them at any point after the closure is set. The interrupt handler itself (not shown) will simply call the closure to do its work. To work around the storage of a closure (which is not <code>Sized</code>) in a static, I need to store a reference to the closure. <code>UnsafeCell</code> isn't necessarily required, but since I'm using <code>FnMut()</code> its referents need to be mutable, which runs into <code>statics require immutable values</code> when trying to set up <code>default_handler</code> during the creation of a <code>static mut HANDLER</code>.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li><p>As posted, this code doesn't compile. For some reason, the
  assignment <code>let h: *mut FnMut() = self.h.get()</code> tells me that it <code>
  expected an Fn&lt;()&gt; closure, found (dyn std::ops::FnMut() + 'a)</code>. Well, I know why it found that type. But why is it expecting <code>Fn&lt;()&gt;</code>?</p></li>
  <li><p>In the <code>call_handler</code> test, why is this compiling at all? The <code>foo</code> closure <code>move</code>s its captured variable <code>a</code>. How is it possible to mutate it after the closure definition? When I've tried this code with a type that doesn't implement <code>Copy</code>, it fails as expected, but I'm frankly surprised that trait matters. Isn't the point that <code>foo</code> owns <code>a</code> now?</p></li>
  </ol>
  
  <p>I realize there are potential issues with changing <code>HANDLER.h</code> at arbitrary points in the code, but I will worry about solving those later, after there's a viable proof-of-concept.</p>