futures-rx 0.3.3

Rx implementations for the futures crate
Documentation
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use futures::{
    future::{select, Either},
    stream::{Fuse, FusedStream},
    FutureExt, Stream, StreamExt,
};
use pin_project_lite::pin_project;

pin_project! {
    /// Stream for the [`debounce`](RxStreamExt::debounce) method.
    #[must_use = "streams do nothing unless polled"]
    pub struct Debounce<S: Stream, Fut, F> {
        #[pin]
        stream: Fuse<S>,
        f: F,
        #[pin]
        current_interval: Option<Fut>,
        candidate_event: Option<S::Item>,
    }
}

impl<S: Stream, Fut, F> Debounce<S, Fut, F> {
    pub(crate) fn new(stream: S, f: F) -> Self {
        Self {
            stream: stream.fuse(),
            f,
            current_interval: None,
            candidate_event: None,
        }
    }
}

impl<S: Stream, Fut, F> FusedStream for Debounce<S, Fut, F>
where
    F: for<'a> FnMut(&'a S::Item) -> Fut,
    Fut: Future,
{
    fn is_terminated(&self) -> bool {
        self.stream.is_terminated() && self.candidate_event.is_none()
    }
}

impl<S: Stream, Fut, F> Stream for Debounce<S, Fut, F>
where
    F: for<'a> FnMut(&'a S::Item) -> Fut,
    Fut: Future,
{
    type Item = S::Item;

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

        loop {
            if let Some(interval) = this.current_interval.as_mut().as_pin_mut() {
                match select(interval, this.stream.next()).poll_unpin(cx) {
                    Poll::Ready(it) => match it {
                        Either::Left(_) => {
                            this.current_interval.set(None);

                            return Poll::Ready(this.candidate_event.take());
                        }
                        Either::Right((it, mut interval)) => match it {
                            Some(item) => {
                                interval.set((this.f)(&item));
                                *this.candidate_event = Some(item);
                            }
                            None => return Poll::Ready(this.candidate_event.take()),
                        },
                    },
                    Poll::Pending => return Poll::Pending,
                }
            } else {
                match this.stream.as_mut().poll_next(cx) {
                    Poll::Ready(Some(item)) => {
                        this.current_interval.set(Some((this.f)(&item)));
                        *this.candidate_event = Some(item);
                    }
                    Poll::Ready(None) => return Poll::Ready(this.candidate_event.take()),
                    Poll::Pending => return Poll::Pending,
                }
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower, upper) = self.stream.size_hint();
        // we know for sure that the final event (if any) will always emit,
        // any other events depend on a time interval and must be discarded.
        let lower = if lower > 0 { 1 } else { 0 };

        (lower, upper)
    }
}

#[cfg(test)]
mod test {
    use futures::{executor::block_on, stream, Stream, StreamExt};
    use futures_time::{
        future::{FutureExt, IntoFuture},
        time::Duration,
    };

    use crate::RxExt;

    #[test]
    fn smoke() {
        let stream = create_stream();

        block_on(async {
            let all_events = stream
                .debounce(|_| Duration::from_millis(150).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(all_events, [2, 6, 9]);
        });
    }

    fn create_stream() -> impl Stream<Item = usize> {
        stream::unfold(0, move |count| async move {
            if count < 10 {
                let interval = match count {
                    3 | 7 => Duration::from_millis(150),
                    _ => Duration::from_millis(50),
                };

                async { true }.delay(interval).await;

                Some((count, count + 1))
            } else {
                None
            }
        })
    }
}

#[cfg(test)]
mod edge_test {
    use futures::{executor::block_on, stream, StreamExt};
    use futures_time::{future::IntoFuture, time::Duration};

    use crate::RxExt;

    #[test]
    fn an_empty_source_emits_nothing() {
        block_on(async {
            let events = stream::empty::<i32>()
                .debounce(|_| Duration::from_millis(10).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(events, []);
        });
    }

    #[test]
    fn a_burst_collapses_to_its_final_event() {
        block_on(async {
            let events = stream::iter(0..=9)
                .debounce(|_| Duration::from_millis(20).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(events, [9]);
        });
    }

    #[test]
    fn a_lone_event_is_always_emitted() {
        block_on(async {
            let events = stream::iter([1])
                .debounce(|_| Duration::from_millis(10).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(events, [1]);
        });
    }

    #[test]
    fn events_spaced_wider_than_the_window_all_survive() {
        block_on(async {
            let source = stream::unfold(0, |count| async move {
                if count < 3 {
                    Duration::from_millis(60).into_future().await;

                    Some((count, count + 1))
                } else {
                    None
                }
            });
            let events = source
                .debounce(|_| Duration::from_millis(20).into_future())
                .collect::<Vec<_>>()
                .await;

            assert_eq!(events, [0, 1, 2]);
        });
    }
}