|
62 | 62 | }
|
63 | 63 | }
|
64 | 64 |
|
| 65 | +pub(crate) fn stream_fn<S, T, A, F, E>(initial_state: S, mut func: F) -> LoopFn<A, F> |
| 66 | +where |
| 67 | + F: FnMut(S) -> A, |
| 68 | + A: Future<Output = Result<Option<(S, T)>, E>>, |
| 69 | +{ |
| 70 | + LoopFn { |
| 71 | + future: func(initial_state), |
| 72 | + func, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<S, T, A, F, E> Stream for LoopFn<A, F> |
| 77 | +where |
| 78 | + F: FnMut(S) -> A, |
| 79 | + A: Future<Output = Result<Option<(S, T)>, E>>, |
| 80 | +{ |
| 81 | + type Item = Result<T, E>; |
| 82 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { |
| 83 | + unsafe { |
| 84 | + let this = Pin::get_unchecked_mut(self); |
| 85 | + match ready!(Pin::new_unchecked(&mut this.future).poll(cx)) { |
| 86 | + Err(e) => Poll::Ready(Some(Err(e))), |
| 87 | + Ok(None) => Poll::Ready(None), |
| 88 | + Ok(Some((s, t))) => { |
| 89 | + this.future = (this.func)(s); |
| 90 | + Poll::Ready(Some(Ok(t))) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// A future created by the `ok_and_then` method. |
| 98 | +#[derive(Debug)] |
| 99 | +#[must_use = "futures do nothing unless polled"] |
| 100 | +pub(crate) struct OkAndThen<A, F> { |
| 101 | + future: A, |
| 102 | + func: F, |
| 103 | +} |
| 104 | + |
| 105 | +impl<U, T, A, F, E> Future for OkAndThen<A, F> |
| 106 | +where |
| 107 | + F: FnMut(U) -> Result<T, E>, |
| 108 | + A: Future<Output = Result<U, E>>, |
| 109 | +{ |
| 110 | + type Output = Result<T, E>; |
| 111 | + |
| 112 | + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<T, E>> { |
| 113 | + unsafe { |
| 114 | + let this = Pin::get_unchecked_mut(self); |
| 115 | + let result = try_ready!(Pin::new_unchecked(&mut this.future).poll(cx)); |
| 116 | + Poll::Ready((this.func)(result)) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// An extension crate to make using our combinator functions more ergonomic. |
| 122 | +pub(crate) trait ClientFutureExt { |
| 123 | + /// This function is similar to `map_ok` combinator. Provide a function which |
| 124 | + /// is applied after the `self` future is resolved, only if that future |
| 125 | + /// resolves to `Ok`. Similar to `Result::and_then`, the supplied function |
| 126 | + /// must return a Result (c.f., `map_ok`, which returns the underlying type, |
| 127 | + /// `T`). |
| 128 | + /// |
| 129 | + /// Note that unlike `and_then`, the supplied function returns a resolved |
| 130 | + /// value, not a closure. |
| 131 | + fn ok_and_then<U, T, F, E>(self, func: F) -> OkAndThen<Self, F> |
| 132 | + where |
| 133 | + F: FnMut(U) -> Result<T, E>, |
| 134 | + Self: Future<Output = Result<U, E>> + Sized, |
| 135 | + { |
| 136 | + OkAndThen { future: self, func } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<T: TryFuture> ClientFutureExt for T {} |
| 141 | + |
65 | 142 | /// Emulate `send_all`/`SendAll` from futures 0.1 since the 0.3 versions don't
|
66 | 143 | /// work with Tokio `Handle`s due to ownership differences.
|
67 | 144 | pub(crate) trait SinkCompat<I, E> {
|
|
0 commit comments