1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
16 let rx = Err::<Vec<usize>, u32>(1).into_future();
18 rx.map(|l: Vec<usize>| stream::iter(l.into_iter().map(|i| Ok(i))))
24 use future::{Future, IntoFuture};
34 fn map<F, U>(self, _: F) -> Map<Self, F>
35 where F: FnOnce(Self::Item) -> U,
41 fn flatten_stream(self) -> FlattenStream<Self>
42 where <Self as Future>::Item: stream::Stream<Error=Self::Error>,
49 pub trait IntoFuture {
50 type Future: Future<Item=Self::Item, Error=Self::Error>;
53 fn into_future(self) -> Self::Future;
56 impl<F: Future> IntoFuture for F {
59 type Error = F::Error;
61 fn into_future(self) -> F {
66 impl<T, E> IntoFuture for result::Result<T, E> {
67 type Future = FutureResult<T, E>;
71 fn into_future(self) -> FutureResult<T, E> {
76 pub struct Map<A, F> {
80 impl<U, A, F> Future for Map<A, F>
82 F: FnOnce(A::Item) -> U,
85 type Error = A::Error;
88 pub struct FlattenStream<F> {
92 impl<F> Stream for FlattenStream<F>
94 <F as Future>::Item: Stream<Error=F::Error>,
96 type Item = <F::Item as Stream>::Item;
97 type Error = <F::Item as Stream>::Error;
100 pub struct FutureResult<T, E> {
104 impl<T, E> Future for FutureResult<T, E> {
117 fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self>
118 where Self::Item: IntoFuture<Error = <Self as Stream>::Error>,
124 fn chunks(self, _capacity: usize) -> Chunks<Self>
131 pub struct IterStream<I> {
135 pub fn iter<J, T, E>(_: J) -> IterStream<J::IntoIter>
136 where J: IntoIterator<Item=Result<T, E>>,
141 impl<I, T, E> Stream for IterStream<I>
142 where I: Iterator<Item=Result<T, E>>,
148 pub struct Chunks<S> {
152 impl<S> Stream for Chunks<S>
155 type Item = Result<Vec<<S as Stream>::Item>, u32>;
156 type Error = <S as Stream>::Error;
159 pub struct BufferUnordered<S> {
168 fn new<S>(_s: S, _amt: usize) -> BufferUnordered<S>
170 S::Item: IntoFuture<Error=<S as Stream>::Error>,
173 Slot::Next::<<S::Item as IntoFuture>::Future>(1)
174 }).collect::<Vec<_>>();
178 impl<S> Stream for BufferUnordered<S>
180 S::Item: IntoFuture<Error=<S as Stream>::Error>,
182 type Item = <S::Item as IntoFuture>::Item;
183 type Error = <S as Stream>::Error;