use futures_core::stream::{FusedStream, Stream};
use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Errors {
#[default]
Continue,
Stop,
}
pub trait Fallible {
fn is_failure(&self) -> bool;
}
impl<T, E> Fallible for Result<T, E> {
fn is_failure(&self) -> bool {
self.is_err()
}
}
pub trait Watch {
type Watching: Stream;
fn watch(self) -> Self::Watching;
}
pub trait StopOnError {
fn stop_on_error(self) -> Self;
}
pub fn watch<W: Watch>(streams: W) -> W::Watching {
streams.watch()
}
pub fn watch_with<W>(streams: W, errors: Errors) -> W::Watching
where
W: Watch,
W::Watching: StopOnError,
{
let w = streams.watch();
match errors {
Errors::Continue => w,
Errors::Stop => w.stop_on_error(),
}
}
impl<A: Stream> Watch for (A,) {
type Watching = A;
fn watch(self) -> A {
self.0
}
}
macro_rules! watch_arity {
($Watched:ident, $Watching:ident, $n:expr; $( ($V:ident, $f:ident, $i:expr) ),+ ) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum $Watched<$($V),+> {
$( $V($V), )+
}
impl<$($V),+> $Watched<$($V),+> {
pub fn index(&self) -> usize {
match self {
$( $Watched::$V(_) => $i, )+
}
}
}
impl<$($V: Fallible),+> Fallible for $Watched<$($V),+> {
fn is_failure(&self) -> bool {
match self {
$( $Watched::$V(x) => x.is_failure(), )+
}
}
}
#[must_use = "streams do nothing unless polled"]
pub struct $Watching<$($V: Stream),+> {
$( $f: Option<$V>, )+
next: usize,
stop_if: Option<fn(&$Watched<$($V::Item),+>) -> bool>,
ended: bool,
}
impl<$($V: Stream),+> $Watching<$($V),+> {
pub fn stop_on_error(mut self) -> Self
where
$( $V::Item: Fallible, )+
{
self.stop_if = Some(|w| w.is_failure());
self
}
pub fn live(&self) -> usize {
0 $( + usize::from(self.$f.is_some()) )+
}
fn end(&mut self) {
self.ended = true;
$( self.$f = None; )+
}
}
impl<$($V: Stream),+> StopOnError for $Watching<$($V),+>
where
$( $V::Item: Fallible, )+
{
fn stop_on_error(self) -> Self {
$Watching::stop_on_error(self)
}
}
impl<$($V: Stream),+> fmt::Debug for $Watching<$($V),+> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(stringify!($Watching))
.field("live", &self.live())
.field("stop_on_error", &self.stop_if.is_some())
.field("ended", &self.ended)
.finish()
}
}
impl<$($V: Stream + Unpin),+> Watch for ($($V,)+) {
type Watching = $Watching<$($V),+>;
#[allow(non_snake_case)]
fn watch(self) -> Self::Watching {
let ($($V,)+) = self;
$Watching {
$( $f: Some($V), )+
next: 0,
stop_if: None,
ended: false,
}
}
}
impl<$($V: Stream + Unpin),+> Stream for $Watching<$($V),+> {
type Item = $Watched<$($V::Item),+>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
if this.ended {
return Poll::Ready(None);
}
let start = this.next;
this.next = (start + 1) % $n;
for k in 0..$n {
let i = (start + k) % $n;
$(
if i == $i {
if let Some(s) = this.$f.as_mut() {
match Pin::new(s).poll_next(cx) {
Poll::Ready(Some(x)) => {
let item = $Watched::$V(x);
if this.stop_if.is_some_and(|stop| stop(&item)) {
this.end();
}
return Poll::Ready(Some(item));
}
Poll::Ready(None) => this.$f = None,
Poll::Pending => {}
}
}
}
)+
}
if this.live() == 0 {
this.ended = true;
Poll::Ready(None)
} else {
Poll::Pending
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let mut lo = 0usize;
let mut hi = Some(0usize);
$(
if let Some(s) = &self.$f {
let (l, h) = s.size_hint();
lo = lo.saturating_add(l);
hi = match (hi, h) {
(Some(a), Some(b)) => a.checked_add(b),
_ => None,
};
}
)+
if self.stop_if.is_some() {
lo = lo.min(1);
}
(lo, hi)
}
}
impl<$($V: Stream + Unpin),+> FusedStream for $Watching<$($V),+> {
fn is_terminated(&self) -> bool {
self.ended
}
}
};
}
watch_arity!(Watched2, Watching2, 2; (A, a, 0), (B, b, 1));
watch_arity!(Watched3, Watching3, 3; (A, a, 0), (B, b, 1), (C, c, 2));
watch_arity!(Watched4, Watching4, 4; (A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3));
watch_arity!(Watched5, Watching5, 5; (A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4));
watch_arity!(Watched6, Watching6, 6; (A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5));
watch_arity!(Watched7, Watching7, 7; (A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5), (G, g, 6));
watch_arity!(Watched8, Watching8, 8;
(A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5), (G, g, 6), (H, h, 7));
watch_arity!(Watched9, Watching9, 9;
(A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5), (G, g, 6), (H, h, 7), (I, i_, 8));
watch_arity!(Watched10, Watching10, 10;
(A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5), (G, g, 6), (H, h, 7), (I, i_, 8),
(J, j, 9));
watch_arity!(Watched11, Watching11, 11;
(A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5), (G, g, 6), (H, h, 7), (I, i_, 8),
(J, j, 9), (K, k, 10));
watch_arity!(Watched12, Watching12, 12;
(A, a, 0), (B, b, 1), (C, c, 2), (D, d, 3), (E, e, 4), (F, f, 5), (G, g, 6), (H, h, 7), (I, i_, 8),
(J, j, 9), (K, k, 10), (L, l, 11));