use crate::NamespaceComponent;
use super::{NamespaceComponentsHint, NamespacePath};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct IntoComponentsSizeHintOverrideIter<IntoIter> {
hint_override: NamespaceComponentsHint,
into_iter: IntoIter,
}
impl<II> IntoComponentsSizeHintOverrideIter<II> {
#[inline]
pub(super) const fn new(into_iter: II, hint: NamespaceComponentsHint) -> Self {
Self {
hint_override: hint,
into_iter,
}
}
}
impl<I: IntoIterator> IntoIterator for IntoComponentsSizeHintOverrideIter<I>
where
ComponentsSizeHintOverrideIter<I::IntoIter>: Iterator<Item = I::Item>,
{
type Item = I::Item;
type IntoIter = ComponentsSizeHintOverrideIter<I::IntoIter>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
let Self {
hint_override,
into_iter,
} = self;
ComponentsSizeHintOverrideIter::new(into_iter.into_iter(), hint_override)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ComponentsSizeHintOverrideIter<Iter> {
hint_override: NamespaceComponentsHint,
iter: Iter,
}
impl<I> ComponentsSizeHintOverrideIter<I> {
#[must_use]
#[inline]
pub(super) const fn new(iterator: I, hint: NamespaceComponentsHint) -> Self {
Self {
hint_override: hint,
iter: iterator,
}
}
#[inline]
const fn hint_was_wrong(&self) -> bool {
self.hint_override.component_count == 0
}
#[inline]
fn remove_components(&mut self, actual_component_info: NamespaceComponentsHint) {
self.hint_override.component_count = self
.hint_override
.component_count
.saturating_sub(actual_component_info.component_count);
if self.hint_override.component_count == 0 {
self.hint_override.total_component_length = 0;
} else {
self.hint_override.total_component_length = self
.hint_override
.total_component_length
.saturating_sub(actual_component_info.total_component_length);
}
}
#[inline(always)]
pub fn into_inner(self) -> I {
self.iter
}
}
impl<'s, I: Iterator<Item = NamespaceComponent<'s>>> Iterator
for ComponentsSizeHintOverrideIter<I>
{
type Item = NamespaceComponent<'s>;
fn next(&mut self) -> Option<Self::Item> {
let output = self.iter.next();
let to_remove = output
.as_ref()
.map(NamespacePath::components_hint)
.unwrap_or_else(|| self.hint_override);
self.remove_components(to_remove);
output
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
if self.hint_was_wrong() {
self.iter.size_hint()
} else {
(self.hint_override.component_count, None)
}
}
#[inline]
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
self.into_inner().last()
}
#[inline]
fn for_each<F>(self, f: F)
where
Self: Sized,
F: FnMut(Self::Item),
{
self.into_inner().for_each(f);
}
#[inline]
fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.into_inner().fold(init, f)
}
#[inline]
fn reduce<F>(self, f: F) -> Option<Self::Item>
where
Self: Sized,
F: FnMut(Self::Item, Self::Item) -> Self::Item,
{
self.into_inner().reduce(f)
}
#[inline]
fn max(self) -> Option<Self::Item>
where
Self: Sized,
Self::Item: Ord,
{
self.into_inner().max()
}
#[inline]
fn min(self) -> Option<Self::Item>
where
Self: Sized,
Self::Item: Ord,
{
self.into_inner().min()
}
#[inline]
fn max_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item) -> B,
{
self.into_inner().max_by_key(f)
}
#[inline]
fn max_by<F>(self, compare: F) -> Option<Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> core::cmp::Ordering,
{
self.into_inner().max_by(compare)
}
#[inline]
fn min_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item) -> B,
{
self.into_inner().min_by_key(f)
}
#[inline]
fn min_by<F>(self, compare: F) -> Option<Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> core::cmp::Ordering,
{
self.into_inner().min_by(compare)
}
#[inline]
fn cmp<It>(self, other: It) -> core::cmp::Ordering
where
It: IntoIterator<Item = Self::Item>,
Self::Item: Ord,
Self: Sized,
{
self.into_inner().cmp(other)
}
#[inline]
fn partial_cmp<It>(self, other: It) -> Option<core::cmp::Ordering>
where
It: IntoIterator,
Self::Item: PartialOrd<It::Item>,
Self: Sized,
{
self.into_inner().partial_cmp(other)
}
#[inline]
fn eq<It>(self, other: It) -> bool
where
It: IntoIterator,
Self::Item: PartialEq<It::Item>,
Self: Sized,
{
self.into_inner().eq(other)
}
#[inline]
fn ne<It>(self, other: It) -> bool
where
It: IntoIterator,
Self::Item: PartialEq<It::Item>,
Self: Sized,
{
self.into_inner().ne(other)
}
#[inline]
fn lt<It>(self, other: It) -> bool
where
It: IntoIterator,
Self::Item: PartialOrd<It::Item>,
Self: Sized,
{
self.into_inner().lt(other)
}
#[inline]
fn le<It>(self, other: It) -> bool
where
It: IntoIterator,
Self::Item: PartialOrd<It::Item>,
Self: Sized,
{
self.into_inner().le(other)
}
#[inline]
fn gt<It>(self, other: It) -> bool
where
It: IntoIterator,
Self::Item: PartialOrd<It::Item>,
Self: Sized,
{
self.into_inner().gt(other)
}
#[inline]
fn ge<It>(self, other: It) -> bool
where
It: IntoIterator,
Self::Item: PartialOrd<It::Item>,
Self: Sized,
{
self.into_inner().ge(other)
}
#[inline]
fn is_sorted(self) -> bool
where
Self: Sized,
Self::Item: PartialOrd,
{
self.into_inner().is_sorted()
}
#[inline]
fn is_sorted_by<F>(self, compare: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> bool,
{
self.into_inner().is_sorted_by(compare)
}
#[inline]
fn is_sorted_by_key<F, K>(self, f: F) -> bool
where
Self: Sized,
F: FnMut(Self::Item) -> K,
K: PartialOrd,
{
self.into_inner().is_sorted_by_key(f)
}
}
impl<'s, I: DoubleEndedIterator + Iterator<Item = NamespaceComponent<'s>>> DoubleEndedIterator
for ComponentsSizeHintOverrideIter<I>
{
fn next_back(&mut self) -> Option<Self::Item> {
let output = self.iter.next_back();
let removed_amount = output
.as_ref()
.map(NamespacePath::components_hint)
.unwrap_or_else(|| self.hint_override);
self.remove_components(removed_amount);
output
}
#[inline]
fn rfold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.into_inner().rfold(init, f)
}
}