|
| 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 | +struct StateMachineIter<'a> { |
| 12 | + statefn: &'a fn(&mut StateMachineIter<'a>) -> Option<&'static str> |
| 13 | +} |
| 14 | + |
| 15 | +impl<'a> Iterator<&'static str> for StateMachineIter<'a> { |
| 16 | + fn next(&mut self) -> Option<&'static str> { |
| 17 | + return (*self.statefn)(self); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +fn state1(self_: &mut StateMachineIter) -> Option<&'static str> { |
| 22 | + self_.statefn = &state2; |
| 23 | + //~^ ERROR borrowed value does not live long enough |
| 24 | + return Some("state1"); |
| 25 | +} |
| 26 | + |
| 27 | +fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> { |
| 28 | + self_.statefn = &state3; |
| 29 | + //~^ ERROR borrowed value does not live long enough |
| 30 | + return Some("state2"); |
| 31 | +} |
| 32 | + |
| 33 | +fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> { |
| 34 | + self_.statefn = &finished; |
| 35 | + //~^ ERROR borrowed value does not live long enough |
| 36 | + return Some("state3"); |
| 37 | +} |
| 38 | + |
| 39 | +fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> { |
| 40 | + return None; |
| 41 | +} |
| 42 | + |
| 43 | +fn state_iter() -> StateMachineIter<'static> { |
| 44 | + StateMachineIter { |
| 45 | + statefn: &state1 //~ ERROR borrowed value does not live long enough |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +fn main() { |
| 51 | + let mut it = state_iter(); |
| 52 | + println!("{}",it.next()); |
| 53 | + println!("{}",it.next()); |
| 54 | + println!("{}",it.next()); |
| 55 | + println!("{}",it.next()); |
| 56 | + println!("{}",it.next()); |
| 57 | +} |
| 58 | + |
0 commit comments