Something that this other thread made me realise:
This can be avoided with the unsafe
(and unstable) #[may_dangle]
attribute:
#![feature(dropck_eyepatch)]
pub
struct Foo<'a, T> /* = */ (&'a mut T);
unsafe impl<#[may_dangle] 'a, T : 'a>
Drop for Foo<'a, T>
{
fn drop (self: &'_ mut Self)
{
// this does not dereference self.0 (it just prints the raw address of the pointee), so it is fine
println!("Dropping Foo({:p})", self.0);
}
}
fn main ()
{
let mut x = 5;
println!("x at {:p}", &x);
let foo = Foo(&mut x);
println!("Created Foo(&mut x = {:p})", foo.0);
println!("Accessing x directly: {}", x);
}
x at 0x7fff656e20d4
Created Foo(&mut x = 0x7fff656e20d4)
Accessing x directly: 5
Dropping Foo(0x7fff656e20d4)