|
| 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +fn main() { |
| 12 | + if false { test(); } |
| 13 | +} |
| 14 | + |
| 15 | +fn test() { |
| 16 | + let rx = Err::<Vec<usize>, u32>(1).into_future(); |
| 17 | + |
| 18 | + rx.map(|l: Vec<usize>| stream::iter(l.into_iter().map(|i| Ok(i)))) |
| 19 | + .flatten_stream() |
| 20 | + .chunks(50) |
| 21 | + .buffer_unordered(5); |
| 22 | +} |
| 23 | + |
| 24 | +use future::{Future, IntoFuture}; |
| 25 | +mod future { |
| 26 | + use std::result; |
| 27 | + |
| 28 | + use {stream, Stream}; |
| 29 | + |
| 30 | + pub trait Future { |
| 31 | + type Item; |
| 32 | + type Error; |
| 33 | + |
| 34 | + fn map<F, U>(self, _: F) -> Map<Self, F> |
| 35 | + where F: FnOnce(Self::Item) -> U, |
| 36 | + Self: Sized, |
| 37 | + { |
| 38 | + panic!() |
| 39 | + } |
| 40 | + |
| 41 | + fn flatten_stream(self) -> FlattenStream<Self> |
| 42 | + where <Self as Future>::Item: stream::Stream<Error=Self::Error>, |
| 43 | + Self: Sized |
| 44 | + { |
| 45 | + panic!() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub trait IntoFuture { |
| 50 | + type Future: Future<Item=Self::Item, Error=Self::Error>; |
| 51 | + type Item; |
| 52 | + type Error; |
| 53 | + fn into_future(self) -> Self::Future; |
| 54 | + } |
| 55 | + |
| 56 | + impl<F: Future> IntoFuture for F { |
| 57 | + type Future = F; |
| 58 | + type Item = F::Item; |
| 59 | + type Error = F::Error; |
| 60 | + |
| 61 | + fn into_future(self) -> F { |
| 62 | + panic!() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + impl<T, E> IntoFuture for result::Result<T, E> { |
| 67 | + type Future = FutureResult<T, E>; |
| 68 | + type Item = T; |
| 69 | + type Error = E; |
| 70 | + |
| 71 | + fn into_future(self) -> FutureResult<T, E> { |
| 72 | + panic!() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + pub struct Map<A, F> { |
| 77 | + _a: (A, F), |
| 78 | + } |
| 79 | + |
| 80 | + impl<U, A, F> Future for Map<A, F> |
| 81 | + where A: Future, |
| 82 | + F: FnOnce(A::Item) -> U, |
| 83 | + { |
| 84 | + type Item = U; |
| 85 | + type Error = A::Error; |
| 86 | + } |
| 87 | + |
| 88 | + pub struct FlattenStream<F> { |
| 89 | + _f: F, |
| 90 | + } |
| 91 | + |
| 92 | + impl<F> Stream for FlattenStream<F> |
| 93 | + where F: Future, |
| 94 | + <F as Future>::Item: Stream<Error=F::Error>, |
| 95 | + { |
| 96 | + type Item = <F::Item as Stream>::Item; |
| 97 | + type Error = <F::Item as Stream>::Error; |
| 98 | + } |
| 99 | + |
| 100 | + pub struct FutureResult<T, E> { |
| 101 | + _inner: (T, E), |
| 102 | + } |
| 103 | + |
| 104 | + impl<T, E> Future for FutureResult<T, E> { |
| 105 | + type Item = T; |
| 106 | + type Error = E; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +mod stream { |
| 111 | + use IntoFuture; |
| 112 | + |
| 113 | + pub trait Stream { |
| 114 | + type Item; |
| 115 | + type Error; |
| 116 | + |
| 117 | + fn buffer_unordered(self, amt: usize) -> BufferUnordered<Self> |
| 118 | + where Self::Item: IntoFuture<Error = <Self as Stream>::Error>, |
| 119 | + Self: Sized |
| 120 | + { |
| 121 | + new(self, amt) |
| 122 | + } |
| 123 | + |
| 124 | + fn chunks(self, _capacity: usize) -> Chunks<Self> |
| 125 | + where Self: Sized |
| 126 | + { |
| 127 | + panic!() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + pub struct IterStream<I> { |
| 132 | + _iter: I, |
| 133 | + } |
| 134 | + |
| 135 | + pub fn iter<J, T, E>(_: J) -> IterStream<J::IntoIter> |
| 136 | + where J: IntoIterator<Item=Result<T, E>>, |
| 137 | + { |
| 138 | + panic!() |
| 139 | + } |
| 140 | + |
| 141 | + impl<I, T, E> Stream for IterStream<I> |
| 142 | + where I: Iterator<Item=Result<T, E>>, |
| 143 | + { |
| 144 | + type Item = T; |
| 145 | + type Error = E; |
| 146 | + } |
| 147 | + |
| 148 | + pub struct Chunks<S> { |
| 149 | + _stream: S |
| 150 | + } |
| 151 | + |
| 152 | + impl<S> Stream for Chunks<S> |
| 153 | + where S: Stream |
| 154 | + { |
| 155 | + type Item = Result<Vec<<S as Stream>::Item>, u32>; |
| 156 | + type Error = <S as Stream>::Error; |
| 157 | + } |
| 158 | + |
| 159 | + pub struct BufferUnordered<S> { |
| 160 | + _stream: S, |
| 161 | + } |
| 162 | + |
| 163 | + enum Slot<T> { |
| 164 | + Next(usize), |
| 165 | + _Data { _a: T }, |
| 166 | + } |
| 167 | + |
| 168 | + fn new<S>(_s: S, _amt: usize) -> BufferUnordered<S> |
| 169 | + where S: Stream, |
| 170 | + S::Item: IntoFuture<Error=<S as Stream>::Error>, |
| 171 | + { |
| 172 | + (0..0).map(|_| { |
| 173 | + Slot::Next::<<S::Item as IntoFuture>::Future>(1) |
| 174 | + }).collect::<Vec<_>>(); |
| 175 | + panic!() |
| 176 | + } |
| 177 | + |
| 178 | + impl<S> Stream for BufferUnordered<S> |
| 179 | + where S: Stream, |
| 180 | + S::Item: IntoFuture<Error=<S as Stream>::Error>, |
| 181 | + { |
| 182 | + type Item = <S::Item as IntoFuture>::Item; |
| 183 | + type Error = <S as Stream>::Error; |
| 184 | + } |
| 185 | +} |
| 186 | +use stream::Stream; |
0 commit comments