]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/dst-tuple-sole.rs
New upstream version 1.20.0+dfsg1
[rustc.git] / src / test / run-pass / dst-tuple-sole.rs
1 // Copyright 2014 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 // As dst-tuple.rs, but the unsized field is the only field in the tuple.
12
13
14 #![feature(unsized_tuple_coercion)]
15
16 type Fat<T: ?Sized> = (T,);
17
18 // x is a fat pointer
19 fn foo(x: &Fat<[isize]>) {
20 let y = &x.0;
21 assert_eq!(x.0.len(), 3);
22 assert_eq!(y[0], 1);
23 assert_eq!(x.0[1], 2);
24 }
25
26 fn foo2<T:ToBar>(x: &Fat<[T]>) {
27 let y = &x.0;
28 let bar = Bar;
29 assert_eq!(x.0.len(), 3);
30 assert_eq!(y[0].to_bar(), bar);
31 assert_eq!(x.0[1].to_bar(), bar);
32 }
33
34 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
35 struct Bar;
36
37 trait ToBar {
38 fn to_bar(&self) -> Bar;
39 }
40
41 impl ToBar for Bar {
42 fn to_bar(&self) -> Bar {
43 *self
44 }
45 }
46
47 pub fn main() {
48 // With a vec of ints.
49 let f1 = ([1, 2, 3],);
50 foo(&f1);
51 let f2 = &f1;
52 foo(f2);
53 let f3: &Fat<[isize]> = f2;
54 foo(f3);
55 let f4: &Fat<[isize]> = &f1;
56 foo(f4);
57 let f5: &Fat<[isize]> = &([1, 2, 3],);
58 foo(f5);
59
60 // With a vec of Bars.
61 let bar = Bar;
62 let f1 = ([bar, bar, bar],);
63 foo2(&f1);
64 let f2 = &f1;
65 foo2(f2);
66 let f3: &Fat<[Bar]> = f2;
67 foo2(f3);
68 let f4: &Fat<[Bar]> = &f1;
69 foo2(f4);
70 let f5: &Fat<[Bar]> = &([bar, bar, bar],);
71 foo2(f5);
72
73 // Assignment.
74 let f5: &mut Fat<[isize]> = &mut ([1, 2, 3],);
75 f5.0[1] = 34;
76 assert_eq!(f5.0[0], 1);
77 assert_eq!(f5.0[1], 34);
78 assert_eq!(f5.0[2], 3);
79
80 // Zero size vec.
81 let f5: &Fat<[isize]> = &([],);
82 assert!(f5.0.is_empty());
83 let f5: &Fat<[Bar]> = &([],);
84 assert!(f5.0.is_empty());
85 }