| @@ -4,15 +4,54 @@ use std::{ · 3 unmodified lines |
| 4 | sync::atomic::{AtomicBool, Ordering}, | 4 | sync::atomic::{AtomicBool, Ordering}, |
| 5 | }; | 5 | }; |
| 6 | | 6 | |
| | 7 | /// A wrapper for an inner iterator which will check for interruptions on each iteration, stopping the iteration when |
| | 8 | /// that is requested. |
| | 9 | pub struct Iter<'a, I> { |
| | 10 | /// The actual iterator to yield elements from. |
| | 11 | pub inner: I, |
| | 12 | should_interrupt: &'a AtomicBool, |
| | 13 | } |
| | 14 | |
| | 15 | impl<'a, I> Iter<'a, I> |
| | 16 | where |
| | 17 | I: Iterator, |
| | 18 | { |
| | 19 | /// Create a new iterator over `inner` which checks for interruptions on each iteration on `should_interrupt`. |
| | 20 | /// |
| | 21 | /// Note that this means the consumer of the iterator data should also be able to access `should_interrupt` and |
| | 22 | /// consider it when producing the final result to avoid claiming success even though the operation is only partially |
| | 23 | /// complete. |
| | 24 | pub fn new(inner: I, should_interrupt: &'a AtomicBool) -> Self { |
| | 25 | Iter { |
| | 26 | inner, |
| | 27 | should_interrupt, |
| | 28 | } |
| | 29 | } |
| | 30 | } |
| | 31 | |
| | 32 | impl<'a, I> Iterator for Iter<'a, I> |
| | 33 | where |
| | 34 | I: Iterator, |
| | 35 | { |
| | 36 | type Item = I::Item; |
| | 37 | |
| | 38 | fn next(&mut self) -> Option<Self::Item> { |
| | 39 | if self.should_interrupt.load(Ordering::Relaxed) { |
| | 40 | return None; |
| | 41 | } |
| | 42 | self.inner.next() |
| | 43 | } |
| | 44 | } |
| | 45 | |
| 7 | /// A wrapper for an inner iterator which will check for interruptions on each iteration. | 46 | /// A wrapper for an inner iterator which will check for interruptions on each iteration. |
| 8 | pub struct Iter<'a, I, EFN> { | 47 | pub struct IterWithErr<'a, I, EFN> { |
| 9 | /// The actual iterator to yield elements from. | 48 | /// The actual iterator to yield elements from. |
| 10 | pub inner: I, | 49 | pub inner: I, |
| 11 | make_err: Option<EFN>, | 50 | make_err: Option<EFN>, |
| 12 | should_interrupt: &'a AtomicBool, | 51 | should_interrupt: &'a AtomicBool, |
| 13 | } | 52 | } |
| 14 | | 53 | |
| 15 | impl<'a, I, EFN, E> Iter<'a, I, EFN> | 54 | impl<'a, I, EFN, E> IterWithErr<'a, I, EFN> |
| 16 | where | 55 | where |
| 17 | I: Iterator, | 56 | I: Iterator, |
| 18 | EFN: FnOnce() -> E, | 57 | EFN: FnOnce() -> E, |
| @@ -20,7 +59,7 @@ where · 1 unmodified line |
| 20 | /// Create a new iterator over `inner` which checks for interruptions on each iteration and cals `make_err()` to | 59 | /// Create a new iterator over `inner` which checks for interruptions on each iteration and cals `make_err()` to |
| 21 | /// signal an interruption happened, causing no further items to be iterated from that point on. | 60 | /// signal an interruption happened, causing no further items to be iterated from that point on. |
| 22 | pub fn new(inner: I, make_err: EFN, should_interrupt: &'a AtomicBool) -> Self { | 61 | pub fn new(inner: I, make_err: EFN, should_interrupt: &'a AtomicBool) -> Self { |
| 23 | Iter { | 62 | IterWithErr { |
| 24 | inner, | 63 | inner, |
| 25 | make_err: Some(make_err), | 64 | make_err: Some(make_err), |
| 26 | should_interrupt, | 65 | should_interrupt, |
| @@ -28,7 +67,7 @@ where · 1 unmodified line |
| 28 | } | 67 | } |
| 29 | } | 68 | } |
| 30 | | 69 | |
| 31 | impl<'a, I, EFN, E> Iterator for Iter<'a, I, EFN> | 70 | impl<'a, I, EFN, E> Iterator for IterWithErr<'a, I, EFN> |
| 32 | where | 71 | where |
| 33 | I: Iterator, | 72 | I: Iterator, |
| 34 | EFN: FnOnce() -> E, | 73 | EFN: FnOnce() -> E, |