Is it possible to move even with immutable borrows?
<pre><code>use std::marker;
use std::ops;
pub struct Shared&lt;'r, T: 'r&gt; {
    data: *mut T,
    _pd: marker::PhantomData&lt;&amp;'r T&gt;,
}

impl&lt;'r, T&gt; Shared&lt;'r, T&gt; {
    pub fn new(value: T) -&gt; Shared&lt;'r, T&gt; {
        let boxed = Box::new(value);
        Shared {
            data: Box::into_raw(boxed),
            _pd: marker::PhantomData,
        }
    }

    pub fn as_ref(&amp;self) -&gt; SharedRef&lt;'r, T&gt; {
        SharedRef {
            data: self.data,
            _pd: marker::PhantomData,
        }
    }
}

impl&lt;'r, T&gt; ops::Deref for Shared&lt;'r, T&gt; {
    type Target = T;
    fn deref(&amp;self) -&gt; &amp;T {
        unsafe { &amp;*self.data }
    }
}

pub struct SharedRef&lt;'r, T: 'r&gt; {
    data: *mut T,
    _pd: marker::PhantomData&lt;&amp;'r T&gt;,
}

impl&lt;'r, T&gt; ops::Deref for SharedRef&lt;'r, T&gt; {
    type Target = T;
    fn deref(&amp;self) -&gt; &amp;T {
        unsafe { &amp;*self.data }
    }
}

impl&lt;'r, T&gt; Drop for Shared&lt;'r, T&gt; {
    fn drop(&amp;mut self) {
        unsafe {
            Box::from_raw(self.data);
        }
    }
}

fn main() {
    let s = Shared::new(42);
    let s_ref = s.as_ref();
    {
        let s1 = s;
    }
    // lifetime should end here
    println!("{}", *s_ref);
}
</code></pre>

<p>What I wanted to express was a mix between a <code>Box</code> and an <code>Arc</code>. A uniquely owned pointer that is also capable of giving out references.</p>

<p>The problem is that I want to be able to move <code>Shared</code> around even if there are currently immutable borrows to it. It should be legal in this scenario because it is heap allocated.</p>

<p>The problem is that I have no idea how to express this.</p>

<pre><code>fn main() {
    let s = Shared::new(42);
    let s_ref = s.as_ref();
    {
        let s1 = s;
    }
    // lifetime should end here
    println!("{}", *s_ref);
}
</code></pre>

<p>Here I move <code>s</code> into a scope with "less" lifetime than it had before. But now after I have moved <code>s</code> into <code>s1</code>, <code>s_ref</code> should not be accessible anymore. So what I want to say is that it is okay to move a <code>Shared</code> if the lifetime does not get smaller.</p>

<p>Can this be expressed in Rust?</p>