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