2 struct Foo<T: ?Sized> {
8 fn get(&self) -> usize;
12 fn get(&self) -> usize {
17 struct Baz<T: ?Sized> {
22 struct HasDrop<T: ?Sized> {
28 // Test that zero-offset works properly
29 let b: Baz<usize> = Baz { a: 7 };
30 assert_eq!(b.a.get(), 7);
31 let b: &Baz<dyn Bar> = &b;
32 assert_eq!(b.a.get(), 7);
34 // Test that the field is aligned properly
35 let f: Foo<usize> = Foo { a: 0, b: 11 };
36 assert_eq!(f.b.get(), 11);
37 let ptr1: *const u8 = &f.b as *const _ as *const u8;
39 let f: &Foo<dyn Bar> = &f;
40 let ptr2: *const u8 = &f.b as *const _ as *const u8;
41 assert_eq!(f.b.get(), 11);
43 // The pointers should be the same
44 assert_eq!(ptr1, ptr2);
46 // Test that nested DSTs work properly
47 let f: Foo<Foo<usize>> = Foo { a: 0, b: Foo { a: 1, b: 17 } };
48 assert_eq!(f.b.b.get(), 17);
49 let f: &Foo<Foo<dyn Bar>> = &f;
50 assert_eq!(f.b.b.get(), 17);
52 // Test that get the pointer via destructuring works
54 let f: Foo<usize> = Foo { a: 0, b: 11 };
55 let f: &Foo<dyn Bar> = &f;
56 let &Foo { a: _, b: ref bar } = f;
57 assert_eq!(bar.get(), 11);
59 // Make sure that drop flags don't screw things up
61 let d: HasDrop<Baz<[i32; 4]>> = HasDrop { ptr: Box::new(0), data: Baz { a: [1, 2, 3, 4] } };
62 assert_eq!([1, 2, 3, 4], d.data.a);
64 let d: &HasDrop<Baz<[i32]>> = &d;
65 assert_eq!(&[1, 2, 3, 4], &d.data.a);