better_collect
Provides a composable, declarative way to consume an iterator.
If Iterator is the "source half" of data pipeline, Collector is the "sink half" of the pipeline.
In order words, Iterator describes how to produce data, and Collector describes how to consume it.
Motivation
Suppose we are given an array of i32 and we are asked to find its sum and maximum value.
What would be our approach?
- Approach 1: Two-pass
let nums = ;
let sum: i32 = nums.into_iter.sum;
let max = nums.into_iter.max.unwrap;
assert_eq!;
assert_eq!;
Cons: This performs two passes over the data, which is worse than one-pass in performance.
That is fine for arrays, but can be much worse for HashSet, LinkedList,
or... data from an IO stream.
- Approach 2:
Iterator::fold
let nums = ;
let = nums
.into_iter
.fold;
assert_eq!;
assert_eq!;
Cons: Not very declarative — the main logic is still kind of procedural. (Doing sum and max by ourselves)
This crate proposes a one-pass, declarative approach:
use ;
let nums = ;
let = nums
.into_iter
.better_collect;
assert_eq!;
assert_eq!;
This approach achieves both one-pass and declarative, while is also composable (more of this later).
This is only with integers. How about with a non-Copy type?
// Suppose we open a connection...
// Task: Returns:
// - An array of data from the stream.
// - How many bytes were read.
// - The last-seen data.
// Usually, we're pretty much stuck with for-loop (tradition, `(try_)fold`, `(try_)for_each`).
// No common existing tools can help us here:
let mut received = vec!;
let mut byte_read = 0_usize;
let mut last_seen = None;
for data in socket_stream
let expected = ;
// This crate's way:
use ;
let = socket_stream
.better_collect;
assert_eq!;
Very declarative! We describe what we want to collect.
You might think this is just like Iterator::unzip, but this crate does a bit better:
it can split the data and feed separately WITHOUT additional allocation.
To demonstrate the difference, take this example:
use HashSet;
use ;
// Suppose we open a connection...
// Task: Collect UNIQUE chunks of data and concatenate them.
// `Iterator::unzip`
let : = socket_stream
// Sad. We have to clone.
// We can't take a reference, since the referenced data is returned too.
.map
.unzip;
let unzip_way = ;
// Another approach is do two passes (collect to `Vec`, then iterate),
// which is still another allocation,
// or `Iterator::fold`, which's procedural.
// `Collector`
let collector_way = socket_stream
// No clone - the data flows smoothly.
.better_collect;
assert_eq!;
Traits
Unlike std::iter, this crate defines two main traits instead. Roughly:
use ControlFlow;
Collector is similar to Extend, but it also returns a ControlFlow
value to indicate whether it should stop accumulating items after a call to
collect().
This serves as a hint for adaptors like then() or chain()
to "vectorize" the remaining items to another collector.
In short, it is like a composable Extend.
RefCollector is a collector that does not require ownership of an item
to process it.
This allows items to flow through multiple collectors without being consumed,
avoiding unnecessary cloning.
It powers then(), which creates a pipeline of collectors,
letting each item pass through safely by reference until the final collector
takes ownership.
Finally, BetterCollect extends Iterator with the
better_collect() method, which feeds all items from an iterator
into a Collector and returns the collector’s result.
To use this method, the BetterCollect trait must be imported.
More details can be found in their respective documentation.
Features
If the std feature is not enabled, the crate builds in no_std mode.
alloc— Enables implementations for types in thealloccrate (e.g.,Vec,VecDeque,BTreeSet).std(default) — Enables theallocfeature and implementations forstd-only types (e.g.,HashMap).
Todos
- More detailed documentation.
- More adaptors (this crate currently only has common ones).
- Possibly foreign implementations for types in other crates.