]>
Commit | Line | Data |
---|---|---|
92a42be0 SL |
1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | struct Foo<T: ?Sized> { | |
12 | a: u16, | |
13 | b: T | |
14 | } | |
15 | ||
16 | trait Bar { | |
17 | fn get(&self) -> usize; | |
18 | } | |
19 | ||
20 | impl Bar for usize { | |
21 | fn get(&self) -> usize { *self } | |
22 | } | |
23 | ||
24 | struct Baz<T: ?Sized> { | |
25 | a: T | |
26 | } | |
27 | ||
92a42be0 SL |
28 | struct HasDrop<T: ?Sized> { |
29 | ptr: Box<usize>, | |
30 | data: T | |
31 | } | |
32 | ||
33 | fn main() { | |
34 | // Test that zero-offset works properly | |
35 | let b : Baz<usize> = Baz { a: 7 }; | |
36 | assert_eq!(b.a.get(), 7); | |
37 | let b : &Baz<Bar> = &b; | |
38 | assert_eq!(b.a.get(), 7); | |
39 | ||
40 | // Test that the field is aligned properly | |
41 | let f : Foo<usize> = Foo { a: 0, b: 11 }; | |
42 | assert_eq!(f.b.get(), 11); | |
43 | let ptr1 : *const u8 = &f.b as *const _ as *const u8; | |
44 | ||
45 | let f : &Foo<Bar> = &f; | |
46 | let ptr2 : *const u8 = &f.b as *const _ as *const u8; | |
47 | assert_eq!(f.b.get(), 11); | |
48 | ||
49 | // The pointers should be the same | |
50 | assert_eq!(ptr1, ptr2); | |
51 | ||
92a42be0 SL |
52 | // Test that nested DSTs work properly |
53 | let f : Foo<Foo<usize>> = Foo { a: 0, b: Foo { a: 1, b: 17 }}; | |
54 | assert_eq!(f.b.b.get(), 17); | |
55 | let f : &Foo<Foo<Bar>> = &f; | |
56 | assert_eq!(f.b.b.get(), 17); | |
57 | ||
58 | // Test that get the pointer via destructuring works | |
59 | ||
60 | let f : Foo<usize> = Foo { a: 0, b: 11 }; | |
61 | let f : &Foo<Bar> = &f; | |
62 | let &Foo { a: _, b: ref bar } = f; | |
63 | assert_eq!(bar.get(), 11); | |
64 | ||
65 | // Make sure that drop flags don't screw things up | |
66 | ||
67 | let d : HasDrop<Baz<[i32; 4]>> = HasDrop { | |
68 | ptr: Box::new(0), | |
69 | data: Baz { a: [1,2,3,4] } | |
70 | }; | |
71 | assert_eq!([1,2,3,4], d.data.a); | |
72 | ||
73 | let d : &HasDrop<Baz<[i32]>> = &d; | |
74 | assert_eq!(&[1,2,3,4], &d.data.a); | |
75 | } |