pub trait ExactSizeConcurrentIter: ConcurrentIter {
// Required methods
fn len(&self) -> usize;
fn next_exact_chunk(
&self,
chunk_size: usize
) -> Option<NextManyExact<Self::Item, impl ExactSizeIterator<Item = Self::Item>>>;
// Provided method
fn is_empty(&self) -> bool { ... }
}
Expand description
A concurrent iterator that knows its exact length.
Required Methods§
sourcefn next_exact_chunk(
&self,
chunk_size: usize
) -> Option<NextManyExact<Self::Item, impl ExactSizeIterator<Item = Self::Item>>>
fn next_exact_chunk( &self, chunk_size: usize ) -> Option<NextManyExact<Self::Item, impl ExactSizeIterator<Item = Self::Item>>>
Returns the next chunk with the requested chunk_size
:
- Returns
None
if there are no more elements to yield. - Returns
Some
of acrate::NextManyExact
which contains the following information:begin_idx
: the index of the first element to be yielded by thevalues
iterator.values
: anExactSizeIterator
with knownlen
which is guaranteed to be positive and less than or equal tochunk_size
.
§Examples
use orx_concurrent_iter::*;
use orx_concurrent_bag::*;
let chunk_size = 2;
let num_threads = 4;
let characters = vec!['0', '1', '2', '3', '4', '5', '6', '7'];
let slice = characters.as_slice();
let outputs = ConcurrentBag::new();
let con_iter = &slice.con_iter();
let bag = &outputs;
std::thread::scope(|s| {
for _ in 0..num_threads {
s.spawn(move || {
while let Some(next) = con_iter.next_exact_chunk(chunk_size) {
let begin = next.begin_idx();
for (i, value) in next.values().enumerate() {
let idx = begin + i;
let expected_value = char::from_digit(idx as u32, 10).unwrap();
assert_eq!(value, &expected_value);
bag.push(*value);
}
}
});
}
});
let mut outputs: Vec<char> = outputs.into_inner().into();
outputs.sort();
assert_eq!(characters, outputs);
Provided Methods§
Object Safety§
This trait is not object safe.