Expand description
Extra Sink
adaptors and functions.
§Forward building with SinkBuild
For an intuitive API that matches the data flow direction, use SinkBuilder
and the SinkBuild
trait to chain
adaptors in forward order:
use sinktools::{SinkBuilder, SinkBuild};
use sinktools::sink::SinkExt; // `futures::sink::SinkExt` for `.send(_).await`
// Forward chain: flatten -> filter_map -> map -> filter -> for_each
let mut pipeline = SinkBuilder::<Vec<i32>>::new()
.flatten::<Vec<i32>>() // Flatten input vectors
.filter_map(|x: i32| { // Double evens, filter odds
if x % 2 == 0 {
Some(x * 2)
} else {
None
}
})
.map(|x| x + 1) // Add 1
.filter(|x| *x < 100) // Only values < 100
.for_each(|x: i32| { // Terminal operation
println!("Received: {}", x);
});
// Send nested data
pipeline.send(vec![1, 2, 3, 4]).await.unwrap();
pipeline.send(vec![5, 6]).await.unwrap();
pipeline.send(vec![]).await.unwrap();
pipeline.send(vec![7, 8, 9]).await.unwrap();
§Direct construction
Alternatively, you can construct sink adaptors directly using their new
methods:
use sinktools::{for_each, filter, map, filter_map, flatten};
use sinktools::sink::SinkExt; // for `.send(_).await`
// Build the same chain from inside out: sink <- filter <- map <- filter_map <- flatten
let sink = for_each(|x: i32| {
println!("Received: {}", x);
});
let filter_sink = filter(|x: &i32| *x < 100, sink);
let map_sink = map(|x: i32| x + 1, filter_sink);
let filter_map_sink = filter_map(|x: i32| {
if x % 2 == 0 {
Some(x * 2)
} else {
None
}
}, map_sink);
let mut complex_sink = flatten::<Vec<i32>, _>(filter_map_sink);
// Send nested data
complex_sink.send(vec![1, 2, 3, 4]).await.unwrap();
complex_sink.send(vec![5, 6]).await.unwrap();
complex_sink.send(vec![]).await.unwrap();
complex_sink.send(vec![7, 8, 9]).await.unwrap();
Each adaptor also provides a new_sink
method which ensures the construction is correct for Sink
to be implemented,
but may require additional type arguments to aid inference.
The forward SinkBuilder
API is more intuitive for direct users, as it matches the data flow direction. Direct construction is
better for generated code as it aids compiler type inference.
Re-exports§
pub use demux_map::demux_map;
std
pub use demux_var::SinkVariadic;
variadics
pub use demux_var::demux_var;
variadics
pub use variadics;
variadics
Modules§
- demux_
map std
DemuxMap
and related items.- demux_
var variadics
DemuxVar
and [variadics
] forSink
.- filter
Filter
and related items.- filter_
map FilterMap
and related items.- flat_
map FlatMap
and related items.- flatten
Flatten
and related items.- for_
each ForEach
consuming sink.- inspect
Inspect
and related items.- map
Map
and related items.- send_
iter SendIter
and related items.- send_
stream SendStream
and related items.- sink
- Asynchronous sinks.
- try_
for_ each TryForEach
consuming sink.- unzip
Unzip
.
Structs§
- Sink
Builder - Start a
SinkBuild
adaptor chain, withItem
as the input item type.
Traits§
- Sink
- A
Sink
is a value into which other values can be sent, asynchronously. - Sink
Build - A helper trait for building
Sink
s in forward order, unlike withSinktools
. - ToSink
Build - Blanket trait for sending items from
Self
into aSinkBuild
.
Functions§
- filter
- Creates a
Filter
sink that filters items based on a predicate. - filter_
map - Creates a
FilterMap
sink that filters and maps items in one step. - flat_
map - Creates a
FlatMap
sink that maps each item to an iterator and flattens the results. - flatten
- Creates a
Flatten
sink that flattens items that are iterators. - for_
each - Creates a
ForEach
sink that consumes each item with a function. - inspect
- Creates an
Inspect
sink that inspects each item without modifying it. - map
- Creates a
Map
sink that applies a function to each item. - send_
iter - Creates a
SendIter
future that sends all items from an iterator to a sink. - send_
stream - Creates a
SendStream
future that sends all items from a stream to a sink. - try_
for_ each - Creates a
TryForEach
sink that consumes each item with a fallible function. - unzip
- Creates an
Unzip
sink that splits tuple items into two separate sinks.