]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
3b2f2976 XL |
11 | fn id<T>(x: T) -> T { x } |
12 | ||
1a4d82fc JJ |
13 | struct StateMachineIter<'a> { |
14 | statefn: &'a StateMachineFunc<'a> | |
15 | } | |
16 | ||
17 | type StateMachineFunc<'a> = fn(&mut StateMachineIter<'a>) -> Option<&'static str>; | |
18 | ||
19 | impl<'a> Iterator for StateMachineIter<'a> { | |
20 | type Item = &'static str; | |
21 | ||
22 | fn next(&mut self) -> Option<&'static str> { | |
23 | return (*self.statefn)(self); | |
24 | } | |
25 | } | |
26 | ||
27 | fn state1(self_: &mut StateMachineIter) -> Option<&'static str> { | |
3b2f2976 | 28 | self_.statefn = &id(state2 as StateMachineFunc); |
1a4d82fc JJ |
29 | //~^ ERROR borrowed value does not live long enough |
30 | return Some("state1"); | |
31 | } | |
32 | ||
33 | fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> { | |
3b2f2976 | 34 | self_.statefn = &id(state3 as StateMachineFunc); |
1a4d82fc JJ |
35 | //~^ ERROR borrowed value does not live long enough |
36 | return Some("state2"); | |
37 | } | |
38 | ||
39 | fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> { | |
3b2f2976 | 40 | self_.statefn = &id(finished as StateMachineFunc); |
1a4d82fc JJ |
41 | //~^ ERROR borrowed value does not live long enough |
42 | return Some("state3"); | |
43 | } | |
44 | ||
45 | fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> { | |
46 | return None; | |
47 | } | |
48 | ||
49 | fn state_iter() -> StateMachineIter<'static> { | |
50 | StateMachineIter { | |
3b2f2976 XL |
51 | statefn: &id(state1 as StateMachineFunc) |
52 | //~^ ERROR borrowed value does not live long enough | |
1a4d82fc JJ |
53 | } |
54 | } | |
55 | ||
56 | ||
57 | fn main() { | |
58 | let mut it = state_iter(); | |
59 | println!("{:?}",it.next()); | |
60 | println!("{:?}",it.next()); | |
61 | println!("{:?}",it.next()); | |
62 | println!("{:?}",it.next()); | |
63 | println!("{:?}",it.next()); | |
64 | } |