Skip to content

Commit af0124b

Browse files
committed
---
yaml --- r: 150365 b: refs/heads/try2 c: 0cbb1ce h: refs/heads/master i: 150363: a275529 v: v3
1 parent 656cb91 commit af0124b

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: ab8e02616cb3f2608208feea9627cb981377ac0d
8+
refs/heads/try2: 0cbb1ceaa99dd7065b1237357e828c57ef4135c9
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
trait Trait<'a> {
15+
fn long(&'a self) -> int;
16+
fn short<'b>(&'b self) -> int;
17+
}
18+
19+
fn poly_invoke<'c, T: Trait<'c>>(x: &'c T) -> (int, int) {
20+
let l = x.long();
21+
let s = x.short();
22+
(l,s)
23+
}
24+
25+
fn object_invoke1<'d>(x: &'d Trait<'d>) -> (int, int) {
26+
let l = x.long();
27+
let s = x.short();
28+
(l,s)
29+
}
30+
31+
struct Struct1<'e> {
32+
f: &'e Trait<'e>
33+
}
34+
35+
fn field_invoke1<'f, 'g>(x: &'g Struct1<'f>) -> (int,int) {
36+
let l = x.f.long();
37+
let s = x.f.short();
38+
(l,s)
39+
}
40+
41+
struct Struct2<'h, 'i> {
42+
f: &'h Trait<'i>
43+
}
44+
45+
fn object_invoke2<'j, 'k>(x: &'k Trait<'j>) -> int {
46+
x.short()
47+
}
48+
49+
fn field_invoke2<'l, 'm, 'n>(x: &'n Struct2<'l,'m>) -> int {
50+
x.f.short()
51+
}
52+
53+
trait MakerTrait<'o> {
54+
fn mk() -> Self;
55+
}
56+
57+
fn make_val<'p, T:MakerTrait<'p>>() -> T {
58+
MakerTrait::mk()
59+
}
60+
61+
trait RefMakerTrait<'q> {
62+
fn mk(Self) -> &'q Self;
63+
}
64+
65+
fn make_ref<'r, T:RefMakerTrait<'r>>(t:T) -> &'r T {
66+
RefMakerTrait::mk(t)
67+
}
68+
69+
impl<'s> Trait<'s> for (int,int) {
70+
fn long(&'s self) -> int {
71+
let &(x,_) = self;
72+
x
73+
}
74+
fn short<'b>(&'b self) -> int {
75+
let &(_,y) = self;
76+
y
77+
}
78+
}
79+
80+
impl<'t> MakerTrait<'t> for ~Trait<'t> {
81+
fn mk() -> ~Trait<'t> { ~(4,5) as ~Trait }
82+
}
83+
84+
enum List<'l> {
85+
Cons(int, &'l List<'l>),
86+
Null
87+
}
88+
89+
impl<'l> List<'l> {
90+
fn car<'m>(&'m self) -> int {
91+
match self {
92+
&Cons(car, _) => car,
93+
&Null => fail!(),
94+
}
95+
}
96+
fn cdr<'n>(&'n self) -> &'l List<'l> {
97+
match self {
98+
&Cons(_, cdr) => cdr,
99+
&Null => fail!(),
100+
}
101+
}
102+
}
103+
104+
impl<'t> RefMakerTrait<'t> for List<'t> {
105+
fn mk(l:List<'t>) -> &'t List<'t> {
106+
l.cdr()
107+
}
108+
}
109+
110+
pub fn main() {
111+
let t = (2,3);
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);
120+
121+
let m : ~Trait = make_val();
122+
assert_eq!(object_invoke1(m), (4,5));
123+
assert_eq!(object_invoke2(m), 5);
124+
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.
129+
let l0 = Null;
130+
let l1 = Cons(1, &l0);
131+
let l2 = Cons(2, &l1);
132+
let rl1 = &l1;
133+
let r = make_ref(l2);
134+
assert_eq!(rl1.car(), r.car());
135+
}

0 commit comments

Comments
 (0)