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
//! `All` and `Any`.

use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};

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

use super::super::CompletionStreamExt;

/// Future for [`CompletionStreamExt::all`].
#[derive(Debug)]
pub struct All<'a, S: ?Sized, F> {
    stream: &'a mut S,
    f: F,
}

impl<'a, S: ?Sized, F> All<'a, S, F> {
    pub(crate) fn new(stream: &'a mut S, f: F) -> Self {
        Self { stream, f }
    }
}

impl<S: Unpin + ?Sized, F> Unpin for All<'_, S, F> {}

impl<S: Unpin + ?Sized, F> CompletionFuture for All<'_, S, F>
where
    S: CompletionStream,
    F: FnMut(S::Item) -> bool,
{
    type Output = bool;

    unsafe fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        while let Some(item) = ready!(self.stream.poll_next(cx)) {
            if !(self.f)(item) {
                return Poll::Ready(false);
            }
        }
        Poll::Ready(true)
    }
    unsafe fn poll_cancel(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.stream.poll_cancel(cx)
    }
}

impl<S: Unpin + ?Sized, F> Future for All<'_, S, F>
where
    S: CompletionStream + Stream<Item = <S as CompletionStream>::Item>,
    F: FnMut(<S as CompletionStream>::Item) -> bool,
{
    type Output = bool;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe { CompletionFuture::poll(self, cx) }
    }
}

/// Future for [`CompletionStreamExt::any`].
#[derive(Debug)]
pub struct Any<'a, S: ?Sized, F> {
    stream: &'a mut S,
    f: F,
}

impl<'a, S: ?Sized, F> Any<'a, S, F> {
    pub(crate) fn new(stream: &'a mut S, f: F) -> Self {
        Self { stream, f }
    }
}

impl<S: Unpin + ?Sized, F> Unpin for Any<'_, S, F> {}

impl<S: Unpin + ?Sized, F> CompletionFuture for Any<'_, S, F>
where
    S: CompletionStream,
    F: FnMut(S::Item) -> bool,
{
    type Output = bool;

    unsafe fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        while let Some(item) = ready!(self.stream.poll_next(cx)) {
            if (self.f)(item) {
                return Poll::Ready(true);
            }
        }
        Poll::Ready(false)
    }
    unsafe fn poll_cancel(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        self.stream.poll_cancel(cx)
    }
}

impl<S: Unpin + ?Sized, F> Future for Any<'_, S, F>
where
    S: CompletionStream + Stream<Item = <S as CompletionStream>::Item>,
    F: FnMut(<S as CompletionStream>::Item) -> bool,
{
    type Output = bool;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe { CompletionFuture::poll(self, cx) }
    }
}