File tree Expand file tree Collapse file tree 3 files changed +60
-28
lines changed Expand file tree Collapse file tree 3 files changed +60
-28
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ #![ feature( ptr_internals) ]
2+
3+ fn main ( ) {
4+ into_raw ( ) ;
5+ into_unique ( ) ;
6+ boxed_pair_to_vec ( ) ;
7+ }
8+
9+ fn into_raw ( ) { unsafe {
10+ let b = Box :: new ( 4i32 ) ;
11+ let r = Box :: into_raw ( b) ;
12+
13+ // "lose the tag"
14+ let r2 = ( ( r as usize ) +0 ) as * mut i32 ;
15+ * ( & mut * r2) = 7 ;
16+
17+ // Use original ptr again
18+ * ( & mut * r) = 17 ;
19+ drop ( Box :: from_raw ( r) ) ;
20+ } }
21+
22+ fn into_unique ( ) { unsafe {
23+ let b = Box :: new ( 4i32 ) ;
24+ let u = Box :: into_unique ( b) ;
25+
26+ // "lose the tag"
27+ let r = ( ( u. as_ptr ( ) as usize ) +0 ) as * mut i32 ;
28+ * ( & mut * r) = 7 ;
29+
30+ // Use original ptr again.
31+ drop ( Box :: from_raw ( u. as_ptr ( ) ) ) ;
32+ } }
33+
34+ fn boxed_pair_to_vec ( ) {
35+ #[ repr( C ) ]
36+ #[ derive( Debug ) ]
37+ struct PairFoo {
38+ fst : Foo ,
39+ snd : Foo ,
40+ }
41+
42+ #[ derive( Debug ) ]
43+ struct Foo ( u64 ) ;
44+ fn reinterstruct ( box_pair : Box < PairFoo > ) -> Vec < Foo > {
45+ let ref_pair = Box :: leak ( box_pair) as * mut PairFoo ;
46+ let ptr_foo = unsafe { & mut ( * ref_pair) . fst as * mut Foo } ;
47+ unsafe {
48+ Vec :: from_raw_parts ( ptr_foo, 2 , 2 )
49+ }
50+ }
51+
52+ let pair_foo = Box :: new ( PairFoo {
53+ fst : Foo ( 42 ) ,
54+ snd : Foo ( 1337 ) ,
55+ } ) ;
56+ println ! ( "pair_foo = {:?}" , pair_foo) ;
57+ for ( n, foo) in reinterstruct ( pair_foo) . into_iter ( ) . enumerate ( ) {
58+ println ! ( "foo #{} = {:?}" , n, foo) ;
59+ }
60+ }
File renamed without changes.
You can’t perform that action at this time.
0 commit comments