windows file format device independent bitmap dib / bmp decoding and encoding
Diffstat (limited to 'src/until.rs')
-rw-r--r--src/until.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/until.rs b/src/until.rs
new file mode 100644
index 0000000..45f2491
--- /dev/null
+++ b/src/until.rs
@@ -0,0 +1,42 @@
+//! “Lifted” from itertools
+pub struct UntilDone<'a, I, E: 'a> {
+ error: &'a mut Result<(), E>,
+ iter: I,
+}
+
+impl<'a, I, T, E> Iterator for UntilDone<'a, I, E>
+where
+ I: Iterator<Item = Result<T, E>>,
+{
+ type Item = T;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ match self.iter.next() {
+ Some(Ok(x)) => Some(x),
+ Some(Err(e)) => {
+ *self.error = Err(e);
+ None
+ }
+ None => None,
+ }
+ }
+}
+
+pub trait Until<T> {
+ /// “Lift” a function of the values of an iterator so that it can process
+ /// an iterator of `Result` values instead.
+ fn until_done<E, R>(self, f: impl FnOnce(UntilDone<Self, E>) -> R) -> Result<R, E>;
+}
+
+impl<T, I: Iterator<Item = T>> Until<T> for I {
+ fn until_done<E, R>(self, f: impl FnOnce(UntilDone<Self, E>) -> R) -> Result<R, E> {
+ let mut error = Ok(());
+
+ let result = f(UntilDone {
+ error: &mut error,
+ iter: self,
+ });
+
+ error.map(|_| result)
+ }
+}