|
| 1 | +use core::fmt; |
| 2 | +use core::pin::Pin; |
| 3 | +use futures_core::future::TryFuture; |
| 4 | +use futures_core::stream::{FusedStream, Stream, TryStream}; |
| 5 | +use futures_core::task::{Context, Poll}; |
| 6 | +use futures_sink::Sink; |
| 7 | +use pin_utils::unsafe_pinned; |
| 8 | + |
| 9 | +#[must_use = "streams do nothing unless polled"] |
| 10 | +pub(crate) struct FlattenStreamSink<Fut> |
| 11 | +where |
| 12 | + Fut: TryFuture, |
| 13 | +{ |
| 14 | + state: State<Fut, Fut::Ok>, |
| 15 | +} |
| 16 | + |
| 17 | +impl<Fut> Unpin for FlattenStreamSink<Fut> |
| 18 | +where |
| 19 | + Fut: TryFuture + Unpin, |
| 20 | + Fut::Ok: Unpin, |
| 21 | +{ |
| 22 | +} |
| 23 | + |
| 24 | +impl<Fut> fmt::Debug for FlattenStreamSink<Fut> |
| 25 | +where |
| 26 | + Fut: TryFuture + fmt::Debug, |
| 27 | + Fut::Ok: fmt::Debug, |
| 28 | +{ |
| 29 | + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 | + fmt.debug_struct("FlattenStreamSink") |
| 31 | + .field("state", &self.state) |
| 32 | + .finish() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<Fut> FlattenStreamSink<Fut> |
| 37 | +where |
| 38 | + Fut: TryFuture, |
| 39 | +{ |
| 40 | + unsafe_pinned!(state: State<Fut, Fut::Ok>); |
| 41 | + |
| 42 | + pub(crate) fn new(future: Fut) -> Self { |
| 43 | + Self { |
| 44 | + state: State::Future(future), |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Debug)] |
| 50 | +enum State<Fut, S> { |
| 51 | + // future is not yet called or called and not ready |
| 52 | + Future(Fut), |
| 53 | + // future resolved to Stream or Sink |
| 54 | + StreamOrSink(S), |
| 55 | + // future resolved to error |
| 56 | + Done, |
| 57 | +} |
| 58 | + |
| 59 | +impl<Fut, S> State<Fut, S> { |
| 60 | + fn get_pin_mut<'a>(self: Pin<&'a mut Self>) -> State<Pin<&'a mut Fut>, Pin<&'a mut S>> { |
| 61 | + // safety: data is never moved via the resulting &mut reference |
| 62 | + match unsafe { Pin::get_unchecked_mut(self) } { |
| 63 | + // safety: the future we're re-pinning here will never be moved; |
| 64 | + // it will just be polled, then dropped in place |
| 65 | + State::Future(f) => State::Future(unsafe { Pin::new_unchecked(f) }), |
| 66 | + // safety: the stream we're repinning here will never be moved; |
| 67 | + // it will just be polled, then dropped in place |
| 68 | + State::StreamOrSink(s) => State::StreamOrSink(unsafe { Pin::new_unchecked(s) }), |
| 69 | + State::Done => State::Done, |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<Fut> State<Fut, Fut::Ok> |
| 75 | +where |
| 76 | + Fut: TryFuture, |
| 77 | +{ |
| 78 | + fn poll_future(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Fut::Error>> { |
| 79 | + if let State::Future(f) = self.as_mut().get_pin_mut() { |
| 80 | + match ready!(f.try_poll(cx)) { |
| 81 | + Ok(s) => { |
| 82 | + // Future resolved to stream. |
| 83 | + // We do not return, but poll that |
| 84 | + // stream in the next loop iteration. |
| 85 | + self.set(State::StreamOrSink(s)); |
| 86 | + } |
| 87 | + Err(e) => { |
| 88 | + // Future resolved to error. |
| 89 | + // We have neither a pollable stream nor a future. |
| 90 | + self.set(State::Done); |
| 91 | + return Poll::Ready(Err(e)); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + Poll::Ready(Ok(())) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<Fut> FusedStream for FlattenStreamSink<Fut> |
| 100 | +where |
| 101 | + Fut: TryFuture, |
| 102 | + Fut::Ok: FusedStream, |
| 103 | +{ |
| 104 | + fn is_terminated(&self) -> bool { |
| 105 | + match &self.state { |
| 106 | + State::Future(_) => false, |
| 107 | + State::StreamOrSink(stream) => stream.is_terminated(), |
| 108 | + State::Done => true, |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl<Fut> Stream for FlattenStreamSink<Fut> |
| 114 | +where |
| 115 | + Fut: TryFuture, |
| 116 | + Fut::Ok: TryStream<Error = Fut::Error>, |
| 117 | +{ |
| 118 | + type Item = Result<<Fut::Ok as TryStream>::Ok, Fut::Error>; |
| 119 | + |
| 120 | + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 121 | + ready!(self.as_mut().state().poll_future(cx)?); |
| 122 | + match self.as_mut().state().get_pin_mut() { |
| 123 | + State::StreamOrSink(s) => s.try_poll_next(cx), |
| 124 | + State::Done => Poll::Ready(None), |
| 125 | + State::Future(_) => unreachable!(), |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<Fut, Item> Sink<Item> for FlattenStreamSink<Fut> |
| 131 | +where |
| 132 | + Fut: TryFuture, |
| 133 | + Fut::Ok: Sink<Item, SinkError = Fut::Error>, |
| 134 | +{ |
| 135 | + type SinkError = Fut::Error; |
| 136 | + |
| 137 | + fn poll_ready( |
| 138 | + mut self: Pin<&mut Self>, |
| 139 | + cx: &mut Context<'_>, |
| 140 | + ) -> Poll<Result<(), Self::SinkError>> { |
| 141 | + ready!(self.as_mut().state().poll_future(cx)?); |
| 142 | + match self.as_mut().state().get_pin_mut() { |
| 143 | + State::StreamOrSink(s) => s.poll_ready(cx), |
| 144 | + State::Done => panic!("poll_ready called after eof"), |
| 145 | + State::Future(_) => unreachable!(), |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::SinkError> { |
| 150 | + match self.state().get_pin_mut() { |
| 151 | + State::StreamOrSink(s) => s.start_send(item), |
| 152 | + State::Future(_) => panic!("poll_ready not called first"), |
| 153 | + State::Done => panic!("start_send called after eof"), |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> { |
| 158 | + match self.state().get_pin_mut() { |
| 159 | + State::StreamOrSink(s) => s.poll_flush(cx), |
| 160 | + // if sink not yet resolved, nothing written ==> everything flushed |
| 161 | + State::Future(_) => Poll::Ready(Ok(())), |
| 162 | + State::Done => panic!("poll_flush called after eof"), |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + fn poll_close( |
| 167 | + mut self: Pin<&mut Self>, |
| 168 | + cx: &mut Context<'_>, |
| 169 | + ) -> Poll<Result<(), Self::SinkError>> { |
| 170 | + let res = match self.as_mut().state().get_pin_mut() { |
| 171 | + State::StreamOrSink(s) => s.poll_close(cx), |
| 172 | + State::Future(_) | State::Done => Poll::Ready(Ok(())), |
| 173 | + }; |
| 174 | + if res.is_ready() { |
| 175 | + self.as_mut().state().set(State::Done); |
| 176 | + } |
| 177 | + res |
| 178 | + } |
| 179 | +} |
0 commit comments