title: Returning the reference inside Rc<RefCell<&'a mut [u8]>>
body: <p>Based on this <a href="https://stackoverflow.com/a/68174563/10116440">https://stackoverflow.com/a/68174563/10116440</a> I have a type that can hold references, but can be cloned also. The clone is just for being able to call a closure <code>dyn Fn</code> that moves this type repeated times.</p>
  <pre class="lang-rust prettyprint-override"><code>use std::cell::RefCell;
  use std::ops::Deref;
  use std::ops::DerefMut;
  use std::rc::Rc;
  use std::vec::Vec;
  
  #[derive(Debug)]
  pub enum EncodedPacketError {
      NotOwned,
      NotMutable,
  }
  
  #[derive(Clone)]
  pub enum EncodedPacket&lt;'a&gt; {
      Owned(Vec&lt;u8&gt;),
      Ref(Rc&lt;&amp;'a [u8]&gt;),
      RefMut(Rc&lt;RefCell&lt;&amp;'a mut [u8]&gt;&gt;),
  }
  
  impl&lt;'a&gt; EncodedPacket&lt;'a&gt; {
      pub fn slice(&amp;'a self) -&gt; &amp;'a [u8] {
          match self {
              EncodedPacket::Owned(vec) =&gt; vec.as_slice(),
              EncodedPacket::Ref(slice) =&gt; slice,
              EncodedPacket::RefMut(slice) =&gt; slice.borrow().deref(),
          }
      }
  
      pub fn slice_mut(&amp;'a mut self) -&gt; Result&lt;&amp;'a mut [u8], EncodedPacketError&gt; {
          match self {
              EncodedPacket::Owned(ref mut vec) =&gt; Ok(vec.as_mut_slice()),
              EncodedPacket::Ref(_) =&gt; Err(EncodedPacketError::NotMutable),
              EncodedPacket::RefMut(slice) =&gt; Ok(slice.borrow_mut().deref_mut()),
          }
      }
  
      pub fn vec_ref(&amp;'a self) -&gt; Result&lt;&amp;'a Vec&lt;u8&gt;, EncodedPacketError&gt; {
          match self {
              EncodedPacket::Owned(vec) =&gt; Ok(&amp;vec),
              EncodedPacket::Ref(slice) =&gt; Err(EncodedPacketError::NotOwned),
              EncodedPacket::RefMut(slice) =&gt; Err(EncodedPacketError::NotOwned),
          }
      }
  
      pub fn vec_ref_mut(&amp;'a mut self) -&gt; Result&lt;&amp;'a Vec&lt;u8&gt;, EncodedPacketError&gt; {
          match self {
              EncodedPacket::Owned(vec) =&gt; Ok(vec),
              EncodedPacket::Ref(slice) =&gt; Err(EncodedPacketError::NotOwned),
              EncodedPacket::RefMut(slice) =&gt; Err(EncodedPacketError::NotOwned),
          }
      }
  }
  </code></pre>
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=428c99820032cbc8527c77d1b0702036" rel="nofollow noreferrer">Playground</a></p>
  <p>I'm having errors on how to access the inner types as references:</p>
  <pre class="lang-none prettyprint-override"><code>error[E0515]: cannot return value referencing temporary value
    --&gt; src/lib.rs:22:9
     |
  22 | /         match self {
  23 | |             EncodedPacket::Owned(vec) =&gt; vec.as_slice(),
  24 | |             EncodedPacket::Ref(slice) =&gt; slice,
  25 | |             EncodedPacket::RefMut(slice) =&gt; slice.borrow().deref(),
     | |                                             -------------- temporary value created here
  26 | |         }
     | |_________^ returns a value referencing data owned by the current function
  
  error[E0515]: cannot return value referencing temporary value
    --&gt; src/lib.rs:33:45
     |
  33 |             EncodedPacket::RefMut(slice) =&gt; Ok(slice.borrow_mut().deref_mut()),
     |                                             ^^^------------------^^^^^^^^^^^^^
     |                                             |  |
     |                                             |  temporary value created here
     |                                             returns a value referencing data owned by the current function
  </code></pre>
  <p>I don't understand why. Indeed, I'm creating a temporary <code>Ref</code> when I call <code>slice.borrow_mut()</code>, but I'm not returning a reference to this <code>Ref</code>, I'm returning a reference to what's inside of the <code>Ref</code>, which I access using <code>deref_mut()</code>. Why do I get temporary value error?</p>