| Crates.io | output_iter |
| lib.rs | output_iter |
| version | 0.1.0 |
| created_at | 2025-01-12 09:06:34.561554+00 |
| updated_at | 2025-01-12 09:06:34.561554+00 |
| description | An iterator that performs calculation during iteration. |
| homepage | |
| repository | https://github.com/vil-mo/output_iter |
| max_upload_size | |
| id | 1513064 |
| size | 32,283 |
A utility for creating iterators that, in addition to yielding items, can perform a computation during iteration and return the result of that computation as soon as it is available.
The OutputIterator is useful for scenarios where the result of a computation depends on iterated data and is not immediately available.
Without OutputIterator, you would rely on one of the following approaches:
Vec, perform the computation, and then return the result along with an iterator over the vector. This approach is inefficient as it requires unnecessary memory allocations.std::iter::Map.Here’s how you can use an OutputIterator to iterate over numbers, compute their sum, and retrieve the result as soon as the sum exceeds a certain threshold:
use output_iter::{OutputIterator, OutputIteratorVariant};
struct SumIterator {
items: Vec<i32>,
sum: i32,
threshold: i32,
}
impl OutputIterator for SumIterator {
type Item = i32;
type Output = i32; // The computed sum
type AfterOutput = std::vec::IntoIter<i32>;
fn next(mut self) -> OutputIteratorVariant<Self> {
use OutputIteratorVariant::*;
if let Some(item) = self.items.pop() {
self.sum += item;
if self.sum >= self.threshold {
Output(self.items.into_iter(), self.sum)
} else {
Next(self, item)
}
} else {
Output(self.items.into_iter(), self.sum)
}
}
}
let iter = SumIterator {
items: vec![1, 2, 3, 4, 5],
sum: 0,
threshold: 10,
};
let (remaining, result) = iter.until_output_iter(|before_iter| {
for item in before_iter {
println!("Processing item: {}", item);
}
});
println!("Sum reached: {}", result);
for item in remaining {
println!("Remaining item: {}", item);
}
Processing item: 5
Processing item: 4
Processing item: 3
Sum reached: 12
Remaining item: 2
Remaining item: 1