async_std/stream/stream/
cmp.rs

1use core::cmp::Ordering;
2use core::future::Future;
3use core::pin::Pin;
4
5use pin_project_lite::pin_project;
6
7use super::fuse::Fuse;
8use crate::stream::stream::StreamExt;
9use crate::stream::Stream;
10use crate::task::{Context, Poll};
11
12pin_project! {
13    // Lexicographically compares the elements of this `Stream` with those
14    // of another using `Ord`.
15    #[doc(hidden)]
16    #[allow(missing_debug_implementations)]
17    pub struct CmpFuture<L: Stream, R: Stream> {
18        #[pin]
19        l: Fuse<L>,
20        #[pin]
21        r: Fuse<R>,
22        l_cache: Option<L::Item>,
23        r_cache: Option<R::Item>,
24    }
25}
26
27impl<L: Stream, R: Stream> CmpFuture<L, R> {
28    pub(super) fn new(l: L, r: R) -> Self {
29        Self {
30            l: l.fuse(),
31            r: r.fuse(),
32            l_cache: None,
33            r_cache: None,
34        }
35    }
36}
37
38impl<L: Stream, R: Stream> Future for CmpFuture<L, R>
39where
40    L: Stream + Sized,
41    R: Stream<Item = L::Item> + Sized,
42    L::Item: Ord,
43{
44    type Output = Ordering;
45
46    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
47        let mut this = self.project();
48        loop {
49            // Stream that completes earliest can be considered Less, etc
50            let l_complete = this.l.done && this.l_cache.is_none();
51            let r_complete = this.r.done && this.r_cache.is_none();
52
53            if l_complete && r_complete {
54                return Poll::Ready(Ordering::Equal);
55            } else if l_complete {
56                return Poll::Ready(Ordering::Less);
57            } else if r_complete {
58                return Poll::Ready(Ordering::Greater);
59            }
60
61            // Get next value if possible and necessary
62            if !this.l.done && this.l_cache.is_none() {
63                let l_next = futures_core::ready!(this.l.as_mut().poll_next(cx));
64                if let Some(item) = l_next {
65                    *this.l_cache = Some(item);
66                }
67            }
68
69            if !this.r.done && this.r_cache.is_none() {
70                let r_next = futures_core::ready!(this.r.as_mut().poll_next(cx));
71                if let Some(item) = r_next {
72                    *this.r_cache = Some(item);
73                }
74            }
75
76            // Compare if both values are available.
77            if this.l_cache.is_some() && this.r_cache.is_some() {
78                let l_value = this.l_cache.take().unwrap();
79                let r_value = this.r_cache.take().unwrap();
80                let result = l_value.cmp(&r_value);
81
82                if let Ordering::Equal = result {
83                    // Reset cache to prepare for next comparison
84                    *this.l_cache = None;
85                    *this.r_cache = None;
86                } else {
87                    // Return non equal value
88                    return Poll::Ready(result);
89                }
90            }
91        }
92    }
93}