//! Draining iterator for [`ArrayString`] //! //! [`ArrayString`]: ../struct.ArrayString.html use crate::{prelude::*, utils::IntoLossy}; use core::fmt::{self, Debug, Formatter}; use core::{cmp::Ordering, hash::Hash, hash::Hasher, iter::FusedIterator}; /// A draining iterator for [`ArrayString`]. /// /// Created through [`drain`] /// /// [`ArrayString`]: ../struct.ArrayString.html /// [`drain`]: ../struct.ArrayString.html#method.drain #[derive(Clone, Default)] pub struct Drain(pub(crate) ArrayString, pub(crate) u8); impl Debug for Drain where SIZE: Capacity, { #[inline] fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_tuple("Drain") .field(&self.0) .field(&self.1) .finish() } } impl PartialEq for Drain where SIZE: Capacity, { #[inline] fn eq(&self, other: &Self) -> bool { self.as_str().eq(other.as_str()) } } impl Eq for Drain {} impl Ord for Drain where SIZE: Capacity, { #[inline] fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialOrd for Drain where SIZE: Capacity, { #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Hash for Drain where SIZE: Capacity, { #[inline] fn hash(&self, hasher: &mut H) { self.as_str().hash(hasher) } } impl Copy for Drain where SIZE::Array: Copy {} impl Drain { /// Extracts string slice containing the remaining characters of `Drain`. #[inline] pub fn as_str(&self) -> &str { unsafe { self.0.as_str().get_unchecked(self.1.into()..) } } } impl Iterator for Drain { type Item = char; #[inline] fn next(&mut self) -> Option { self.0 .as_str() .get(self.1.into()..) .and_then(|s| s.chars().next()) .map(|c| { self.1 = self.1.saturating_add(c.len_utf8().into_lossy()); c }) } } impl DoubleEndedIterator for Drain { #[inline] fn next_back(&mut self) -> Option { self.0.pop() } } impl FusedIterator for Drain {}