moved, invalidating the result of deref(). ``` # use std::ops::Deref; struct Foo(Box); impl Deref for Foo { type Target = u8; fn deref(&self) -> &Self::Target { &*self.0 } } ``` Foo can safely implement StableDeref, due to the use of Box. ``` # use std::ops::Deref; # use std::ops::DerefMut; # use std::rc::Rc; #[derive(Clone)] struct Foo(Rc); impl Deref for Foo { type Target = u8; fn deref(&self) -> &Self::Target { &*self.0 } } impl DerefMut for Foo { fn deref_mut(&mut self) -> &mut Self::Target { Rc::make_mut(&mut self.0) } } ``` This is a simple implementation of copy-on-write: Foo's deref_mut will copy the underlying int if it is not uniquely owned, ensuring unique access at the point where deref_mut() returns. However, Foo cannot implement StableDeref because calling deref_mut(), followed by clone().deref() will result in mutable and immutable references to the same location. Note that if the DerefMut implementation were removed, Foo could safely implement StableDeref. Likewise, if the Clone implementation were removed, it would be safe to implement StableDeref, although Foo would not be very useful in that case, (without clones, the rc will always be uniquely owned). ``` # use std::ops::Deref; struct Foo; impl Deref for Foo { type Target = str; fn deref(&self) -> &Self::Target { &"Hello" } } ``` Foo can safely implement StableDeref. It doesn't own the data being derefed, but the data is gaurenteed to live long enough, due to it being 'static. ``` # use std::ops::Deref; # use std::cell::Cell; struct Foo(Cell); impl Deref for Foo { type Target = str; fn deref(&self) -> &Self::Target { let b = self.0.get(); self.0.set(!b); if b { &"Hello" } else { &"World" } } } ``` Foo cannot safely implement StableDeref, even though every possible result of deref lives long enough. In order to safely implement StableAddress, multiple calls to deref must return the same result. ``` # use std::ops::Deref; # use std::ops::DerefMut; struct Foo(Box<(u8, u8)>); impl Deref for Foo { type Target = u8; fn deref(&self) -> &Self::Target { &self.0.deref().0 } } impl DerefMut for Foo { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0.deref_mut().1 } } ``` Foo cannot implement StableDeref because deref and deref_mut return different addresses.