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
//! An adapter for merging the output of several streams with priority.
//!
//! The merged stream produces items from either of the underlying streams as they become available,
//! and the streams are polled according to priority.
//!
//! Example:
//! ```
//! # use futures::{prelude::*, stream::iter_ok};
//! use weighted_select::{self, IncompleteSelect};
//!
//! let select = weighted_select::new()
//!     .append(iter_ok::<_, ()>(vec![1u32, 1]), 1)
//!     .append(iter_ok(vec![2, 2, 2, 2, 2]), 3)
//!     .append(iter_ok(vec![3, 3, 3, 3]), 2)
//!     .build();
//!
//! let actual = select.wait().collect::<Result<Vec<_>, _>>().unwrap();
//!
//! assert_eq!(actual, vec![1, 2, 2, 2, 3, 3, 1, 2, 2, 3, 3]);
//! ```

use std::marker::PhantomData;

use futures::{prelude::*, stream::Fuse};

#[cfg(test)]
mod tests;

/// An adapter for merging the output of several streams with priority.
///
/// The merged stream produces items from either of the underlying streams as they become available,
/// and the streams are polled according to priority.
#[must_use = "streams do nothing unless polled"]
#[derive(Debug)]
pub struct Select<N> {
    head: N,
    cursor: u64,
    limit: u64,
}

impl<N> Stream for Select<N>
where
    N: IncompleteSelect,
{
    type Item = N::Item;
    type Error = N::Error;

    fn poll(&mut self) -> Poll<Option<N::Item>, N::Error> {
        let (cnt, res) = match self.head.poll_chain(self.cursor) {
            (_, Ok(Async::NotReady)) | (_, Ok(Async::Ready(None))) if self.cursor > 0 => {
                self.head.poll_chain(0)
            }
            res => res,
        };

        self.cursor = cnt % self.limit;
        res
    }
}

#[derive(Debug)]
pub struct SelectPart<S, N> {
    stream: Fuse<S>,
    weight: u32,
    start_at: u64,
    prev_start_at: u64,
    next: N,
}

pub trait IncompleteSelect: Sized {
    type Item;
    type Error;

    fn append<NS>(self, stream: NS, weight: u32) -> SelectPart<NS, Self>
    where
        NS: Stream<Item = Self::Item, Error = Self::Error>;

    fn build(self) -> Select<Self>;

    fn poll_chain(&mut self, cursor: u64) -> (u64, Poll<Option<Self::Item>, Self::Error>);
}

impl<S, N> IncompleteSelect for SelectPart<S, N>
where
    S: Stream,
    N: IncompleteSelect<Item = S::Item, Error = S::Error>,
{
    type Item = S::Item;
    type Error = S::Error;

    fn append<NS>(self, stream: NS, weight: u32) -> SelectPart<NS, Self>
    where
        NS: Stream<Item = S::Item, Error = S::Error>,
    {
        assert!(weight > 0);

        let start_at = self.start_at + u64::from(self.weight);

        SelectPart {
            stream: stream.fuse(),
            weight,
            start_at,
            prev_start_at: start_at + u64::from(weight),
            next: self,
        }
    }

    fn build(self) -> Select<Self> {
        Select {
            limit: self.prev_start_at,
            head: self,
            cursor: 0,
        }
    }

    fn poll_chain(&mut self, cursor: u64) -> (u64, Poll<Option<Self::Item>, Self::Error>) {
        let (cursor, next_done) = if cursor < self.start_at {
            match self.next.poll_chain(cursor) {
                (cursor, Ok(Async::Ready(None))) => (cursor, true),
                (cursor, Ok(Async::NotReady)) => (cursor, false),
                result => return result,
            }
        } else {
            (cursor, cursor == 0)
        };

        debug_assert!(cursor >= self.start_at);

        match self.stream.poll() {
            Ok(Async::Ready(None)) if next_done => (self.prev_start_at, Ok(Async::Ready(None))),
            Ok(Async::NotReady) | Ok(Async::Ready(None)) => {
                (self.prev_start_at, Ok(Async::NotReady))
            }
            Err(err) => (self.prev_start_at, Err(err)),
            x => (cursor + 1, x),
        }
    }
}

#[derive(Debug)]
struct Terminal<I, E>(PhantomData<(I, E)>);

impl<I, E> IncompleteSelect for Terminal<I, E> {
    type Item = I;
    type Error = E;

    fn append<NS>(self, stream: NS, weight: u32) -> SelectPart<NS, Self>
    where
        NS: Stream<Item = I, Error = E>,
    {
        assert!(weight > 0);

        SelectPart {
            stream: stream.fuse(),
            weight,
            start_at: 0,
            prev_start_at: u64::from(weight),
            next: self,
        }
    }

    fn build(self) -> Select<Self> {
        Select {
            limit: 1, // Avoid calculating the remainder with a divisor of zero.
            head: self,
            cursor: 0,
        }
    }

    #[inline]
    fn poll_chain(&mut self, cursor: u64) -> (u64, Poll<Option<Self::Item>, Self::Error>) {
        debug_assert_eq!(cursor, 0);
        (0, Ok(Async::Ready(None)))
    }
}

pub fn new<I, E>() -> impl IncompleteSelect<Item = I, Error = E> {
    Terminal(PhantomData)
}