use futures_util::stream;
use tokio::sync::mpsc;
pub fn from_slice<T: Send + 'static>(
items: Vec<T>,
) -> impl futures::Stream<Item = T> + Send + 'static {
stream::iter(items)
}
pub fn from_fn<T, F, Fut>(mut f: F) -> impl futures::Stream<Item = T> + Send + 'static
where
T: Send + 'static,
F: FnMut() -> Fut + Send + 'static,
Fut: std::future::Future<Output = Option<T>> + Send + 'static,
{
stream::unfold((), move |()| {
let fut = f();
async move { fut.await.map(|v| (v, ())) }
})
}
pub fn from_channel<T: Send + 'static>(
rx: mpsc::Receiver<T>,
) -> impl futures::Stream<Item = T> + Send + 'static {
tokio_stream::wrappers::ReceiverStream::new(rx)
}