1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
pub trait TailRec<Output, F> { #[inline] fn rec(self, iterate: F) -> Output where Self: Sized, F: Fn(Self) -> Result<Output, Self> { let mut state = iterate(self); loop { match state { Ok (done) => { return done } Err(more) => { state = iterate(more); } }} } #[inline] fn rec_ref<'a>(&'a self, iterate: F) -> Output where F: for<'r> Fn(&'r Self) -> Result<Output, &'r Self> { let mut state = iterate(self); loop { match state { Ok (done) => { return done } Err(more) => { state = iterate(more); } }} } #[inline] fn rec_mut(self, mut iterate: F) -> Output where Self: Sized, F: FnMut(Self) -> Result<Output, Self> { let mut state = iterate(self); loop { match state { Ok (done) => { return done } Err(more) => { state = iterate(more); } }} } #[inline] fn rec_ref_mut<'a>(&'a mut self, mut iterate: F) -> Output where F: for<'r> FnMut(&'r mut Self) -> Result<Output, &'r mut Self> { let mut state = iterate(self); loop { match state { Ok (done) => { return done } Err(more) => { state = iterate(more); } }} } } impl<Output, F, State> TailRec<Output, F> for State {}