engineio_rs/
generator.rs

1use std::pin::Pin;
2
3use futures_util::{Stream, StreamExt};
4
5/// A generator is an internal type that represents a [`Send`] [`futures_util::Stream`]
6/// that yields a certain type `T` whenever it's polled.
7pub type Generator<T> = Pin<Box<dyn Stream<Item = T> + 'static + Send>>;
8
9/// An internal type that implements stream by repeatedly calling [`Stream::poll_next`] on an
10/// underlying stream. Note that the generic parameter will be wrapped in a [`Result`].
11// #[derive(Clone)]
12pub struct StreamGenerator<T, E> {
13    inner: Generator<Result<T, E>>,
14}
15
16impl<T, E> Stream for StreamGenerator<T, E> {
17    type Item = Result<T, E>;
18
19    fn poll_next(
20        mut self: Pin<&mut Self>,
21        cx: &mut std::task::Context<'_>,
22    ) -> std::task::Poll<Option<Self::Item>> {
23        self.inner.poll_next_unpin(cx)
24    }
25}
26
27impl<T, E> StreamGenerator<T, E> {
28    pub fn new(generator_stream: Generator<Result<T, E>>) -> Self {
29        StreamGenerator {
30            inner: generator_stream,
31        }
32    }
33}