Files
orchid/src/utils/iter.rs
Lawrence Bethlenfalvy e99ade92ba Cleanup #1
- Removed notes
- Removed superfluous uses of `where`
2023-05-23 18:39:45 +01:00

38 lines
953 B
Rust

/// Utility functions to get rid of explicit casts to BoxedIter which are tedious
use std::iter;
pub type BoxedIter<'a, T> = Box<dyn Iterator<Item = T> + 'a>;
pub type BoxedIterIter<'a, T> = BoxedIter<'a, BoxedIter<'a, T>>;
/// BoxedIter of a single element
pub fn box_once<'a, T: 'a>(t: T) -> BoxedIter<'a, T> {
Box::new(iter::once(t))
}
/// BoxedIter of no elements
pub fn box_empty<'a, T: 'a>() -> BoxedIter<'a, T> {
Box::new(iter::empty())
}
#[macro_export]
macro_rules! box_chain {
($curr:expr) => {
Box::new($curr) as BoxedIter<_>
};
($curr:expr, $($rest:expr),*) => {
Box::new($curr$(.chain($rest))*) as $crate::utils::iter::BoxedIter<_>
};
}
pub fn box_flatten<'a,
T: 'a,
I: 'a + Iterator<Item = J>,
J: 'a + Iterator<Item = T>
>(i: I) -> BoxedIter<'a, T> {
Box::new(i.flatten())
}
pub fn into_boxed_iter<'a,
T: 'a + IntoIterator
>(t: T) -> BoxedIter<'a, <T as IntoIterator>::Item> {
Box::new(t.into_iter())
}