use core::iter;
use non_zero_size::Size;
use crate::non_empty::NonEmptyIterator;
pub const fn repeat<T: Clone>(item: T) -> Repeat<T> {
Repeat::new(item)
}
#[derive(Debug, Clone)]
#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
pub struct Repeat<T: Clone> {
item: T,
}
impl<T: Clone> Repeat<T> {
pub const fn new(item: T) -> Self {
Self { item }
}
}
impl<T: Clone> IntoIterator for Repeat<T> {
type Item = T;
type IntoIter = iter::Repeat<T>;
fn into_iter(self) -> Self::IntoIter {
iter::repeat(self.item)
}
}
unsafe impl<T: Clone> NonEmptyIterator for Repeat<T> {}
pub const fn repeat_with<T, F: FnMut() -> T>(function: F) -> RepeatWith<F> {
RepeatWith::new(function)
}
#[derive(Debug, Clone)]
#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
pub struct RepeatWith<F> {
function: F,
}
impl<F> RepeatWith<F> {
pub const fn new(function: F) -> Self {
Self { function }
}
}
impl<T, F: FnMut() -> T> IntoIterator for RepeatWith<F> {
type Item = T;
type IntoIter = iter::RepeatWith<F>;
fn into_iter(self) -> Self::IntoIter {
iter::repeat_with(self.function)
}
}
unsafe impl<T, F: FnMut() -> T> NonEmptyIterator for RepeatWith<F> {}
pub const fn repeat_n<T: Clone>(item: T, count: Size) -> RepeatN<T> {
RepeatN::new(item, count)
}
#[derive(Debug, Clone)]
#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
pub struct RepeatN<T: Clone> {
item: T,
count: Size,
}
impl<T: Clone> RepeatN<T> {
pub const fn new(item: T, count: Size) -> Self {
Self { item, count }
}
}
impl<T: Clone> IntoIterator for RepeatN<T> {
type Item = T;
type IntoIter = iter::RepeatN<T>;
fn into_iter(self) -> Self::IntoIter {
iter::repeat_n(self.item, self.count.get())
}
}
unsafe impl<T: Clone> NonEmptyIterator for RepeatN<T> {}