1 | use super::assert_stream; |
2 | use core::marker::PhantomData; |
3 | use core::pin::Pin; |
4 | use futures_core::stream::{FusedStream, Stream}; |
5 | use futures_core::task::{Context, Poll}; |
6 | |
7 | /// Stream for the [`empty`] function. |
8 | #[derive(Debug)] |
9 | #[must_use = "streams do nothing unless polled" ] |
10 | pub struct Empty<T> { |
11 | _phantom: PhantomData<T>, |
12 | } |
13 | |
14 | /// Creates a stream which contains no elements. |
15 | /// |
16 | /// The returned stream will always return `Ready(None)` when polled. |
17 | pub fn empty<T>() -> Empty<T> { |
18 | assert_stream::<T, _>(Empty { _phantom: PhantomData }) |
19 | } |
20 | |
21 | impl<T> Unpin for Empty<T> {} |
22 | |
23 | impl<T> FusedStream for Empty<T> { |
24 | fn is_terminated(&self) -> bool { |
25 | true |
26 | } |
27 | } |
28 | |
29 | impl<T> Stream for Empty<T> { |
30 | type Item = T; |
31 | |
32 | fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
33 | Poll::Ready(None) |
34 | } |
35 | |
36 | fn size_hint(&self) -> (usize, Option<usize>) { |
37 | (0, Some(0)) |
38 | } |
39 | } |
40 | |
41 | impl<T> Clone for Empty<T> { |
42 | fn clone(&self) -> Self { |
43 | empty() |
44 | } |
45 | } |
46 | |