broker_tokio/stream/
empty.rs

1use crate::stream::Stream;
2
3use core::marker::PhantomData;
4use core::pin::Pin;
5use core::task::{Context, Poll};
6
7/// Stream for the [`empty`] function.
8#[derive(Debug)]
9#[must_use = "streams do nothing unless polled"]
10pub struct Empty<T>(PhantomData<T>);
11
12impl<T> Unpin for Empty<T> {}
13
14/// Creates a stream that yields nothing.
15///
16/// The returned stream is immediately ready and returns `None`. Use
17/// [`stream::pending()`](super::pending()) to obtain a stream that is never
18/// ready.
19///
20/// # Examples
21///
22/// Basic usage:
23///
24/// ```
25/// use tokio::stream::{self, StreamExt};
26///
27/// #[tokio::main]
28/// async fn main() {
29///     let mut none = stream::empty::<i32>();
30///
31///     assert_eq!(None, none.next().await);
32/// }
33/// ```
34pub const fn empty<T>() -> Empty<T> {
35    Empty(PhantomData)
36}
37
38impl<T> Stream for Empty<T> {
39    type Item = T;
40
41    fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<T>> {
42        Poll::Ready(None)
43    }
44
45    fn size_hint(&self) -> (usize, Option<usize>) {
46        (0, Some(0))
47    }
48}