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.
14 #![allow(unknown_features)]
15 #![feature(box_syntax)]
18 fn long(&'a self) -> int;
19 fn short<'b>(&'b self) -> int;
22 fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
28 fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
38 fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
44 struct Struct2<'h, 'i> {
48 fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
52 fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
56 trait MakerTrait<'o> {
60 fn make_val<'p, T:MakerTrait<'p>>() -> T {
64 trait RefMakerTrait<'q> {
65 fn mk(Self) -> &'q Self;
68 fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
72 impl<'s> Trait<'s> for (int,int) {
73 fn long(&'s self) -> int {
77 fn short<'b>(&'b self) -> int {
83 impl<'t> MakerTrait<'t> for Box<Trait<'t>+'static> {
84 fn mk() -> Box<Trait<'t>+'static> { box() (4,5) as Box<Trait> }
88 Cons(int, &'l List<'l>),
93 fn car<'m>(&'m self) -> int {
95 &List::Cons(car, _) => car,
96 &List::Null => panic!(),
99 fn cdr<'n>(&'n self) -> &'l List<'l> {
101 &List::Cons(_, cdr) => cdr,
102 &List::Null => panic!(),
107 impl<'t> RefMakerTrait<'t> for List<'t> {
108 fn mk(l:List<'t>) -> &'t List<'t> {
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);
124 let m : Box<Trait> = make_val();
125 assert_eq!(object_invoke1(&*m), (4,5));
126 assert_eq!(object_invoke2(&*m), 5);
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.
133 let l1 = List::Cons(1, &l0);
134 let l2 = List::Cons(2, &l1);
136 let r = make_ref(l2);
137 assert_eq!(rl1.car(), r.car());