#stream #async-stream #future #extension #async

jstream-ext

A crate which adds some extra extensions to futures Stream traits

1 unstable release

0.1.0 Feb 5, 2021

#1941 in Asynchronous

Apache-2.0

34KB
729 lines

jstream-ext

This crate provides some additional extensions to the futures stream traits.

Usage

Add the following to your Cargo.toml:

[dependencies]
jstream-ext = "0.1"

lib.rs:

Introduction

Extensions to the Stream and TryStream traits which implement behavior that I've implemented at least a few times while working with them.

To use these extensions, simply use the JStreamExt or JTryStreamExt items exported by this crate.

Summary

Here's a list of the various extensions provided by this crate:

Stream Extensions

The extensions to Stream are provided by the JStreamExt trait.

  • dedup - remove duplicate items from a stream
  • fold_mut - Similar to fold, but asks for a (&mut T, Self::Item) -> Future<Output=()> instead of a (T, Self::Item) -> Future<Output=T> folding function.
  • first - turns a stream into a future which emits only the first item emitted by the source.
  • nth - turns a stream into a future which emits an item after skipping a specified number of preceding items.

TryStream Extensions

The extensions to TryStream are provided by the JTryStreamExt trait.

  • try_first - turns the stream into a future which emits only the first result emitted by the source.
  • try_nth - turns the stream into a future which emits an item after skipping a specified number of preceding items, or emits an error immediately when encountered.
  • try_filter_map_ok - similar to filter_map, except it allows you to filter-map on the Ok part of the TryStream, and it emits any errors immediately when they are encountered.
  • try_dedup - remove duplicate items from a stream, but also emit any errors immediately when they are seen.
  • fuse_on_fail - if an error is seen, "fuse" the stream such that it panics if try_poll_next is called after an Err(Self::Error) item is emitted. This also makes a TryStream implement FusedStream regardless if the source implements that trait.
  • try_fold_mut - Similar to try_fold, but asks for a (&mut T, Self::Ok) -> Future<Output=Result<(), Self::Error>> instead of a (T, Self::Ok) -> Future<Output=Result<T, Self::Error>> folding function.

Dependencies

~0.6–0.8MB
~15K SLoC