How to write a safe wrap for HashMap with default value
<p>I implemented a wrap for <code>HashMap</code> with default values and I would like to know if it's safe.</p>

<p>When <code>get</code> is called, the internal map may be resized and previous references to values (obtained with <code>get</code>) would be pointing to invalid address. I tried to solve this problem using the idea that "all problems in computer science can be solved by another level of indirection" (<a href="http://www.dmst.aueb.gr/dds/pubs/inbook/beautiful_code/html/Spi07g.html" rel="nofollow">Butler Lampson</a>). I would like to know if this trick makes this code safe.</p>

<pre><code>use std::cell::UnsafeCell;
use std::collections::HashMap;
use std::hash::Hash;

pub struct DefaultHashMap&lt;I: Hash + Eq, T: Clone&gt; {
    default: T,
    map: UnsafeCell&lt;HashMap&lt;I, Box&lt;T&gt;&gt;&gt;,
}

impl&lt;I: Hash + Eq, T: Clone&gt; DefaultHashMap&lt;I, T&gt; {
    pub fn new(default: T) -&gt; Self {
        DefaultHashMap {
            default: default,
            map: UnsafeCell::new(HashMap::new()),
        }
    }

    pub fn get_mut(&amp;mut self, v: I) -&gt; &amp;mut T {
        let m = unsafe { &amp;mut *self.map.get() };
        m.entry(v).or_insert_with(|| Box::new(self.default.clone()))
    }

    pub fn get(&amp;self, v: I) -&gt; &amp;T {
        let m = unsafe { &amp;mut *self.map.get() };
        m.entry(v).or_insert_with(|| Box::new(self.default.clone()))
    }
}

#[test]
fn test() {
    let mut m = DefaultHashMap::new(10usize);
    *m.get_mut(4) = 40;
    let a = m.get(4);
    for i in 1..1024 {
        m.get(i);
    }
    assert_eq!(a, m.get(4));
    assert_eq!(40, *m.get(4));
}
</code></pre>

<p>(<a href="https://play.rust-lang.org/?gist=9a6b22174c512d23dc7d&amp;version=stable" rel="nofollow">Playground</a>)</p>