Skip to content

Commit a806805

Browse files
committed
add an interesting testcase
1 parent 06d7773 commit a806805

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#[repr(C)]
2+
#[derive(Debug)]
3+
struct PairFoo {
4+
fst: Foo,
5+
snd: Foo,
6+
}
7+
8+
#[derive(Debug)]
9+
struct Foo(u64);
10+
fn reinterstruct(box_pair: Box<PairFoo>) -> Vec<Foo> {
11+
let ref_pair = Box::leak(box_pair) as *mut PairFoo;
12+
let ptr_foo = unsafe { &mut (*ref_pair).fst as *mut Foo };
13+
unsafe {
14+
Vec::from_raw_parts(ptr_foo, 2, 2)
15+
}
16+
}
17+
18+
fn main() {
19+
let pair_foo = Box::new(PairFoo {
20+
fst: Foo(42),
21+
snd: Foo(1337),
22+
});
23+
println!("pair_foo = {:?}", pair_foo);
24+
for (n, foo) in reinterstruct(pair_foo).into_iter().enumerate() {
25+
println!("foo #{} = {:?}", n, foo);
26+
}
27+
}
28+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pair_foo = PairFoo { fst: Foo(42), snd: Foo(1337) }
2+
foo #0 = Foo(42)
3+
foo #1 = Foo(1337)

0 commit comments

Comments
 (0)