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