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 [`pending`](fn@pending) function. 8 #[derive(Debug)] 9 #[must_use = "streams do nothing unless polled"] 10 pub struct Pending<T>(PhantomData<T>); 11 12 impl<T> Unpin for Pending<T> {} 13 unsafe impl<T> Send for Pending<T> {} 14 unsafe impl<T> Sync for Pending<T> {} 15 16 /// Creates a stream that is never ready 17 /// 18 /// The returned stream is never ready. Attempting to call 19 /// [`next()`](crate::stream::StreamExt::next) will never complete. Use 20 /// [`stream::empty()`](super::empty()) to obtain a stream that is is 21 /// immediately empty but returns no values. 22 /// 23 /// # Examples 24 /// 25 /// Basic usage: 26 /// 27 /// ```no_run 28 /// use tokio::stream::{self, StreamExt}; 29 /// 30 /// #[tokio::main] 31 /// async fn main() { 32 /// let mut never = stream::pending::<i32>(); 33 /// 34 /// // This will never complete 35 /// never.next().await; 36 /// 37 /// unreachable!(); 38 /// } 39 /// ``` pending<T>() -> Pending<T>40pub const fn pending<T>() -> Pending<T> { 41 Pending(PhantomData) 42 } 43 44 impl<T> Stream for Pending<T> { 45 type Item = T; 46 poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<T>>47 fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<T>> { 48 Poll::Pending 49 } 50 size_hint(&self) -> (usize, Option<usize>)51 fn size_hint(&self) -> (usize, Option<usize>) { 52 (0, None) 53 } 54 } 55