title: How can I construct a container from within a closure with a 'static bound?
body: <p>I'm using the <a href="https://docs.rs/libpulse-binding/2.5.0/libpulse_binding/index.html" rel="nofollow noreferrer">libpulse_binding</a> library, and I'm trying to obtain a sequence of <a href="https://docs.rs/libpulse-binding/2.5.0/libpulse_binding/context/introspect/struct.SinkInputInfo.html" rel="nofollow noreferrer"><code>SinkInputInfo</code></a>s from the <a href="https://docs.rs/libpulse-binding/2.5.0/libpulse_binding/context/introspect/struct.Introspector.html#method.get_sink_input_info_list" rel="nofollow noreferrer"><code>get_sink_input_info_list</code></a> function:</p>
  
  <pre><code>pub fn get_sink_input_info_list&lt;F&gt;(
      &amp;self,
      callback: F,
  ) -&gt; Operation&lt;dyn FnMut(ListResult&lt;&amp;SinkInputInfo&gt;)&gt;
  where
      F: FnMut(ListResult&lt;&amp;SinkInputInfo&gt;) + 'static,
  </code></pre>
  
  <p>The function takes a callback and calls it once for each <code>SinkInputInfo</code> it produces. I'm trying to collect all those <code>SinkInputInfo</code>s into a single list so I can get a clearer view of the state of the world. Irritatingly, <code>SinkInputInfo</code> doesn't implement <code>Copy</code> or <code>Clone</code>, so I made a custom struct and implemented <code>From</code> to get the useful information out of the <code>SinkInputInfo</code>:</p>
  
  <pre><code>struct StreamInfo {
      readable_name: String,
      binary_name: String,
      pid: String,
  }
  
  impl From&lt;&amp;pulse::context::introspect::SinkInputInfo&lt;'_&gt;&gt; for StreamInfo {
      fn from(info: &amp;pulse::context::introspect::SinkInputInfo) -&gt; Self {
          let name = info.proplist.gets("application.name").unwrap();
          let binary = info.proplist.gets("application.process.binary").unwrap();
          let pid = info.proplist.gets("application.process.id").unwrap();
          StreamInfo {
              readable_name: name,
              binary_name: binary,
              pid: pid,
          }
      }
  }
  </code></pre>
  
  <p>However, this doesn't seem to work. I have the following code:</p>
  
  <pre><code>let mut sink_infos: Vec&lt;StreamInfo&gt; = Vec::new();
  let op = introspector.get_sink_input_info_list(|result| match result {
      pulse::callbacks::ListResult::Item(info) =&gt; sink_infos.push(info.into()),
      pulse::callbacks::ListResult::End =&gt; {},
      pulse::callbacks::ListResult::Error =&gt; panic!("Error getting sink input info"),
  });
  </code></pre>
  
  <p>but it doesn't compile:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0373]: closure may outlive the current function, but it borrows `sink_infos`, which is owned by the current function
    --&gt; src/bin/play-pause.rs:49:52
     |
  49 |     let op = introspector.get_sink_input_info_list(|result| match result {
     |                                                    ^^^^^^^^ may outlive borrowed value `sink_infos`
  50 |         pulse::callbacks::ListResult::Item(info) =&gt; sink_infos.push(info.into()),
     |                                                     ---------- `sink_infos` is borrowed here
     |
  note: function requires argument type to outlive `'static`
    --&gt; src/bin/play-pause.rs:49:14
     |
  49 |       let op = introspector.get_sink_input_info_list(|result| match result {
     |  ______________^
  50 | |         pulse::callbacks::ListResult::Item(info) =&gt; sink_infos.push(info.into()),
  51 | |         pulse::callbacks::ListResult::End =&gt; {},
  52 | |         pulse::callbacks::ListResult::Error =&gt; panic!("Error getting sink input info"),
  53 | |     });
     | |______^
  help: to force the closure to take ownership of `sink_infos` (and any other referenced variables), use the `move` keyword
     |
  49 |     let op = introspector.get_sink_input_info_list(move |result| match result {
     |                                                    ^^^^^^^^^^^^^
  </code></pre>
  
  <p>tldr: the closure has to have the <code>'static</code> lifetime because libpulse_binding says so (presumably because it gets handed to the PulseAudio C API, which can then do whatever it likes with it), but <code>sink_infos</code> is not <code>'static</code>, and the closure has to borrow <code>sink_infos</code> in order to append to it, which makes the closure not <code>'static</code>, IIUC.</p>
  
  <p>How can I make a <code>Vec</code> (or any container, I'm not fussy) of <code>SinkInputInfo</code>s given a <code>'static</code> closure that gets called repeatedly with a <code>&amp;SinkInputInfo</code>?</p>