use fluent_result::into::IntoResult;
use crate::errors::ExtendError;
use crate::errors::capacity::error::CapacityError;
use crate::errors::capacity::traits::RemainingCap;
use crate::errors::types::SizeHint;
#[cfg(doc)]
use crate::errors::capacity::CapacityErrorKind;
impl<I: Iterator> ExtendError<I, CapacityError<I::Item>> {
#[must_use]
pub fn bounds(iter: I, cap: SizeHint) -> Self {
let hint = iter.size_hint().try_into().expect("Invalid size hint");
Self::new(iter, CapacityError::bounds(cap, hint))
}
pub fn ensure_fits_into<C: RemainingCap>(iter: I, collection: &C) -> Result<I, Self> {
match CapacityError::ensure_fits(&iter, collection.remaining_cap()) {
Ok(()) => Ok(iter),
Err(error) => Self::new(iter, error).into_err(),
}
}
#[must_use]
pub fn overflow(iter: I, overflow: I::Item) -> Self {
Self::new(iter, CapacityError::overflow(SizeHint::ZERO, overflow))
}
#[must_use]
pub fn overflow_remaining_cap<C: RemainingCap>(iter: I, overflow: I::Item, collection: &C) -> Self {
Self::new(iter, CapacityError::overflow(collection.remaining_cap(), overflow))
}
}