blob: eb6b7d38aebe52464c437ec20a64f25748681592 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
// by steffahn
#![feature(pin_macro)]
use std::cell::Cell;
use std::pin::pin;
use selfref::opaque;
use selfref::{new_with_closure, 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(new_with_closure::<MyStructKey, _>(|[]| MyStruct {
cell: Default::default(),
}));
let s = 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
});
}
|