History

feat!: `interrupt::Iter`, rename `interrupt::Iter` -> `interrupt::IterWithError` (#301)

Sebastian Thiel committed 4 years ago

Commit 8945d951 parent 0f0d3903 files · +47 −7Browse files

Unified · Split

3 files
  1. git-features/src/interrupt.rs +43 −4
  2. git-features/src/parallel/in_parallel.rs +2 −1
  3. git-repository/src/interrupt.rs +2 −2
git-features/src/interrupt.rs+43−4
@@ -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.
9pub struct Iter<'a, I> {
10 /// The actual iterator to yield elements from.
11 pub inner: I,
12 should_interrupt: &'a AtomicBool,
13}
14
15impl<'a, I> Iter<'a, I>
16where
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
32impl<'a, I> Iterator for Iter<'a, I>
33where
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.
8pub struct Iter<'a, I, EFN> {47pub 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
15impl<'a, I, EFN, E> Iter<'a, I, EFN>54impl<'a, I, EFN, E> IterWithErr<'a, I, EFN>
16where55where
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()` to59 /// 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
31impl<'a, I, EFN, E> Iterator for Iter<'a, I, EFN>70impl<'a, I, EFN, E> Iterator for IterWithErr<'a, I, EFN>
32where71where
33 I: Iterator,72 I: Iterator,
34 EFN: FnOnce() -> E,73 EFN: FnOnce() -> E,
git-features/src/parallel/in_parallel.rs+2−1
@@ -13,7 +13,8 @@ pub fn join<O1: Send, O2: Send>(left: impl FnOnce() -> O1 + Send, right: impl Fn · 12 unmodified lines
13/// Runs `f` with a scope to be used for spawning threads that will not outlive the function call.13/// Runs `f` with a scope to be used for spawning threads that will not outlive the function call.
14/// That way it's possible to handle threads without needing the 'static lifetime for data they interact with.14/// That way it's possible to handle threads without needing the 'static lifetime for data they interact with.
15///15///
16/// Note that the threads should not rely on actual parallelism as threading might be turned off entirely.16/// Note that the threads should not rely on actual parallelism as threading might be turned off entirely, hence should not
17/// connect each other with channels as deadlock would occour in single-threaded mode.
17pub fn threads<'env, F, R>(f: F) -> std::thread::Result<R>18pub fn threads<'env, F, R>(f: F) -> std::thread::Result<R>
18where19where
19 F: FnOnce(&crossbeam_utils::thread::Scope<'env>) -> R,20 F: FnOnce(&crossbeam_utils::thread::Scope<'env>) -> R,
git-repository/src/interrupt.rs+2−2
@@ -62,7 +62,7 @@ pub use init::init_handler; · 61 unmodified lines
62/// A wrapper for an inner iterator which will check for interruptions on each iteration.62/// A wrapper for an inner iterator which will check for interruptions on each iteration.
63pub struct Iter<I, EFN> {63pub struct Iter<I, EFN> {
64 /// The actual iterator to yield elements from.64 /// The actual iterator to yield elements from.
65 inner: git_features::interrupt::Iter<'static, I, EFN>,65 inner: git_features::interrupt::IterWithErr<'static, I, EFN>,
66}66}
67 67
68impl<I, EFN, E> Iter<I, EFN>68impl<I, EFN, E> Iter<I, EFN>
@@ -74,7 +74,7 @@ where · 5 unmodified lines
74 /// signal an interruption happened, causing no further items to be iterated from that point on.74 /// signal an interruption happened, causing no further items to be iterated from that point on.
75 pub fn new(inner: I, make_err: EFN) -> Self {75 pub fn new(inner: I, make_err: EFN) -> Self {
76 Iter {76 Iter {
77 inner: git_features::interrupt::Iter::new(inner, make_err, &IS_INTERRUPTED),77 inner: git_features::interrupt::IterWithErr::new(inner, make_err, &IS_INTERRUPTED),
78 }78 }
79 }79 }
80 80

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t