use crate::shard::Events as ShardEvents;
use futures_util::stream::{SelectAll, Stream};
use std::{
pin::Pin,
task::{Context, Poll},
};
use twilight_model::gateway::event::Event;
#[derive(Debug)]
pub struct Events {
stream: SelectAll<ShardEventsWithId>,
}
impl Events {
pub(super) const fn new(stream: SelectAll<ShardEventsWithId>) -> Self {
Self { stream }
}
}
impl Stream for Events {
type Item = (u64, Event);
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.stream).poll_next(cx)
}
}
#[derive(Debug)]
pub struct ShardEventsWithId {
id: u64,
stream: ShardEvents,
}
impl ShardEventsWithId {
pub(super) const fn new(id: u64, stream: ShardEvents) -> Self {
Self { id, stream }
}
}
impl Stream for ShardEventsWithId {
type Item = (u64, Event);
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.stream).poll_next(cx) {
Poll::Ready(Some(event)) => Poll::Ready(Some((self.id, event))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}
#[cfg(test)]
mod tests {
use super::Events;
use futures_util::stream::Stream;
use static_assertions::assert_impl_all;
use std::fmt::Debug;
assert_impl_all!(Events: Debug, Send, Stream, Sync);
}