]>
Commit | Line | Data |
---|---|---|
1 | // run-pass | |
2 | #![allow(dead_code)] | |
3 | struct Foo<T: ?Sized> { | |
4 | a: u16, | |
5 | b: T | |
6 | } | |
7 | ||
8 | trait Bar { | |
9 | fn get(&self) -> usize; | |
10 | } | |
11 | ||
12 | impl Bar for usize { | |
13 | fn get(&self) -> usize { *self } | |
14 | } | |
15 | ||
16 | struct Baz<T: ?Sized> { | |
17 | a: T | |
18 | } | |
19 | ||
20 | struct HasDrop<T: ?Sized> { | |
21 | ptr: Box<usize>, | |
22 | data: T | |
23 | } | |
24 | ||
25 | fn main() { | |
26 | // Test that zero-offset works properly | |
27 | let b : Baz<usize> = Baz { a: 7 }; | |
28 | assert_eq!(b.a.get(), 7); | |
29 | let b : &Baz<dyn Bar> = &b; | |
30 | assert_eq!(b.a.get(), 7); | |
31 | ||
32 | // Test that the field is aligned properly | |
33 | let f : Foo<usize> = Foo { a: 0, b: 11 }; | |
34 | assert_eq!(f.b.get(), 11); | |
35 | let ptr1 : *const u8 = &f.b as *const _ as *const u8; | |
36 | ||
37 | let f : &Foo<dyn Bar> = &f; | |
38 | let ptr2 : *const u8 = &f.b as *const _ as *const u8; | |
39 | assert_eq!(f.b.get(), 11); | |
40 | ||
41 | // The pointers should be the same | |
42 | assert_eq!(ptr1, ptr2); | |
43 | ||
44 | // Test that nested DSTs work properly | |
45 | let f : Foo<Foo<usize>> = Foo { a: 0, b: Foo { a: 1, b: 17 }}; | |
46 | assert_eq!(f.b.b.get(), 17); | |
47 | let f : &Foo<Foo<dyn Bar>> = &f; | |
48 | assert_eq!(f.b.b.get(), 17); | |
49 | ||
50 | // Test that get the pointer via destructuring works | |
51 | ||
52 | let f : Foo<usize> = Foo { a: 0, b: 11 }; | |
53 | let f : &Foo<dyn Bar> = &f; | |
54 | let &Foo { a: _, b: ref bar } = f; | |
55 | assert_eq!(bar.get(), 11); | |
56 | ||
57 | // Make sure that drop flags don't screw things up | |
58 | ||
59 | let d : HasDrop<Baz<[i32; 4]>> = HasDrop { | |
60 | ptr: Box::new(0), | |
61 | data: Baz { a: [1,2,3,4] } | |
62 | }; | |
63 | assert_eq!([1,2,3,4], d.data.a); | |
64 | ||
65 | let d : &HasDrop<Baz<[i32]>> = &d; | |
66 | assert_eq!(&[1,2,3,4], &d.data.a); | |
67 | } |