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
44 sync::atomic::{AtomicBool, Ordering},
55 };
66
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+
746 /// 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> {
948 /// The actual iterator to yield elements from.
1049 pub inner: I,
1150 make_err: Option<EFN>,
1251 should_interrupt: &'a AtomicBool,
1352 }
1453
15−impl<'a, I, EFN, E> Iter<'a, I, EFN>
54+impl<'a, I, EFN, E> IterWithErr<'a, I, EFN>
1655 where
1756 I: Iterator,
1857 EFN: FnOnce() -> E,
@@ -20,7 +59,7 @@ where · 1 unmodified line
2059 /// Create a new iterator over `inner` which checks for interruptions on each iteration and cals `make_err()` to
2160 /// signal an interruption happened, causing no further items to be iterated from that point on.
2261 pub fn new(inner: I, make_err: EFN, should_interrupt: &'a AtomicBool) -> Self {
23− Iter {
62+ IterWithErr {
2463 inner,
2564 make_err: Some(make_err),
2665 should_interrupt,
@@ -28,7 +67,7 @@ where · 1 unmodified line
2867 }
2968 }
3069
31−impl<'a, I, EFN, E> Iterator for Iter<'a, I, EFN>
70+impl<'a, I, EFN, E> Iterator for IterWithErr<'a, I, EFN>
3271 where
3372 I: Iterator,
3473 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
1313 /// Runs `f` with a scope to be used for spawning threads that will not outlive the function call.
1414 /// That way it's possible to handle threads without needing the 'static lifetime for data they interact with.
1515 ///
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.
1718 pub fn threads<'env, F, R>(f: F) -> std::thread::Result<R>
1819 where
1920 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
6262 /// A wrapper for an inner iterator which will check for interruptions on each iteration.
6363 pub struct Iter<I, EFN> {
6464 /// 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>,
6666 }
6767
6868 impl<I, EFN, E> Iter<I, EFN>
@@ -74,7 +74,7 @@ where · 5 unmodified lines
7474 /// signal an interruption happened, causing no further items to be iterated from that point on.
7575 pub fn new(inner: I, make_err: EFN) -> Self {
7676 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),
7878 }
7979 }
8080

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