use crate::fmt; use crate::iter::{FusedIterator, TrustedLen}; use crate::marker; /// Creates an iterator that yields nothing. /// /// # Examples /// /// Basic usage: /// /// ``` /// use std::iter; /// /// // this could have been an iterator over i32, but alas, it's just not. /// let mut nope = iter::empty::(); /// /// assert_eq!(None, nope.next()); /// ``` #[stable(feature = "iter_empty", since = "1.2.0")] #[rustc_const_stable(feature = "const_iter_empty", since = "1.32.0")] pub const fn empty() -> Empty { Empty(marker::PhantomData) } /// An iterator that yields nothing. /// /// This `struct` is created by the [`empty()`] function. See its documentation for more. #[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "iter_empty", since = "1.2.0")] pub struct Empty(marker::PhantomData T>); #[stable(feature = "core_impl_debug", since = "1.9.0")] impl fmt::Debug for Empty { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Empty").finish() } } #[stable(feature = "iter_empty", since = "1.2.0")] impl Iterator for Empty { type Item = T; fn next(&mut self) -> Option { None } fn size_hint(&self) -> (usize, Option) { (0, Some(0)) } } #[stable(feature = "iter_empty", since = "1.2.0")] impl DoubleEndedIterator for Empty { fn next_back(&mut self) -> Option { None } } #[stable(feature = "iter_empty", since = "1.2.0")] impl ExactSizeIterator for Empty { fn len(&self) -> usize { 0 } } #[unstable(feature = "trusted_len", issue = "37572")] unsafe impl TrustedLen for Empty {} #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Empty {} // not #[derive] because that adds a Clone bound on T, // which isn't necessary. #[stable(feature = "iter_empty", since = "1.2.0")] impl Clone for Empty { fn clone(&self) -> Empty { Empty(marker::PhantomData) } } // not #[derive] because that adds a Default bound on T, // which isn't necessary. #[stable(feature = "iter_empty", since = "1.2.0")] impl Default for Empty { fn default() -> Empty { Empty(marker::PhantomData) } }