chiark / gitweb /
utils: Loop: tidy up and provide try version
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 12:55:27 +0000 (12:55 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 12:55:27 +0000 (12:55 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/utils.rs

index 7b38ccb8aae2fcb957db71c6b5d6d852583f7fe4..e7c8d9c9556c4eeaf5edef7189e3b707ebda4535 100644 (file)
@@ -306,23 +306,11 @@ impl<E> From<E> for Loop<E> {
 pub trait IteratorExt<'f,U,E,F>: Iterator
   where F: 'f + FnMut(Self::Item) -> Result<U,Loop<E>>,
 {
-  type R1: Iterator<Item=U>;
-  fn map_loop(self, f: F) -> Self::R1 where E: EmptyType;
-/*
-  fn try_map_loop<
-    U, E,
-    F: FnMut(Self::Item) -> Result<U,Loop<E>>
-  >(self, f: &mut F) -> impl Iterator<Result<U>> {
-    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<Item=U>;
+  fn map_loop(self, f: F) -> Self::Return where E: EmptyType;
+
+  type TryReturn: Iterator<Item=Result<U,E>>;
+  fn try_map_loop(self, f: F) -> Self::TryReturn;
 }
 
 pub trait EmptyType { fn diverge<T>(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<U,Loop<E>>,
 {
-  type R1 = impl Iterator<Item=U> + 'f;
-  fn map_loop(self, f: F) -> Self::R1 where E: EmptyType {
+  type Return = impl Iterator<Item=U> + '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<Item=Result<U,E>> + '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!(),
+      })
+  }
 }