Skip to main content

streamify/
any.rs

1use futures_core::stream::Stream;
2use std::marker::Unpin;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6pub struct StreamifyAny<T: Clone> {
7  inner: T,
8  completed: bool,
9}
10
11impl<I: Clone> Unpin for StreamifyAny<I> {}
12
13impl<T: Clone> StreamifyAny<T> {
14  pub fn new(inner: T) -> Self {
15    StreamifyAny {
16      inner,
17      completed: false,
18    }
19  }
20}
21
22impl<T: Clone> Stream for StreamifyAny<T> {
23  type Item = T;
24
25  fn poll_next(self: Pin<&mut Self>, _: &mut Context) -> Poll<Option<Self::Item>> {
26    if self.completed {
27      Poll::Ready(None)
28    } else {
29      Poll::Ready(Some(self.inner.clone()))
30    }
31  }
32
33  fn size_hint(&self) -> (usize, Option<usize>) {
34    (1, Some(1))
35  }
36}
37
38pub fn from_any<T: Clone>(f: T) -> StreamifyAny<T> {
39  StreamifyAny::new(f)
40}