summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Kreutz <mail@skreutz.com>2022-12-25 19:41:02 +0100
committerStefan Kreutz <mail@skreutz.com>2022-12-25 19:43:15 +0100
commitacb122c0fcb3757aef2dc31a4757c1215b2917cf (patch)
tree26b4a1ecc68fa85bc4b7814804f3bef2bbe706ca
parentfe6f896d1a513431c3ddeecd30e5befe81514db5 (diff)
downloadparseq-acb122c0fcb3757aef2dc31a4757c1215b2917cf.tar
Implement size_hint, FusedIterator, ExactIterator
-rw-r--r--CHANGELOG.md12
-rw-r--r--src/lib.rs22
2 files changed, 30 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4dd07b2..548607b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,17 +10,23 @@ The format is based on [Keep A Changelog][] and this project adheres to
## Unreleased
+### Changed
+
+- Implemented `Iterator::size_hint` for `ParallelMap`
+- Implemented `FusedIterator` for `ParallelMap`
+- Implemented `ExactSizeIterator` for `ParallelMap`
+
## [0.1.1] - 2022-12-21
Improved documentation and tests.
### Added
-- Added recursive SHA256 example application.
+- Added recursive SHA256 example application
### Changed
-- `ParallelIterator` and `ParallelMap` must be used.
+- `ParallelIterator` and `ParallelMap` must be used
## [0.1.0] - 2022-12-18
@@ -28,4 +34,4 @@ Released the first version.
### Added
-- Added initial implementation.
+- Added initial implementation
diff --git a/src/lib.rs b/src/lib.rs
index 9c030d0..2d34b1c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -44,7 +44,7 @@
#![forbid(unsafe_code)]
#![warn(missing_docs)]
-use std::{collections::HashMap, num::NonZeroUsize};
+use std::{collections::HashMap, iter::FusedIterator, num::NonZeroUsize};
use crossbeam_channel::{Receiver, Select, Sender, TryRecvError};
@@ -295,6 +295,26 @@ where
}
}
}
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ let (lower, upper) = self.iter.size_hint();
+ let inflight = self.inflight();
+ (
+ lower.saturating_add(inflight),
+ upper.and_then(|i| i.checked_add(inflight)),
+ )
+ }
+}
+
+impl<I, B> FusedIterator for ParallelMap<I, B> where I: FusedIterator {}
+
+impl<I, B> ExactSizeIterator for ParallelMap<I, B>
+where
+ I: ExactSizeIterator,
+{
+ fn len(&self) -> usize {
+ self.iter.len() + self.inflight()
+ }
}
/// Calls a given closure when the thread unwinds due to a panic.
Generated by cgit. See skreutz.com for my tech blog and contact information.