From: Ian Jackson Date: Fri, 26 Feb 2021 12:55:27 +0000 (+0000) Subject: utils: Loop: tidy up and provide try version X-Git-Tag: otter-0.4.0~351 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=9c4d0c72cacb9ec202ae15365d15ed5904119286;p=otter.git utils: Loop: tidy up and provide try version Signed-off-by: Ian Jackson --- diff --git a/src/utils.rs b/src/utils.rs index 7b38ccb8..e7c8d9c9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -306,23 +306,11 @@ impl From for Loop { pub trait IteratorExt<'f,U,E,F>: Iterator where F: 'f + FnMut(Self::Item) -> Result>, { - type R1: Iterator; - fn map_loop(self, f: F) -> Self::R1 where E: EmptyType; -/* - fn try_map_loop< - U, E, - F: FnMut(Self::Item) -> Result> - >(self, f: &mut F) -> impl Iterator> { - self - .map(f) - .filter(|i| matches!(i, Err(Loop::Continue))) - .take_while(|i| matches!(i, Err(Loop::Break))) - .map(|i| match i { - Ok(y) => Ok(y), - Err(Loop::Error(e)) => Err(e), - _ => panic!(), - }) - }*/ + type Return: Iterator; + fn map_loop(self, f: F) -> Self::Return where E: EmptyType; + + type TryReturn: Iterator>; + fn try_map_loop(self, f: F) -> Self::TryReturn; } pub trait EmptyType { fn diverge(self) -> T; } @@ -335,12 +323,25 @@ impl<'f,T,U,E,F> IteratorExt<'f,U,E,F> for T where T: 'f + Iterator, F: 'f + FnMut(Self::Item) -> Result>, { - type R1 = impl Iterator + 'f; - fn map_loop(self, f: F) -> Self::R1 where E: EmptyType { + type Return = impl Iterator + 'f; + fn map_loop(self, f: F) -> Self::Return where E: EmptyType { self .map(f) .filter(|i| !matches!(i, Err(Loop::Continue))) .take_while(|i| !matches!(i, Err(Loop::Break))) .map(|i| i.ok().unwrap()) } + + type TryReturn = impl Iterator> + 'f; + fn try_map_loop(self, f: F) -> Self::TryReturn { + self + .map(f) + .filter(|i| matches!(i, Err(Loop::Continue))) + .take_while(|i| matches!(i, Err(Loop::Break))) + .map(|i| match i { + Ok(y) => Ok(y), + Err(Loop::Error(e)) => Err(e), + _ => panic!(), + }) + } }