use core::async_iter::AsyncIterator; use core::iter::FusedIterator; use core::pin::Pin; use core::slice; use core::task::{Context, Poll}; use crate::alloc::Allocator; #[cfg(not(no_global_oom_handling))] use crate::borrow::Cow; use crate::boxed::Box; #[cfg(not(no_global_oom_handling))] use crate::string::String; use crate::vec; #[cfg(not(no_global_oom_handling))] use crate::vec::Vec; #[stable(feature = "rust1", since = "1.0.0")] impl Iterator for Box { type Item = I::Item; fn next(&mut self) -> Option { (**self).next() } fn size_hint(&self) -> (usize, Option) { (**self).size_hint() } fn nth(&mut self, n: usize) -> Option { (**self).nth(n) } fn last(self) -> Option { BoxIter::last(self) } } trait BoxIter { type Item; fn last(self) -> Option; } impl BoxIter for Box { type Item = I::Item; default fn last(self) -> Option { #[inline] fn some(_: Option, x: T) -> Option { Some(x) } self.fold(None, some) } } /// Specialization for sized `I`s that uses `I`s implementation of `last()` /// instead of the default. #[stable(feature = "rust1", since = "1.0.0")] impl BoxIter for Box { fn last(self) -> Option { (*self).last() } } #[stable(feature = "rust1", since = "1.0.0")] impl DoubleEndedIterator for Box { fn next_back(&mut self) -> Option { (**self).next_back() } fn nth_back(&mut self, n: usize) -> Option { (**self).nth_back(n) } } #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for Box { fn len(&self) -> usize { (**self).len() } fn is_empty(&self) -> bool { (**self).is_empty() } } #[stable(feature = "fused", since = "1.26.0")] impl FusedIterator for Box {} #[unstable(feature = "async_iterator", issue = "79024")] impl AsyncIterator for Box { type Item = S::Item; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { Pin::new(&mut **self).poll_next(cx) } fn size_hint(&self) -> (usize, Option) { (**self).size_hint() } } /// This implementation is required to make sure that the `Box<[I]>: IntoIterator` /// implementation doesn't overlap with `IntoIterator for T where T: Iterator` blanket. #[stable(feature = "boxed_slice_into_iter", since = "1.80.0")] impl !Iterator for Box<[I], A> {} /// This implementation is required to make sure that the `&Box<[I]>: IntoIterator` /// implementation doesn't overlap with `IntoIterator for T where T: Iterator` blanket. #[stable(feature = "boxed_slice_into_iter", since = "1.80.0")] impl<'a, I, A: Allocator> !Iterator for &'a Box<[I], A> {} /// This implementation is required to make sure that the `&mut Box<[I]>: IntoIterator` /// implementation doesn't overlap with `IntoIterator for T where T: Iterator` blanket. #[stable(feature = "boxed_slice_into_iter", since = "1.80.0")] impl<'a, I, A: Allocator> !Iterator for &'a mut Box<[I], A> {} // Note: the `#[rustc_skip_during_method_dispatch(boxed_slice)]` on `trait IntoIterator` // hides this implementation from explicit `.into_iter()` calls on editions < 2024, // so those calls will still resolve to the slice implementation, by reference. #[stable(feature = "boxed_slice_into_iter", since = "1.80.0")] impl IntoIterator for Box<[I], A> { type IntoIter = vec::IntoIter; type Item = I; fn into_iter(self) -> vec::IntoIter { self.into_vec().into_iter() } } #[stable(feature = "boxed_slice_into_iter", since = "1.80.0")] impl<'a, I, A: Allocator> IntoIterator for &'a Box<[I], A> { type IntoIter = slice::Iter<'a, I>; type Item = &'a I; fn into_iter(self) -> slice::Iter<'a, I> { self.iter() } } #[stable(feature = "boxed_slice_into_iter", since = "1.80.0")] impl<'a, I, A: Allocator> IntoIterator for &'a mut Box<[I], A> { type IntoIter = slice::IterMut<'a, I>; type Item = &'a mut I; fn into_iter(self) -> slice::IterMut<'a, I> { self.iter_mut() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_slice_from_iter", since = "1.32.0")] impl FromIterator for Box<[I]> { fn from_iter>(iter: T) -> Self { iter.into_iter().collect::>().into_boxed_slice() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_str_from_iter", since = "1.80.0")] impl FromIterator for Box { fn from_iter>(iter: T) -> Self { String::from_iter(iter).into_boxed_str() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_str_from_iter", since = "1.80.0")] impl<'a> FromIterator<&'a char> for Box { fn from_iter>(iter: T) -> Self { String::from_iter(iter).into_boxed_str() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_str_from_iter", since = "1.80.0")] impl<'a> FromIterator<&'a str> for Box { fn from_iter>(iter: T) -> Self { String::from_iter(iter).into_boxed_str() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_str_from_iter", since = "1.80.0")] impl FromIterator for Box { fn from_iter>(iter: T) -> Self { String::from_iter(iter).into_boxed_str() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_str_from_iter", since = "1.80.0")] impl FromIterator> for Box { fn from_iter>>(iter: T) -> Self { String::from_iter(iter).into_boxed_str() } } #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_str_from_iter", since = "1.80.0")] impl<'a> FromIterator> for Box { fn from_iter>>(iter: T) -> Self { String::from_iter(iter).into_boxed_str() } }