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.
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.
11 // Tests that you can use an early-bound lifetime parameter as
12 // on of the generic parameters in a trait.
15 fn long(&'a self) -> int;
16 fn short<'b>(&'b self) -> int;
19 fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
25 fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
35 fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
41 struct Struct2<'h, 'i> {
45 fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
49 fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
53 trait MakerTrait<'o> {
57 fn make_val<'p, T:MakerTrait<'p>>() -> T {
61 trait RefMakerTrait<'q> {
62 fn mk(Self) -> &'q Self;
65 fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
69 impl<'s> Trait<'s> for (int,int) {
70 fn long(&'s self) -> int {
74 fn short<'b>(&'b self) -> int {
80 impl<'t> MakerTrait<'t> for ~Trait<'t> {
81 fn mk() -> ~Trait<'t> { ~(4,5) as ~Trait }
85 Cons(int, &'l List<'l>),
90 fn car<'m>(&'m self) -> int {
96 fn cdr<'n>(&'n self) -> &'l List<'l> {
104 impl<'t> RefMakerTrait<'t> for List<'t> {
105 fn mk(l:List<'t>) -> &'t List<'t> {
112 let o = &t as &Trait;
113 let s1 = Struct1 { f: o };
114 let s2 = Struct2 { f: o };
115 assert_eq!(poly_invoke(&t), (2,3));
116 assert_eq!(object_invoke1(&t), (2,3));
117 assert_eq!(field_invoke1(&s1), (2,3));
118 assert_eq!(object_invoke2(&t), 3);
119 assert_eq!(field_invoke2(&s2), 3);
121 let m : ~Trait = make_val();
122 assert_eq!(object_invoke1(m), (4,5));
123 assert_eq!(object_invoke2(m), 5);
125 // The RefMakerTrait above is pretty strange (i.e. it is strange
126 // to consume a value of type T and return a &T). Easiest thing
127 // that came to my mind: consume a cell of a linked list and
128 // return a reference to the list it points to.
130 let l1 = Cons(1, &l0);
131 let l2 = Cons(2, &l1);
133 let r = make_ref(l2);
134 assert_eq!(rl1.car(), r.car());