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;stdpub use demux_map_lazy::demux_map_lazy;stdpub use demux_var::SinkVariadic;variadicspub use demux_var::demux_var;variadicspub use variadics;variadics
Modules§
- demux_
map std DemuxMapand related items.- demux_
map_ lazy std LazyDemuxSinkand related items.- demux_
var variadics DemuxVarand [variadics] forSink.- filter
Filterand related items.- filter_
map FilterMapand related items.- flat_
map FlatMapand related items.- flatten
Flattenand related items.- for_
each ForEachconsuming sink.- inspect
Inspectand related items.- lazy
LazySink,LazySource, and related items.- lazy_
sink_ source LazySinkSource, and related items.- map
Mapand related items.- send_
iter SendIterand related items.- send_
stream SendStreamand related items.- sink
- Asynchronous sinks.
- try_
for_ each TryForEachconsuming sink.- unzip
Unzip.
Structs§
- Sink
Builder - Start a
SinkBuildadaptor chain, withItemas the input item type.
Traits§
- Sink
- A
Sinkis a value into which other values can be sent, asynchronously. - Sink
Build - A helper trait for building
Sinks in forward order, unlike withSinktools. - ToSink
Build - Blanket trait for sending items from
Selfinto aSinkBuild.
Functions§
- filter
- Creates a
Filtersink that filters items based on a predicate. - filter_
map - Creates a
FilterMapsink that filters and maps items in one step. - flat_
map - Creates a
FlatMapsink that maps each item to an iterator and flattens the results. - flatten
- Creates a
Flattensink that flattens items that are iterators. - for_
each - Creates a
ForEachsink that consumes each item with a function. - inspect
- Creates an
Inspectsink that inspects each item without modifying it. - map
- Creates a
Mapsink that applies a function to each item. - send_
iter - Creates a
SendIterfuture that sends all items from an iterator to a sink. - send_
stream - Creates a
SendStreamfuture that sends all items from a stream to a sink. - try_
for_ each - Creates a
TryForEachsink that consumes each item with a fallible function. - unzip
- Creates an
Unzipsink that splits tuple items into two separate sinks.