[][src]Trait hyper::rt::Stream

#[must_use = "streams do nothing unless polled"]
pub trait Stream {
    type Item;
    type Error;
    fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error>;

    fn wait(self) -> Wait<Self> { ... }
fn into_future(self) -> StreamFuture<Self> { ... }
fn map<U, F>(self, f: F) -> Map<Self, F>
    where
        F: FnMut(Self::Item) -> U
, { ... }
fn map_err<U, F>(self, f: F) -> MapErr<Self, F>
    where
        F: FnMut(Self::Error) -> U
, { ... }
fn filter<F>(self, f: F) -> Filter<Self, F>
    where
        F: FnMut(&Self::Item) -> bool
, { ... }
fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F>
    where
        F: FnMut(Self::Item) -> Option<B>
, { ... }
fn then<F, U>(self, f: F) -> Then<Self, F, U>
    where
        F: FnMut(Result<Self::Item, Self::Error>) -> U,
        U: IntoFuture
, { ... }
fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U>
    where
        F: FnMut(Self::Item) -> U,
        U: IntoFuture<Error = Self::Error>
, { ... }
fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U>
    where
        F: FnMut(Self::Error) -> U,
        U: IntoFuture<Item = Self::Item>
, { ... }
fn collect(self) -> Collect<Self> { ... }
fn concat2(self) -> Concat2<Self>
    where
        Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
        Self::Item: IntoIterator,
        Self::Item: Default
, { ... }
fn concat(self) -> Concat<Self>
    where
        Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
        Self::Item: IntoIterator
, { ... }
fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T>
    where
        F: FnMut(T, Self::Item) -> Fut,
        Fut: IntoFuture<Item = T>,
        Self::Error: From<<Fut as IntoFuture>::Error>
, { ... }
fn flatten(self) -> Flatten<Self>
    where
        Self::Item: Stream,
        <Self::Item as Stream>::Error: From<Self::Error>
, { ... }
fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R>
    where
        P: FnMut(&Self::Item) -> R,
        R: IntoFuture<Item = bool, Error = Self::Error>
, { ... }
fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R>
    where
        P: FnMut(&Self::Item) -> R,
        R: IntoFuture<Item = bool, Error = Self::Error>
, { ... }
fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U>
    where
        F: FnMut(Self::Item) -> U,
        U: IntoFuture<Item = (), Error = Self::Error>
, { ... }
fn from_err<E>(self) -> FromErr<Self, E>
    where
        E: From<Self::Error>
, { ... }
fn take(self, amt: u64) -> Take<Self> { ... }
fn skip(self, amt: u64) -> Skip<Self> { ... }
fn fuse(self) -> Fuse<Self> { ... }
fn by_ref(&mut self) -> &mut Self { ... }
fn catch_unwind(self) -> CatchUnwind<Self>
    where
        Self: UnwindSafe
, { ... }
fn buffered(self, amt: usize) -> Buffered<Self>
    where
        Self::Item: IntoFuture,
        <Self::Item as IntoFuture>::Error == Self::Error
, { ... }
fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self>
    where
        Self::Item: IntoFuture,
        <Self::Item as IntoFuture>::Error == Self::Error
, { ... }
fn merge<S>(self, other: S) -> Merge<Self, S>
    where
        S: Stream<Error = Self::Error>
, { ... }
fn zip<S>(self, other: S) -> Zip<Self, S>
    where
        S: Stream<Error = Self::Error>
, { ... }
fn chain<S>(self, other: S) -> Chain<Self, S>
    where
        S: Stream<Item = Self::Item, Error = Self::Error>
, { ... }
fn peekable(self) -> Peekable<Self> { ... }
fn chunks(self, capacity: usize) -> Chunks<Self> { ... }
fn select<S>(self, other: S) -> Select<Self, S>
    where
        S: Stream<Item = Self::Item, Error = Self::Error>
, { ... }
fn forward<S>(self, sink: S) -> Forward<Self, S>
    where
        S: Sink<SinkItem = Self::Item>,
        Self::Error: From<<S as Sink>::SinkError>
, { ... }
fn split(self) -> (SplitSink<Self>, SplitStream<Self>)
    where
        Self: Sink
, { ... }
fn inspect<F>(self, f: F) -> Inspect<Self, F>
    where
        F: FnMut(&Self::Item)
, { ... }
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
    where
        F: FnMut(&Self::Error)
, { ... } }

A stream of values, not all of which may have been produced yet.

Stream is a trait to represent any source of sequential events or items which acts like an iterator but long periods of time may pass between items. Like Future the methods of Stream never block and it is thus suitable for programming in an asynchronous fashion. This trait is very similar to the Iterator trait in the standard library where Some is used to signal elements of the stream and None is used to indicate that the stream is finished.

Like futures a stream has basic combinators to transform the stream, perform more work on each item, etc.

You can find more information/tutorials about streams online at https://tokio.rs

Streams as Futures

Any instance of Stream can also be viewed as a Future where the resolved value is the next item in the stream along with the rest of the stream. The into_future adaptor can be used here to convert any stream into a future for use with other future methods like join and select.

Errors

Streams, like futures, can also model errors in their computation. All streams have an associated Error type like with futures. Currently as of the 0.1 release of this library an error on a stream does not terminate the stream. That is, after one error is received, another error may be received from the same stream (it's valid to keep polling).

This property of streams, however, is being considered for change in 0.2 where an error on a stream is similar to None, it terminates the stream entirely. If one of these use cases suits you perfectly and not the other, please feel welcome to comment on the issue!

Associated Types

type Item

The type of item this stream will yield on success.

type Error

The type of error this stream may generate.

Loading content...

Required methods

fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error>

Attempt to pull out the next value of this stream, returning None if the stream is finished.

This method, like Future::poll, is the sole method of pulling out a value from a stream. This method must also be run within the context of a task typically and implementors of this trait must ensure that implementations of this method do not block, as it may cause consumers to behave badly.

Return value

If NotReady is returned then this stream's next value is not ready yet and implementations will ensure that the current task will be notified when the next value may be ready. If Some is returned then the returned value represents the next value on the stream. Err indicates an error happened, while Ok indicates whether there was a new item on the stream or whether the stream has terminated.

Panics

Once a stream is finished, that is Ready(None) has been returned, further calls to poll may result in a panic or other "bad behavior". If this is difficult to guard against then the fuse adapter can be used to ensure that poll always has well-defined semantics.

Loading content...

Provided methods

fn wait(self) -> Wait<Self>

Creates an iterator which blocks the current thread until each item of this stream is resolved.

This method will consume ownership of this stream, returning an implementation of a standard iterator. This iterator will block the current thread on each call to next if the item in the stream isn't ready yet.

Note: This method is not appropriate to call on event loops or similar I/O situations because it will prevent the event loop from making progress (this blocks the thread). This method should only be called when it's guaranteed that the blocking work associated with this stream will be completed by another thread.

This method is only available when the use_std feature of this library is activated, and it is activated by default.

Panics

The returned iterator does not attempt to catch panics. If the poll function panics, panics will be propagated to the caller of next.

fn into_future(self) -> StreamFuture<Self>

Converts this stream into a Future.

A stream can be viewed as a future which will resolve to a pair containing the next element of the stream plus the remaining stream. If the stream terminates, then the next element is None and the remaining stream is still passed back, to allow reclamation of its resources.

The returned future can be used to compose streams and futures together by placing everything into the "world of futures".

fn map<U, F>(self, f: F) -> Map<Self, F> where
    F: FnMut(Self::Item) -> U, 

Converts a stream of type T to a stream of type U.

The provided closure is executed over all elements of this stream as they are made available, and the callback will be executed inline with calls to poll.

Note that this function consumes the receiving stream and returns a wrapped version of it, similar to the existing map methods in the standard library.

Examples

use futures::prelude::*;
use futures::sync::mpsc;

let (_tx, rx) = mpsc::channel::<i32>(1);
let rx = rx.map(|x| x + 3);

fn map_err<U, F>(self, f: F) -> MapErr<Self, F> where
    F: FnMut(Self::Error) -> U, 

Converts a stream of error type T to a stream of error type U.

The provided closure is executed over all errors of this stream as they are made available, and the callback will be executed inline with calls to poll.

Note that this function consumes the receiving stream and returns a wrapped version of it, similar to the existing map_err methods in the standard library.

Examples

use futures::prelude::*;
use futures::sync::mpsc;

let (_tx, rx) = mpsc::channel::<i32>(1);
let rx = rx.map_err(|()| 3);

fn filter<F>(self, f: F) -> Filter<Self, F> where
    F: FnMut(&Self::Item) -> bool

Filters the values produced by this stream according to the provided predicate.

As values of this stream are made available, the provided predicate will be run against them. If the predicate returns true then the stream will yield the value, but if the predicate returns false then the value will be discarded and the next value will be produced.

All errors are passed through without filtering in this combinator.

Note that this function consumes the receiving stream and returns a wrapped version of it, similar to the existing filter methods in the standard library.

Examples

use futures::prelude::*;
use futures::sync::mpsc;

let (_tx, rx) = mpsc::channel::<i32>(1);
let evens = rx.filter(|x| x % 2 == 0);

fn filter_map<F, B>(self, f: F) -> FilterMap<Self, F> where
    F: FnMut(Self::Item) -> Option<B>, 

Filters the values produced by this stream while simultaneously mapping them to a different type.

As values of this stream are made available, the provided function will be run on them. If the predicate returns Some(e) then the stream will yield the value e, but if the predicate returns None then the next value will be produced.

All errors are passed through without filtering in this combinator.

Note that this function consumes the receiving stream and returns a wrapped version of it, similar to the existing filter_map methods in the standard library.

Examples

use futures::prelude::*;
use futures::sync::mpsc;

let (_tx, rx) = mpsc::channel::<i32>(1);
let evens_plus_one = rx.filter_map(|x| {
    if x % 0 == 2 {
        Some(x + 1)
    } else {
        None
    }
});

fn then<F, U>(self, f: F) -> Then<Self, F, U> where
    F: FnMut(Result<Self::Item, Self::Error>) -> U,
    U: IntoFuture

Chain on a computation for when a value is ready, passing the resulting item to the provided closure f.

This function can be used to ensure a computation runs regardless of the next value on the stream. The closure provided will be yielded a Result once a value is ready, and the returned future will then be run to completion to produce the next value on this stream.

The returned value of the closure must implement the IntoFuture trait and can represent some more work to be done before the composed stream is finished. Note that the Result type implements the IntoFuture trait so it is possible to simply alter the Result yielded to the closure and return it.

Note that this function consumes the receiving stream and returns a wrapped version of it.

Examples

use futures::prelude::*;
use futures::sync::mpsc;

let (_tx, rx) = mpsc::channel::<i32>(1);

let rx = rx.then(|result| {
    match result {
        Ok(e) => Ok(e + 3),
        Err(()) => Err(4),
    }
});

fn and_then<F, U>(self, f: F) -> AndThen<Self, F, U> where
    F: FnMut(Self::Item) -> U,
    U: IntoFuture<Error = Self::Error>, 

Chain on a computation for when a value is ready, passing the successful results to the provided closure f.

This function can be used to run a unit of work when the next successful value on a stream is ready. The closure provided will be yielded a value when ready, and the returned future will then be run to completion to produce the next value on this stream.

Any errors produced by this stream will not be passed to the closure, and will be passed through.

The returned value of the closure must implement the IntoFuture trait and can represent some more work to be done before the composed stream is finished. Note that the Result type implements the IntoFuture trait so it is possible to simply alter the Result yielded to the closure and return it.

Note that this function consumes the receiving stream and returns a wrapped version of it.

To process the entire stream and return a single future representing success or error, use for_each instead.

Examples

use futures::prelude::*;
use futures::sync::mpsc;

let (_tx, rx) = mpsc::channel::<i32>(1);

let rx = rx.and_then(|result| {
    if result % 2 == 0 {
        Ok(result)
    } else {
        Err(())
    }
});

fn or_else<F, U>(self, f: F) -> OrElse<Self, F, U> where
    F: FnMut(Self::Error) -> U,
    U: IntoFuture<Item = Self::Item>, 

Chain on a computation for when an error happens, passing the erroneous result to the provided closure f.

This function can be used to run a unit of work and attempt to recover from an error if one happens. The closure provided will be yielded an error when one appears, and the returned future will then be run to completion to produce the next value on this stream.

Any successful values produced by this stream will not be passed to the closure, and will be passed through.

The returned value of the closure must implement the IntoFuture trait and can represent some more work to be done before the composed stream is finished. Note that the Result type implements the IntoFuture trait so it is possible to simply alter the Result yielded to the closure and return it.

Note that this function consumes the receiving stream and returns a wrapped version of it.

fn collect(self) -> Collect<Self>

Collect all of the values of this stream into a vector, returning a future representing the result of that computation.

This combinator will collect all successful results of this stream and collect them into a Vec<Self::Item>. If an error happens then all collected elements will be dropped and the error will be returned.

The returned future will be resolved whenever an error happens or when the stream returns Ok(None).

This method is only available when the use_std feature of this library is activated, and it is activated by default.

Examples

use std::thread;

use futures::prelude::*;
use futures::sync::mpsc;

let (mut tx, rx) = mpsc::channel(1);

thread::spawn(|| {
    for i in (0..5).rev() {
        tx = tx.send(i + 1).wait().unwrap();
    }
});

let mut result = rx.collect();
assert_eq!(result.wait(), Ok(vec![5, 4, 3, 2, 1]));

fn concat2(self) -> Concat2<Self> where
    Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
    Self::Item: IntoIterator,
    Self::Item: Default

Concatenate all results of a stream into a single extendable destination, returning a future representing the end result.

This combinator will extend the first item with the contents of all the successful results of the stream. If the stream is empty, the default value will be returned. If an error occurs, all the results will be dropped and the error will be returned.

The name concat2 is an intermediate measure until the release of futures 0.2, at which point it will be renamed back to concat.

Examples

use std::thread;

use futures::prelude::*;
use futures::sync::mpsc;

let (mut tx, rx) = mpsc::channel(1);

thread::spawn(move || {
    for i in (0..3).rev() {
        let n = i * 3;
        tx = tx.send(vec![n + 1, n + 2, n + 3]).wait().unwrap();
    }
});
let result = rx.concat2();
assert_eq!(result.wait(), Ok(vec![7, 8, 9, 4, 5, 6, 1, 2, 3]));

fn concat(self) -> Concat<Self> where
    Self::Item: Extend<<Self::Item as IntoIterator>::Item>,
    Self::Item: IntoIterator

Deprecated since 0.1.14:

please use Stream::concat2 instead

Concatenate all results of a stream into a single extendable destination, returning a future representing the end result.

This combinator will extend the first item with the contents of all the successful results of the stream. If an error occurs, all the results will be dropped and the error will be returned.

Examples

use std::thread;

use futures::prelude::*;
use futures::sync::mpsc;

let (mut tx, rx) = mpsc::channel(1);

thread::spawn(move || {
    for i in (0..3).rev() {
        let n = i * 3;
        tx = tx.send(vec![n + 1, n + 2, n + 3]).wait().unwrap();
    }
});
let result = rx.concat();
assert_eq!(result.wait(), Ok(vec![7, 8, 9, 4, 5, 6, 1, 2, 3]));

Panics

It's important to note that this function will panic if the stream is empty, which is the reason for its deprecation.

fn fold<F, T, Fut>(self, init: T, f: F) -> Fold<Self, F, Fut, T> where
    F: FnMut(T, Self::Item) -> Fut,
    Fut: IntoFuture<Item = T>,
    Self::Error: From<<Fut as IntoFuture>::Error>, 

Execute an accumulating computation over a stream, collecting all the values into one final result.

This combinator will collect all successful results of this stream according to the closure provided. The initial state is also provided to this method and then is returned again by each execution of the closure. Once the entire stream has been exhausted the returned future will resolve to this value.

If an error happens then collected state will be dropped and the error will be returned.

Examples

use futures::prelude::*;
use futures::stream;
use futures::future;

let number_stream = stream::iter_ok::<_, ()>(0..6);
let sum = number_stream.fold(0, |acc, x| future::ok(acc + x));
assert_eq!(sum.wait(), Ok(15));

fn flatten(self) -> Flatten<Self> where
    Self::Item: Stream,
    <Self::Item as Stream>::Error: From<Self::Error>, 

Flattens a stream of streams into just one continuous stream.

If this stream's elements are themselves streams then this combinator will flatten out the entire stream to one long chain of elements. Any errors are passed through without looking at them, but otherwise each individual stream will get exhausted before moving on to the next.

use std::thread;

use futures::prelude::*;
use futures::sync::mpsc;

let (tx1, rx1) = mpsc::channel::<i32>(1);
let (tx2, rx2) = mpsc::channel::<i32>(1);
let (tx3, rx3) = mpsc::channel(1);

thread::spawn(|| {
    tx1.send(1).wait().unwrap()
       .send(2).wait().unwrap();
});
thread::spawn(|| {
    tx2.send(3).wait().unwrap()
       .send(4).wait().unwrap();
});
thread::spawn(|| {
    tx3.send(rx1).wait().unwrap()
       .send(rx2).wait().unwrap();
});

let mut result = rx3.flatten().collect();
assert_eq!(result.wait(), Ok(vec![1, 2, 3, 4]));

fn skip_while<P, R>(self, pred: P) -> SkipWhile<Self, P, R> where
    P: FnMut(&Self::Item) -> R,
    R: IntoFuture<Item = bool, Error = Self::Error>, 

Skip elements on this stream while the predicate provided resolves to true.

This function, like Iterator::skip_while, will skip elements on the stream until the predicate resolves to false. Once one element returns false all future elements will be returned from the underlying stream.

fn take_while<P, R>(self, pred: P) -> TakeWhile<Self, P, R> where
    P: FnMut(&Self::Item) -> R,
    R: IntoFuture<Item = bool, Error = Self::Error>, 

Take elements from this stream while the predicate provided resolves to true.

This function, like Iterator::take_while, will take elements from the stream until the predicate resolves to false. Once one element returns false it will always return that the stream is done.

fn for_each<F, U>(self, f: F) -> ForEach<Self, F, U> where
    F: FnMut(Self::Item) -> U,
    U: IntoFuture<Item = (), Error = Self::Error>, 

Runs this stream to completion, executing the provided closure for each element on the stream.

The closure provided will be called for each item this stream resolves to successfully, producing a future. That future will then be executed to completion before moving on to the next item.

The returned value is a Future where the Item type is () and errors are otherwise threaded through. Any error on the stream or in the closure will cause iteration to be halted immediately and the future will resolve to that error.

To process each item in the stream and produce another stream instead of a single future, use and_then instead.

fn from_err<E>(self) -> FromErr<Self, E> where
    E: From<Self::Error>, 

Map this stream's error to any error implementing From for this stream's Error, returning a new stream.

This function does for streams what try! does for Result, by letting the compiler infer the type of the resulting error. Just as map_err above, this is useful for example to ensure that streams have the same error type when used with combinators.

Note that this function consumes the receiving stream and returns a wrapped version of it.

fn take(self, amt: u64) -> Take<Self>

Creates a new stream of at most amt items of the underlying stream.

Once amt items have been yielded from this stream then it will always return that the stream is done.

Errors

Any errors yielded from underlying stream, before the desired amount of items is reached, are passed through and do not affect the total number of items taken.

fn skip(self, amt: u64) -> Skip<Self>

Creates a new stream which skips amt items of the underlying stream.

Once amt items have been skipped from this stream then it will always return the remaining items on this stream.

Errors

All errors yielded from underlying stream are passed through and do not affect the total number of items skipped.

fn fuse(self) -> Fuse<Self>

Fuse a stream such that poll will never again be called once it has finished.

Currently once a stream has returned None from poll any further calls could exhibit bad behavior such as block forever, panic, never return, etc. If it is known that poll may be called after stream has already finished, then this method can be used to ensure that it has defined semantics.

Once a stream has been fused and it finishes, then it will forever return None from poll. This, unlike for the traits poll method, is guaranteed.

Also note that as soon as this stream returns None it will be dropped to reclaim resources associated with it.

fn by_ref(&mut self) -> &mut Self

Borrows a stream, rather than consuming it.

This is useful to allow applying stream adaptors while still retaining ownership of the original stream.

use futures::prelude::*;
use futures::stream;
use futures::future;

let mut stream = stream::iter_ok::<_, ()>(1..5);

let sum = stream.by_ref().take(2).fold(0, |a, b| future::ok(a + b)).wait();
assert_eq!(sum, Ok(3));

// You can use the stream again
let sum = stream.take(2).fold(0, |a, b| future::ok(a + b)).wait();
assert_eq!(sum, Ok(7));

fn catch_unwind(self) -> CatchUnwind<Self> where
    Self: UnwindSafe

Catches unwinding panics while polling the stream.

Caught panic (if any) will be the last element of the resulting stream.

In general, panics within a stream can propagate all the way out to the task level. This combinator makes it possible to halt unwinding within the stream itself. It's most commonly used within task executors. This method should not be used for error handling.

Note that this method requires the UnwindSafe bound from the standard library. This isn't always applied automatically, and the standard library provides an AssertUnwindSafe wrapper type to apply it after-the fact. To assist using this method, the Stream trait is also implemented for AssertUnwindSafe<S> where S implements Stream.

This method is only available when the use_std feature of this library is activated, and it is activated by default.

Examples

use futures::prelude::*;
use futures::stream;

let stream = stream::iter_ok::<_, bool>(vec![Some(10), None, Some(11)]);
// panic on second element
let stream_panicking = stream.map(|o| o.unwrap());
let mut iter = stream_panicking.catch_unwind().wait();

assert_eq!(Ok(10), iter.next().unwrap().ok().unwrap());
assert!(iter.next().unwrap().is_err());
assert!(iter.next().is_none());

fn buffered(self, amt: usize) -> Buffered<Self> where
    Self::Item: IntoFuture,
    <Self::Item as IntoFuture>::Error == Self::Error

An adaptor for creating a buffered list of pending futures.

If this stream's item can be converted into a future, then this adaptor will buffer up to at most amt futures and then return results in the same order as the underlying stream. No more than amt futures will be buffered at any point in time, and less than amt may also be buffered depending on the state of each future.

The returned stream will be a stream of each future's result, with errors passed through whenever they occur.

This method is only available when the use_std feature of this library is activated, and it is activated by default.

fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self> where
    Self::Item: IntoFuture,
    <Self::Item as IntoFuture>::Error == Self::Error

An adaptor for creating a buffered list of pending futures (unordered).

If this stream's item can be converted into a future, then this adaptor will buffer up to amt futures and then return results in the order in which they complete. No more than amt futures will be buffered at any point in time, and less than amt may also be buffered depending on the state of each future.

The returned stream will be a stream of each future's result, with errors passed through whenever they occur.

This method is only available when the use_std feature of this library is activated, and it is activated by default.

fn merge<S>(self, other: S) -> Merge<Self, S> where
    S: Stream<Error = Self::Error>, 

Deprecated:

functionality provided by select now

An adapter for merging the output of two streams.

The merged stream produces items from one or both of the underlying streams as they become available. Errors, however, are not merged: you get at most one error at a time.

fn zip<S>(self, other: S) -> Zip<Self, S> where
    S: Stream<Error = Self::Error>, 

An adapter for zipping two streams together.

The zipped stream waits for both streams to produce an item, and then returns that pair. If an error happens, then that error will be returned immediately. If either stream ends then the zipped stream will also end.

fn chain<S>(self, other: S) -> Chain<Self, S> where
    S: Stream<Item = Self::Item, Error = Self::Error>, 

Adapter for chaining two stream.

The resulting stream emits elements from the first stream, and when first stream reaches the end, emits the elements from the second stream.

use futures::prelude::*;
use futures::stream;

let stream1 = stream::iter_result(vec![Ok(10), Err(false)]);
let stream2 = stream::iter_result(vec![Err(true), Ok(20)]);
let mut chain = stream1.chain(stream2).wait();

assert_eq!(Some(Ok(10)), chain.next());
assert_eq!(Some(Err(false)), chain.next());
assert_eq!(Some(Err(true)), chain.next());
assert_eq!(Some(Ok(20)), chain.next());
assert_eq!(None, chain.next());

fn peekable(self) -> Peekable<Self>

Creates a new stream which exposes a peek method.

Calling peek returns a reference to the next item in the stream.

fn chunks(self, capacity: usize) -> Chunks<Self>

An adaptor for chunking up items of the stream inside a vector.

This combinator will attempt to pull items from this stream and buffer them into a local vector. At most capacity items will get buffered before they're yielded from the returned stream.

Note that the vectors returned from this iterator may not always have capacity elements. If the underlying stream ended and only a partial vector was created, it'll be returned. Additionally if an error happens from the underlying stream then the currently buffered items will be yielded.

Errors are passed through the stream unbuffered.

This method is only available when the use_std feature of this library is activated, and it is activated by default.

Panics

This method will panic of capacity is zero.

fn select<S>(self, other: S) -> Select<Self, S> where
    S: Stream<Item = Self::Item, Error = Self::Error>, 

Creates a stream that selects the next element from either this stream or the provided one, whichever is ready first.

This combinator will attempt to pull items from both streams. Each stream will be polled in a round-robin fashion, and whenever a stream is ready to yield an item that item is yielded.

The select function is similar to merge except that it requires both streams to have the same item and error types.

Error are passed through from either stream.

fn forward<S>(self, sink: S) -> Forward<Self, S> where
    S: Sink<SinkItem = Self::Item>,
    Self::Error: From<<S as Sink>::SinkError>, 

A future that completes after the given stream has been fully processed into the sink, including flushing.

This future will drive the stream to keep producing items until it is exhausted, sending each item to the sink. It will complete once both the stream is exhausted, and the sink has fully processed received item, flushed successfully, and closed successfully.

Doing stream.forward(sink) is roughly equivalent to sink.send_all(stream). The returned future will exhaust all items from self, sending them all to sink. Furthermore the sink will be closed and flushed.

On completion, the pair (stream, sink) is returned.

fn split(self) -> (SplitSink<Self>, SplitStream<Self>) where
    Self: Sink

Splits this Stream + Sink object into separate Stream and Sink objects.

This can be useful when you want to split ownership between tasks, or allow direct interaction between the two objects (e.g. via Sink::send_all).

This method is only available when the use_std feature of this library is activated, and it is activated by default.

fn inspect<F>(self, f: F) -> Inspect<Self, F> where
    F: FnMut(&Self::Item), 

Do something with each item of this stream, afterwards passing it on.

This is similar to the Iterator::inspect method in the standard library where it allows easily inspecting each value as it passes through the stream, for example to debug what's going on.

fn inspect_err<F>(self, f: F) -> InspectErr<Self, F> where
    F: FnMut(&Self::Error), 

Do something with the error of this stream, afterwards passing it on.

This is similar to the Stream::inspect method where it allows easily inspecting the error as it passes through the stream, for example to debug what's going on.

Loading content...

Implementations on Foreign Types

impl<S, F> Stream for Inspect<S, F> where
    F: FnMut(&<S as Stream>::Item),
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S1, S2> Stream for Select<S1, S2> where
    S1: Stream,
    S2: Stream<Item = <S1 as Stream>::Item, Error = <S1 as Stream>::Error>, 
[src]

type Item = <S1 as Stream>::Item

type Error = <S1 as Stream>::Error

impl<S> Stream for Peekable<S> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S> Stream for Skip<S> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S, F> Stream for SinkMapErr<S, F> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S, F, U> Stream for MapErr<S, F> where
    F: FnMut(<S as Stream>::Error) -> U,
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = U

impl<T, E> Stream for Repeat<T, E> where
    T: Clone
[src]

type Item = T

type Error = E

impl<T> Stream for Receiver<T>[src]

type Item = T

type Error = ()

impl<I, T, E> Stream for IterResult<I> where
    I: Iterator<Item = Result<T, E>>, 
[src]

type Item = T

type Error = E

impl<I, E> Stream for SpawnHandle<I, E>[src]

type Item = I

type Error = E

impl<S> Stream for Buffer<S> where
    S: Sink + Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S, E> Stream for SinkFromErr<S, E> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<T> Stream for UnboundedReceiver<T>[src]

type Item = T

type Error = ()

impl<S> Stream for Take<S> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S> Stream for Box<S> where
    S: Stream + ?Sized
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S> Stream for Flatten<S> where
    S: Stream,
    <S as Stream>::Item: Stream,
    <<S as Stream>::Item as Stream>::Error: From<<S as Stream>::Error>, 
[src]

type Item = <<S as Stream>::Item as Stream>::Item

type Error = <<S as Stream>::Item as Stream>::Error

impl<T> Stream for FuturesUnordered<T> where
    T: Future
[src]

type Item = <T as Future>::Item

type Error = <T as Future>::Error

impl<I, E> Stream for SpawnHandle<I, E>[src]

type Item = I

type Error = E

impl<A, B> Stream for Either<A, B> where
    A: Stream,
    B: Stream<Item = <A as Stream>::Item, Error = <A as Stream>::Error>, 
[src]

type Item = <A as Stream>::Item

type Error = <A as Stream>::Error

impl<S, F> Stream for Filter<S, F> where
    F: FnMut(&<S as Stream>::Item) -> bool,
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S, F, U> Stream for Map<S, F> where
    F: FnMut(<S as Stream>::Item) -> U,
    S: Stream
[src]

type Item = U

type Error = <S as Stream>::Error

impl<T, E> Stream for Receiver<T, E>[src]

type Item = T

type Error = E

impl<S1, S2> Stream for Merge<S1, S2> where
    S1: Stream,
    S2: Stream<Error = <S1 as Stream>::Error>, 
[src]

type Item = MergedItem<<S1 as Stream>::Item, <S2 as Stream>::Item>

type Error = <S1 as Stream>::Error

impl<S> Stream for BufferUnordered<S> where
    S: Stream,
    <S as Stream>::Item: IntoFuture,
    <<S as Stream>::Item as IntoFuture>::Error == <S as Stream>::Error
[src]

type Item = <<S as Stream>::Item as IntoFuture>::Item

type Error = <S as Stream>::Error

impl<S, F, B> Stream for FilterMap<S, F> where
    F: FnMut(<S as Stream>::Item) -> Option<B>,
    S: Stream
[src]

type Item = B

type Error = <S as Stream>::Error

impl<S> Stream for Chunks<S> where
    S: Stream
[src]

type Item = Vec<<S as Stream>::Item>

type Error = <S as Stream>::Error

impl<'a, S> Stream for &'a mut S where
    S: Stream + ?Sized
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<I, T, E> Stream for Iter<I> where
    I: Iterator<Item = Result<T, E>>, 
[src]

type Item = T

type Error = E

impl<S> Stream for Fuse<S> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S> Stream for AssertUnwindSafe<S> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S, F, U> Stream for OrElse<S, F, U> where
    F: FnMut(<S as Stream>::Error) -> U,
    S: Stream,
    U: IntoFuture<Item = <S as Stream>::Item>, 
[src]

type Item = <S as Stream>::Item

type Error = <U as IntoFuture>::Error

impl<T> Stream for FuturesOrdered<T> where
    T: Future
[src]

type Item = <T as Future>::Item

type Error = <T as Future>::Error

impl<T> Stream for UnboundedReceiver<T>[src]

type Item = T

type Error = ()

impl<S, P, R> Stream for SkipWhile<S, P, R> where
    P: FnMut(&<S as Stream>::Item) -> R,
    R: IntoFuture<Item = bool, Error = <S as Stream>::Error>,
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<T, F, Fut, It> Stream for Unfold<T, F, Fut> where
    F: FnMut(T) -> Option<Fut>,
    Fut: IntoFuture<Item = (It, T)>, 
[src]

type Item = It

type Error = <Fut as IntoFuture>::Error

impl<S1, S2> Stream for Zip<S1, S2> where
    S1: Stream,
    S2: Stream<Error = <S1 as Stream>::Error>, 
[src]

type Item = (<S1 as Stream>::Item, <S2 as Stream>::Item)

type Error = <S1 as Stream>::Error

impl<I, E> Stream for IterOk<I, E> where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

type Error = E

impl<S1, S2> Stream for Chain<S1, S2> where
    S1: Stream,
    S2: Stream<Item = <S1 as Stream>::Item, Error = <S1 as Stream>::Error>, 
[src]

type Item = <S1 as Stream>::Item

type Error = <S1 as Stream>::Error

impl<T, E, F> Stream for PollFn<F> where
    F: FnMut() -> Result<Async<Option<T>>, E>, 
[src]

type Item = T

type Error = E

impl<S, E> Stream for FromErr<S, E> where
    E: From<<S as Stream>::Error>,
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = E

impl<F> Stream for FlattenStream<F> where
    F: Future,
    <F as Future>::Item: Stream,
    <<F as Future>::Item as Stream>::Error == <F as Future>::Error
[src]

type Item = <<F as Future>::Item as Stream>::Item

type Error = <<F as Future>::Item as Stream>::Error

impl<T> Stream for Receiver<T>[src]

type Item = T

type Error = ()

impl<T, E> Stream for Empty<T, E>[src]

type Item = T

type Error = E

impl<S, F, U> Stream for Then<S, F, U> where
    F: FnMut(Result<<S as Stream>::Item, <S as Stream>::Error>) -> U,
    S: Stream,
    U: IntoFuture
[src]

type Item = <U as IntoFuture>::Item

type Error = <U as IntoFuture>::Error

impl<S> Stream for Buffered<S> where
    S: Stream,
    <S as Stream>::Item: IntoFuture,
    <<S as Stream>::Item as IntoFuture>::Error == <S as Stream>::Error
[src]

type Item = <<S as Stream>::Item as IntoFuture>::Item

type Error = <S as Stream>::Error

impl<S, U, F, St> Stream for WithFlatMap<S, U, F, St> where
    F: FnMut(U) -> St,
    S: Stream + Sink,
    St: Stream<Item = <S as Sink>::SinkItem, Error = <S as Sink>::SinkError>, 
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S, F, U> Stream for AndThen<S, F, U> where
    F: FnMut(<S as Stream>::Item) -> U,
    S: Stream,
    U: IntoFuture<Error = <S as Stream>::Error>, 
[src]

type Item = <U as IntoFuture>::Item

type Error = <S as Stream>::Error

impl<S, U, F, Fut> Stream for With<S, U, F, Fut> where
    F: FnMut(U) -> Fut,
    Fut: IntoFuture,
    S: Stream + Sink
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<T, E> Stream for Once<T, E>[src]

type Item = T

type Error = E

impl<F> Stream for IntoStream<F> where
    F: Future
[src]

type Item = <F as Future>::Item

type Error = <F as Future>::Error

impl<S, F> Stream for InspectErr<S, F> where
    F: FnMut(&<S as Stream>::Error),
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S> Stream for CatchUnwind<S> where
    S: Stream + UnwindSafe
[src]

type Item = Result<<S as Stream>::Item, <S as Stream>::Error>

type Error = Box<dyn Any + 'static + Send>

impl<S, P, R> Stream for TakeWhile<S, P, R> where
    P: FnMut(&<S as Stream>::Item) -> R,
    R: IntoFuture<Item = bool, Error = <S as Stream>::Error>,
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<S> Stream for SplitStream<S> where
    S: Stream
[src]

type Item = <S as Stream>::Item

type Error = <S as Stream>::Error

impl<T, B> Stream for Connection<T, B> where
    B: IntoBuf,
    T: AsyncRead + AsyncWrite,
    <B as IntoBuf>::Buf: 'static, 
[src]

type Item = (Request<RecvStream>, SendResponse<B>)

type Error = Error

impl Stream for RecvStream[src]

type Item = Bytes

type Error = Error

impl Stream for PushPromises[src]

type Item = PushPromise

type Error = Error

impl<A> Stream for Lines<A> where
    A: AsyncRead + BufRead
[src]

type Item = String

type Error = Error

impl<T> Stream for IntoStream<T> where
    T: BufStream
[src]

type Item = <T as BufStream>::Item

type Error = <T as BufStream>::Error

impl<T> Stream for Receiver<T>[src]

type Item = T

type Error = RecvError

impl<T> Stream for Receiver<T> where
    T: Clone
[src]

type Item = T

type Error = RecvError

impl<T> Stream for UnboundedReceiver<T>[src]

type Item = T

type Error = UnboundedRecvError

impl Stream for Incoming[src]

type Item = TcpStream

type Error = Error

impl Stream for Interval[src]

type Item = Instant

type Error = Error

impl<T> Stream for Throttle<T> where
    T: Stream
[src]

type Item = <T as Stream>::Item

type Error = ThrottleError<<T as Stream>::Error>

impl<T> Stream for DelayQueue<T>[src]

type Item = Expired<T>

type Error = Error

impl<T> Stream for Timeout<T> where
    T: Stream
[src]

type Item = <T as Stream>::Item

type Error = Error<<T as Stream>::Error>

Loading content...

Implementors

impl Stream for AddrIncoming[src]

type Item = AddrStream

type Error = Error

impl Stream for Body[src]

type Item = Chunk

type Error = Error

impl<I, S, B, E> Stream for Serve<I, S, E> where
    I: Stream,
    I::Item: AsyncRead + AsyncWrite,
    I::Error: Into<Box<dyn StdError + Send + Sync>>,
    S: MakeServiceRef<I::Item, ReqBody = Body, ResBody = B>,
    B: Payload,
    E: H2Exec<<S::Service as Service>::Future, B>, 
[src]

type Item = Connecting<I::Item, S::Future, E>

type Error = Error

Loading content...