]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/dst-struct-sole.rs
:arrow_up: rust-analyzer
[rust.git] / src / tools / miri / tests / pass / dst-struct-sole.rs
1 // As dst-struct.rs, but the unsized field is the only field in the struct.
2
3 struct Fat<T: ?Sized> {
4     ptr: T,
5 }
6
7 // x is a fat pointer
8 fn foo(x: &Fat<[isize]>) {
9     let y = &x.ptr;
10     assert_eq!(x.ptr.len(), 3);
11     assert_eq!(y[0], 1);
12     assert_eq!(x.ptr[1], 2);
13 }
14
15 fn foo2<T: ToBar>(x: &Fat<[T]>) {
16     let y = &x.ptr;
17     let bar = Bar;
18     assert_eq!(x.ptr.len(), 3);
19     assert_eq!(y[0].to_bar(), bar);
20     assert_eq!(x.ptr[1].to_bar(), bar);
21 }
22
23 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
24 struct Bar;
25
26 trait ToBar {
27     fn to_bar(&self) -> Bar;
28 }
29
30 impl ToBar for Bar {
31     fn to_bar(&self) -> Bar {
32         *self
33     }
34 }
35
36 pub fn main() {
37     // With a vec of ints.
38     let f1 = Fat { ptr: [1, 2, 3] };
39     foo(&f1);
40     let f2 = &f1;
41     foo(f2);
42     let f3: &Fat<[isize]> = f2;
43     foo(f3);
44     let f4: &Fat<[isize]> = &f1;
45     foo(f4);
46     let f5: &Fat<[isize]> = &Fat { ptr: [1, 2, 3] };
47     foo(f5);
48
49     // With a vec of Bars.
50     let bar = Bar;
51     let f1 = Fat { ptr: [bar, bar, bar] };
52     foo2(&f1);
53     let f2 = &f1;
54     foo2(f2);
55     let f3: &Fat<[Bar]> = f2;
56     foo2(f3);
57     let f4: &Fat<[Bar]> = &f1;
58     foo2(f4);
59     let f5: &Fat<[Bar]> = &Fat { ptr: [bar, bar, bar] };
60     foo2(f5);
61
62     // Assignment.
63     let f5: &mut Fat<[isize]> = &mut Fat { ptr: [1, 2, 3] };
64     f5.ptr[1] = 34;
65     assert_eq!(f5.ptr[0], 1);
66     assert_eq!(f5.ptr[1], 34);
67     assert_eq!(f5.ptr[2], 3);
68
69     // Zero size vec.
70     let f5: &Fat<[isize]> = &Fat { ptr: [] };
71     assert!(f5.ptr.is_empty());
72     let f5: &Fat<[Bar]> = &Fat { ptr: [] };
73     assert!(f5.ptr.is_empty());
74 }