]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/regions-early-bound-trait-param.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / test / run-pass / regions-early-bound-trait-param.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 // Tests that you can use an early-bound lifetime parameter as
12 // on of the generic parameters in a trait.
13
14 #![allow(unknown_features)]
15 #![feature(box_syntax)]
16
17 trait Trait<'a> {
18     fn long(&'a self) -> int;
19     fn short<'b>(&'b self) -> int;
20 }
21
22 fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
23     let l = x.long();
24     let s = x.short();
25     (l,s)
26 }
27
28 fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
29     let l = x.long();
30     let s = x.short();
31     (l,s)
32 }
33
34 struct Struct1<'e> {
35     f: &'e (Trait<'e>+'e)
36 }
37
38 fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
39     let l = x.f.long();
40     let s = x.f.short();
41     (l,s)
42 }
43
44 struct Struct2<'h, 'i> {
45     f: &'h (Trait<'i>+'h)
46 }
47
48 fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
49     x.short()
50 }
51
52 fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
53     x.f.short()
54 }
55
56 trait MakerTrait<'o> {
57     fn mk() -> Self;
58 }
59
60 fn make_val<'p, T:MakerTrait<'p>>() -> T {
61     MakerTrait::mk()
62 }
63
64 trait RefMakerTrait<'q> {
65     fn mk(Self) -> &'q Self;
66 }
67
68 fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
69     RefMakerTrait::mk(t)
70 }
71
72 impl<'s> Trait<'s> for (int,int) {
73     fn long(&'s self) -> int {
74         let &(x,_) = self;
75         x
76     }
77     fn short<'b>(&'b self) -> int {
78         let &(_,y) = self;
79         y
80     }
81 }
82
83 impl<'t> MakerTrait<'t> for Box<Trait<'t>+'static> {
84     fn mk() -> Box<Trait<'t>+'static> { box() (4,5) as Box<Trait> }
85 }
86
87 enum List<'l> {
88     Cons(int, &'l List<'l>),
89     Null
90 }
91
92 impl<'l> List<'l> {
93     fn car<'m>(&'m self) -> int {
94         match self {
95             &List::Cons(car, _) => car,
96             &List::Null => panic!(),
97         }
98     }
99     fn cdr<'n>(&'n self) -> &'l List<'l> {
100         match self {
101             &List::Cons(_, cdr) => cdr,
102             &List::Null => panic!(),
103         }
104     }
105 }
106
107 impl<'t> RefMakerTrait<'t> for List<'t> {
108     fn mk(l:List<'t>) -> &'t List<'t> {
109         l.cdr()
110     }
111 }
112
113 pub fn main() {
114     let t = (2,3);
115     let o = &t as &Trait;
116     let s1 = Struct1 { f: o };
117     let s2 = Struct2 { f: o };
118     assert_eq!(poly_invoke(&t), (2,3));
119     assert_eq!(object_invoke1(&t), (2,3));
120     assert_eq!(field_invoke1(&s1), (2,3));
121     assert_eq!(object_invoke2(&t), 3);
122     assert_eq!(field_invoke2(&s2), 3);
123
124     let m : Box<Trait> = make_val();
125     assert_eq!(object_invoke1(&*m), (4,5));
126     assert_eq!(object_invoke2(&*m), 5);
127
128     // The RefMakerTrait above is pretty strange (i.e. it is strange
129     // to consume a value of type T and return a &T).  Easiest thing
130     // that came to my mind: consume a cell of a linked list and
131     // return a reference to the list it points to.
132     let l0 = List::Null;
133     let l1 = List::Cons(1, &l0);
134     let l2 = List::Cons(2, &l1);
135     let rl1 = &l1;
136     let r  = make_ref(l2);
137     assert_eq!(rl1.car(), r.car());
138 }