use fluent_result::into::IntoResult;
use crate::errors::CollectError;
use crate::errors::capacity::error::CapacityError;
use crate::errors::capacity::traits::{FixedCap, RemainingCap};
use crate::errors::types::SizeHint;
#[cfg(doc)]
use crate::errors::capacity::CapacityErrorKind;
impl<I: Iterator, C> CollectError<I, C, CapacityError<I::Item>> {
#[must_use]
pub fn bounds(iter: I, cap: SizeHint) -> Self
where
C: Default,
{
let hint = iter.size_hint().try_into().expect("Invalid size hint");
Self::new(iter, C::default(), CapacityError::bounds(cap, hint))
}
pub fn ensure_fits_in<Col: FixedCap>(iter: I) -> Result<I, Self>
where
C: Default,
{
match CapacityError::ensure_fits(&iter, Col::CAP) {
Err(error) => Self::new(iter, C::default(), error).into_err(),
Ok(()) => Ok(iter),
}
}
pub fn ensure_fits_into(iter: I, collection: &C) -> Result<I, Self>
where
C: RemainingCap + Default,
{
match CapacityError::ensure_fits(&iter, collection.remaining_cap()) {
Err(error) => Self::new(iter, C::default(), error).into_err(),
Ok(()) => Ok(iter),
}
}
#[must_use]
pub fn overflow(remain: I, collected: C, overflow: I::Item, cap: SizeHint) -> Self {
Self::new(remain, collected, CapacityError::overflow(cap, overflow))
}
#[must_use]
pub fn overflow_remaining_cap<Col: RemainingCap>(remain: I, collected: C, overflow: I::Item, collection: &Col) -> Self {
Self::overflow(remain, collected, overflow, collection.remaining_cap())
}
#[must_use]
pub fn collect_overflow<Col: FixedCap>(iter: I, collected: C, overflow: I::Item) -> Self {
Self::overflow(iter, collected, overflow, Col::CAP)
}
#[must_use]
pub fn underflow(iter: I, collected: C, cap: SizeHint) -> Self
where
for<'a> &'a C: IntoIterator<IntoIter: ExactSizeIterator>,
{
let count = (&collected).into_iter().len();
Self::new(iter, collected, CapacityError::underflow(cap, count))
}
}