title: General pointer type for `Rc`, `Box`, `Arc`
body: <p>I have a <code>struct</code> which references a value (because it is <code>?Sized</code> or very big). This value has to live with the struct, of course.<br>
  However, the struct shouldn't restrict the user on <em>how</em> to accomplish that. Whether the user wraps the value in a <code>Box</code> or <code>Rc</code> or makes it <code>'static</code>, the value just has to survive with the struct. Using named lifetimes would be complicated because the reference will be moved around and may outlive our <code>struct</code>. What I am looking for is a general pointer type (if it exists / can exist). </p>
  
  <p><strong>How can the struct make sure the referenced value lives as long as the struct lives, without specifying how?</strong>  </p>
  
  <p>Example (<a href="http://is.gd/Is9Av6" rel="nofollow noreferrer">is.gd/Is9Av6</a>):</p>
  
  <pre><code>type CallBack = Fn(f32) -&gt; f32;
  
  struct Caller {
      call_back: Box&lt;CallBack&gt;,
  }
  
  impl Caller {
      fn new(call_back: Box&lt;CallBack&gt;) -&gt; Caller {
          Caller {call_back: call_back}
      }
  
      fn call(&amp;self, x: f32) -&gt; f32 {
          (self.call_back)(x)
      }
  }
  
  let caller = {
      // func goes out of scope
      let func = |x| 2.0 * x; 
      Caller {call_back: Box::new(func)}
  };
  
  // func survives because it is referenced through a `Box` in `caller`
  let y = caller.call(1.0);
  assert_eq!(y, 2.0);
  </code></pre>
  
  <p>Compiles, all good. But if we don't want to use a <code>Box</code> as a pointer to our function (one can call <code>Box</code> a pointer, right?), but something else, like <code>Rc</code>, this wont be possible, since <code>Caller</code> restricts the pointer to be a <code>Box</code>.</p>
  
  <pre><code>let caller = {
      // function is used by `Caller` and `main()` =&gt; shared resource
      // solution: `Rc`
      let func = Rc::new(|x| 2.0 * x); 
      let caller = Caller {call_back: func.clone()}; // ERROR Rc != Box
  
      // we also want to use func now
      let y = func(3.0);
  
      caller
  };
  
  // func survives because it is referenced through a `Box` in `caller`
  let y = caller.call(1.0);
  assert_eq!(y, 2.0);
  </code></pre>
  
  <p>(<a href="http://is.gd/qUkAvZ" rel="nofollow noreferrer">is.gd/qUkAvZ</a>)</p>
  
  <p>Possible solution: <code>Deref</code>? (<a href="http://is.gd/mmY6QC" rel="nofollow noreferrer">http://is.gd/mmY6QC</a>)</p>
  
  <pre><code>use std::rc::Rc;
  use std::ops::Deref;
  
  type CallBack = Fn(f32) -&gt; f32;
  
  struct Caller&lt;T&gt;
          where T: Deref&lt;Target = Box&lt;CallBack&gt;&gt; {
      call_back: T,
  }
  
  impl&lt;T&gt; Caller&lt;T&gt; 
          where T: Deref&lt;Target = Box&lt;CallBack&gt;&gt; {
      fn new(call_back: T) -&gt; Caller&lt;T&gt; {
          Caller {call_back: call_back}
      }
  
      fn call(&amp;self, x: f32) -&gt; f32 {
          (*self.call_back)(x)
      }
  }
  
  fn main() {
      let caller = {
          // function is used by `Caller` and `main()` =&gt; shared resource
          // solution: `Rc`
          let func_obj = Box::new(|x: f32| 2.0 * x) as Box&lt;CallBack&gt;;
          let func = Rc::new(func_obj); 
          let caller = Caller::new(func.clone());
  
          // we also want to use func now
          let y = func(3.0);
  
          caller
      };
  
      // func survives because it is referenced through a `Box` in `caller`
      let y = caller.call(1.0);
      assert_eq!(y, 2.0);
  }
  </code></pre>
  
  <p>Is this the way to go with Rust? Using <code>Deref</code>? It works at least.</p>
  
  <p>Am I missing something obvious?  </p>
  
  <p><a href="https://stackoverflow.com/q/31215418/2979958">This question</a> did not solve my problem, since the value is practically unusable as a <code>T</code>.</p>