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
use super::*;
use std::{
    pin::Pin,
    task::{Context, Poll},
};
use tokio::stream::Stream;

/// A [Stream] that produces an item
///
/// The items are found [here]. The items wil be wrapped in an `Arc` and be `'static`.
///
/// These are returned from an [event subscription][sub]
///
/// [Stream]: https://docs.rs/tokio/0.2/tokio/stream/trait.Stream.html
/// [sub]: ./struct.Dispatcher.html#method.subscribe
/// [here]: ./messages/index.html
pub struct EventStream<T>(pub(crate) mpsc::UnboundedReceiver<T>);

impl<T> std::fmt::Debug for EventStream<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EventStream")
            .field("type", &std::any::type_name::<T>())
            .finish()
    }
}

impl<T: Clone> Stream for EventStream<T> {
    type Item = T;
    fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.0.poll_recv(ctx)
    }
}