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