summary refs log blame commit diff stats
path: root/tests/ui/no_uaf_2.rs
blob: 53e3ad845b626c8f161168b2df335f928bbd1160 (plain) (tree)
1
2
3
4
5
6

              
                    

                    
                    














                                           
                                                                              

                                     
                        




                                                                                               
// by steffahn

use std::cell::Cell;

use selfref::opaque;
use selfref::Holder;

struct MyStruct<'this> {
    cell: Cell<&'this str>,
}

struct MyStructKey;

opaque! {
    impl Opaque for MyStructKey {
        type Kind<'this> = MyStruct<'this>;
    }
}

fn main() {
    let s =
        Holder::<'_, MyStructKey>::new_with(|builder| builder.build(MyStruct {
            cell: Default::default(),
        }));
    let s = Box::pin(s);
    s.as_ref().operate_in(|r| {
        r.cell.set(&String::from("hello world")); // temporary dropped at end of this statement
        println!("{}", r.cell.get()) // accesses dropped `String` data
    });
}