|
| 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 | +#[cfg(feature = "sink")] |
| 7 | +use futures_sink::Sink; |
| 8 | +use pin_project::pin_project; |
| 9 | + |
| 10 | +/// Stream for the [`try_take_while`](super::TryStreamExt::try_take_while) |
| 11 | +/// method. |
| 12 | +#[pin_project] |
| 13 | +#[must_use = "streams do nothing unless polled"] |
| 14 | +pub struct TryTakeWhile<St, Fut, F> |
| 15 | +where |
| 16 | + St: TryStream, |
| 17 | +{ |
| 18 | + #[pin] |
| 19 | + stream: St, |
| 20 | + f: F, |
| 21 | + #[pin] |
| 22 | + pending_fut: Option<Fut>, |
| 23 | + pending_item: Option<St::Ok>, |
| 24 | + done_taking: bool, |
| 25 | +} |
| 26 | + |
| 27 | +impl<St, Fut, F> fmt::Debug for TryTakeWhile<St, Fut, F> |
| 28 | +where |
| 29 | + St: TryStream + fmt::Debug, |
| 30 | + St::Ok: fmt::Debug, |
| 31 | + Fut: fmt::Debug, |
| 32 | +{ |
| 33 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 34 | + f.debug_struct("TryTakeWhile") |
| 35 | + .field("stream", &self.stream) |
| 36 | + .field("pending_fut", &self.pending_fut) |
| 37 | + .field("pending_item", &self.pending_item) |
| 38 | + .field("done_taking", &self.done_taking) |
| 39 | + .finish() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<St, Fut, F> TryTakeWhile<St, Fut, F> |
| 44 | +where |
| 45 | + St: TryStream, |
| 46 | + F: FnMut(&St::Ok) -> Fut, |
| 47 | + Fut: TryFuture<Ok = bool, Error = St::Error>, |
| 48 | +{ |
| 49 | + pub(super) fn new(stream: St, f: F) -> TryTakeWhile<St, Fut, F> { |
| 50 | + TryTakeWhile { |
| 51 | + stream, |
| 52 | + f, |
| 53 | + pending_fut: None, |
| 54 | + pending_item: None, |
| 55 | + done_taking: false, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + delegate_access_inner!(stream, St, ()); |
| 60 | +} |
| 61 | + |
| 62 | +impl<St, Fut, F> Stream for TryTakeWhile<St, Fut, F> |
| 63 | +where |
| 64 | + St: TryStream, |
| 65 | + F: FnMut(&St::Ok) -> Fut, |
| 66 | + Fut: TryFuture<Ok = bool, Error = St::Error>, |
| 67 | +{ |
| 68 | + type Item = Result<St::Ok, St::Error>; |
| 69 | + |
| 70 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 71 | + let mut this = self.project(); |
| 72 | + |
| 73 | + if *this.done_taking { |
| 74 | + return Poll::Ready(None); |
| 75 | + } |
| 76 | + |
| 77 | + Poll::Ready(loop { |
| 78 | + if let Some(fut) = this.pending_fut.as_mut().as_pin_mut() { |
| 79 | + let take = ready!(fut.try_poll(cx)?); |
| 80 | + let item = this.pending_item.take(); |
| 81 | + this.pending_fut.set(None); |
| 82 | + if take { |
| 83 | + break item.map(Ok); |
| 84 | + } else { |
| 85 | + *this.done_taking = true; |
| 86 | + break None; |
| 87 | + } |
| 88 | + } else if let Some(item) = ready!(this.stream.as_mut().try_poll_next(cx)?) { |
| 89 | + this.pending_fut.set(Some((this.f)(&item))); |
| 90 | + *this.pending_item = Some(item); |
| 91 | + } else { |
| 92 | + break None; |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 98 | + if self.done_taking { |
| 99 | + return (0, Some(0)); |
| 100 | + } |
| 101 | + |
| 102 | + let pending_len = if self.pending_item.is_some() { 1 } else { 0 }; |
| 103 | + let (_, upper) = self.stream.size_hint(); |
| 104 | + let upper = match upper { |
| 105 | + Some(x) => x.checked_add(pending_len), |
| 106 | + None => None, |
| 107 | + }; |
| 108 | + (0, upper) // can't know a lower bound, due to the predicate |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl<St, Fut, F> FusedStream for TryTakeWhile<St, Fut, F> |
| 113 | +where |
| 114 | + St: TryStream + FusedStream, |
| 115 | + F: FnMut(&St::Ok) -> Fut, |
| 116 | + Fut: TryFuture<Ok = bool, Error = St::Error>, |
| 117 | +{ |
| 118 | + fn is_terminated(&self) -> bool { |
| 119 | + self.done_taking || self.pending_item.is_none() && self.stream.is_terminated() |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Forwarding impl of Sink from the underlying stream |
| 124 | +#[cfg(feature = "sink")] |
| 125 | +impl<S, Fut, F, Item, E> Sink<Item> for TryTakeWhile<S, Fut, F> |
| 126 | +where |
| 127 | + S: TryStream + Sink<Item, Error = E>, |
| 128 | +{ |
| 129 | + type Error = E; |
| 130 | + |
| 131 | + delegate_sink!(stream, Item); |
| 132 | +} |
0 commit comments