use core::iter;
use crate::non_empty::NonEmptyIterator;
pub const fn once<T>(value: T) -> Once<T> {
Once::new(value)
}
pub const fn once_with<T, F: FnOnce() -> T>(function: F) -> OnceWith<F> {
OnceWith::new(function)
}
#[derive(Debug, Clone)]
#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
pub struct Once<T> {
value: T,
}
impl<T> Once<T> {
pub const fn new(value: T) -> Self {
Self { value }
}
}
impl<T> IntoIterator for Once<T> {
type Item = T;
type IntoIter = iter::Once<T>;
fn into_iter(self) -> Self::IntoIter {
iter::once(self.value)
}
}
unsafe impl<T> NonEmptyIterator for Once<T> {}
#[derive(Debug, Clone)]
#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
pub struct OnceWith<F> {
function: F,
}
impl<F> OnceWith<F> {
pub const fn new(function: F) -> Self {
Self { function }
}
}
impl<T, F: FnOnce() -> T> IntoIterator for OnceWith<F> {
type Item = T;
type IntoIter = iter::OnceWith<F>;
fn into_iter(self) -> Self::IntoIter {
iter::once_with(self.function)
}
}
unsafe impl<T, F: FnOnce() -> T> NonEmptyIterator for OnceWith<F> {}