1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use core::fmt::{self, Debug, Formatter};
use core::future::Future;
use core::mem;
use core::pin::Pin;
use core::task::{Context, Poll};

use completion_core::{CompletionFuture, CompletionStream};
use futures_core::{ready, Stream};
use pin_project_lite::pin_project;

pin_project! {
    /// Stream for [`CompletionStreamExt::peekable`](crate::CompletionStreamExt::peekable).
    #[derive(Debug, Clone)]
    pub struct Peekable<S: CompletionStream> {
        #[pin]
        stream: S,
        peeked: Poll<Option<S::Item>>,
    }
}

impl<S: CompletionStream> Peekable<S> {
    pub(crate) fn new(stream: S) -> Self {
        Self {
            stream,
            peeked: Poll::Pending,
        }
    }

    /// Peek the next value in the stream.
    ///
    /// # Cancellation
    ///
    /// If the returned future is cancelled and the next value in the stream has not been peeked
    /// yet, the entire stream is cancelled, otherwise nothing happens.
    ///
    /// # Examples
    ///
    /// ```
    /// use completion::{CompletionStreamExt, StreamExt};
    /// use futures_lite::{stream, pin};
    ///
    /// # completion::future::block_on(completion::completion_async! {
    /// let stream = stream::once(5).into_completion().peekable();
    /// pin!(stream);
    ///
    /// assert_eq!(stream.as_mut().peek().await, Some(&5));
    /// assert_eq!(stream.as_mut().peek().await, Some(&5));
    /// assert_eq!(stream.next().await, Some(5));
    /// assert_eq!(stream.as_mut().peek().await, None);
    /// assert_eq!(stream.next().await, None);
    /// # });
    /// ```
    #[must_use]
    pub fn peek(self: Pin<&mut Self>) -> Peek<'_, S> {
        Peek { stream: Some(self) }
    }

    /// Peek the next value in the stream if the underlying type implements [`Unpin`].
    ///
    /// `stream.peek_unpin()` is equivalent to `Pin::new(&mut stream).peek()`. See
    /// [`peek`](Self::peek) for more information.
    ///
    /// # Examples
    ///
    /// ```
    /// use completion::{CompletionStreamExt, StreamExt};
    /// use futures_lite::stream;
    ///
    /// # completion::future::block_on(completion::completion_async! {
    /// let mut stream = stream::once(8).into_completion().peekable();
    ///
    /// assert_eq!(stream.peek_unpin().await, Some(&8));
    /// assert_eq!(stream.peek_unpin().await, Some(&8));
    /// assert_eq!(stream.next().await, Some(8));
    /// assert_eq!(stream.peek_unpin().await, None);
    /// assert_eq!(stream.next().await, None);
    /// # });
    /// ```
    #[must_use]
    pub fn peek_unpin(&mut self) -> Peek<'_, S>
    where
        Self: Unpin,
    {
        Pin::new(self).peek()
    }

    /// Attempt to peek the next value in the stream.
    ///
    /// This function is quite low level, use [`peek`](Self::peek) or
    /// [`peek_unpin`](Self::peek_unpin) for a higher-level equivalent.
    ///
    /// # Safety
    ///
    /// See [`CompletionStream::poll_next`].
    pub unsafe fn poll_peek(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<&S::Item>> {
        let mut this = self.project();

        if this.peeked.is_pending() {
            *this.peeked = this.stream.as_mut().poll_next(cx);
        }
        match &*this.peeked {
            Poll::Ready(ready) => Poll::Ready(ready.as_ref()),
            Poll::Pending => Poll::Pending,
        }
    }

    /// Attempt to cancel peeking the next value in the stream.
    ///
    /// This will cancel the underlying stream if the next value in the stream has not already been
    /// peeked.
    ///
    /// This function is quite low level, use [`peek`](Self::peek) or
    /// [`peek_unpin`](Self::peek_unpin) for a higher-level equivalent.
    ///
    /// # Safety
    ///
    /// See [`CompletionStream::poll_cancel`].
    pub unsafe fn poll_peek_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        let this = self.project();

        if this.peeked.is_pending() {
            this.stream.poll_cancel(cx)
        } else {
            Poll::Ready(())
        }
    }
}

impl<S: CompletionStream> CompletionStream for Peekable<S> {
    type Item = S::Item;

    unsafe fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        if this.peeked.is_pending() {
            *this.peeked = this.stream.as_mut().poll_next(cx);
        }
        mem::replace(this.peeked, Poll::Pending)
    }

    unsafe fn poll_cancel(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        let this = self.project();

        if this.peeked.is_pending() {
            this.stream.poll_cancel(cx)
        } else {
            Poll::Ready(())
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let peek_len = match self.peeked {
            Poll::Ready(Some(_)) => 1,
            Poll::Ready(None) => return (0, Some(0)),
            Poll::Pending => 0,
        };
        let (lower, upper) = self.stream.size_hint();
        (
            lower.saturating_add(peek_len),
            upper.and_then(|n| n.checked_add(peek_len)),
        )
    }
}

impl<S> Stream for Peekable<S>
where
    S: CompletionStream + Stream<Item = <S as CompletionStream>::Item>,
{
    type Item = <S as CompletionStream>::Item;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        unsafe { CompletionStream::poll_next(self, cx) }
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        CompletionStream::size_hint(self)
    }
}

/// Future for [`Peekable::peek`].
pub struct Peek<'a, S: CompletionStream> {
    stream: Option<Pin<&'a mut Peekable<S>>>,
}

impl<S: CompletionStream + Debug> Debug for Peek<'_, S>
where
    S::Item: Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Peek")
            .field("stream", &self.stream)
            .finish()
    }
}

impl<'a, S> CompletionFuture for Peek<'a, S>
where
    S: CompletionStream,
{
    type Output = Option<&'a S::Item>;
    unsafe fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let stream = self.stream.as_mut().expect("polled after completion");
        ready!(stream.as_mut().poll_peek(cx));
        self.stream.take().unwrap().poll_peek(cx)
    }
    unsafe fn poll_cancel(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        let stream = self.stream.as_mut().expect("polled after completion");
        stream.as_mut().poll_peek_cancel(cx)
    }
}

impl<'a, S> Future for Peek<'a, S>
where
    S: CompletionStream + Stream<Item = <S as CompletionStream>::Item>,
{
    type Output = <Self as CompletionFuture>::Output;
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe { CompletionFuture::poll(self, cx) }
    }
}