use super::Possible;
use core::iter::FusedIterator;
impl<T> Possible<T> {
#[inline]
pub const fn iter(&self) -> Iter<'_, T> {
Iter {
inner: Item { opt: self.as_ref() },
}
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
IterMut {
inner: Item { opt: self.as_mut() },
}
}
}
#[derive(Debug)]
pub struct Iter<'a, A: 'a> {
inner: Item<&'a A>,
}
impl<'a, A> Iterator for Iter<'a, A> {
type Item = &'a A;
#[inline]
fn next(&mut self) -> Option<&'a A> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, A> DoubleEndedIterator for Iter<'a, A> {
#[inline]
fn next_back(&mut self) -> Option<&'a A> {
self.inner.next_back()
}
}
impl<A> ExactSizeIterator for Iter<'_, A> {}
impl<A> FusedIterator for Iter<'_, A> {}
impl<A> Clone for Iter<'_, A> {
#[inline]
fn clone(&self) -> Self {
Iter {
inner: self.inner.clone(),
}
}
}
#[derive(Debug)]
pub struct IterMut<'a, A: 'a> {
inner: Item<&'a mut A>,
}
impl<'a, A> Iterator for IterMut<'a, A> {
type Item = &'a mut A;
#[inline]
fn next(&mut self) -> Option<&'a mut A> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<'a, A> DoubleEndedIterator for IterMut<'a, A> {
#[inline]
fn next_back(&mut self) -> Option<&'a mut A> {
self.inner.next_back()
}
}
impl<A> ExactSizeIterator for IterMut<'_, A> {}
impl<A> FusedIterator for IterMut<'_, A> {}
#[derive(Clone, Debug)]
pub struct IntoIter<A> {
inner: Item<A>,
}
impl<A> Iterator for IntoIter<A> {
type Item = A;
#[inline]
fn next(&mut self) -> Option<A> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<A> DoubleEndedIterator for IntoIter<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
self.inner.next_back()
}
}
impl<A> ExactSizeIterator for IntoIter<A> {}
impl<A> FusedIterator for IntoIter<A> {}
#[derive(Clone, Debug)]
struct Item<A> {
opt: Possible<A>,
}
impl<A> Iterator for Item<A> {
type Item = A;
#[inline]
fn next(&mut self) -> Option<A> {
match self.opt.take() {
Possible::Some(v) => Some(v),
Possible::None | Possible::Void => None,
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
match self.opt {
Possible::Some(_) => (1, Option::Some(1)),
Possible::None | Possible::Void => (0, Option::Some(0)),
}
}
}
impl<A> DoubleEndedIterator for Item<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
match self.opt.take() {
Possible::Some(v) => Some(v),
Possible::None | Possible::Void => None,
}
}
}
impl<A> ExactSizeIterator for Item<A> {}
impl<A> FusedIterator for Item<A> {}
impl<T> IntoIterator for Possible<T> {
type Item = T;
type IntoIter = IntoIter<T>;
#[inline]
fn into_iter(self) -> IntoIter<T> {
IntoIter {
inner: Item { opt: self },
}
}
}
impl<'a, T> IntoIterator for &'a Possible<T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}
impl<'a, T> IntoIterator for &'a mut Possible<T> {
type Item = &'a mut T;
type IntoIter = IterMut<'a, T>;
fn into_iter(self) -> IterMut<'a, T> {
self.iter_mut()
}
}