pddl/types/
iterators.rs

1//! Contains iterator types.
2
3use std::iter::once;
4use std::vec;
5
6/// An iterator that can represent instances with either
7/// one or many values, such as [`ConditionalEffect`](crate::ConditionalEffect),
8/// [`DurationConstraint`](crate::DurationConstraint),
9/// [`Effect`](crate::Effects) and others.
10pub enum FlatteningIntoIterator<T> {
11    /// The iterator is already empty (similar to [`std::iter::Empty`]).
12    Never,
13    /// The iterator will produce exactly one value (similar to [`std::iter::Once`]).
14    Once(std::iter::Once<T>),
15    /// The iterator will produce none, one or many values.
16    Vec(vec::IntoIter<T>),
17}
18
19impl<T> FlatteningIntoIterator<T> {
20    /// Creates an iterator that will create exactly one item.
21    pub fn new(item: T) -> Self {
22        Self::Once(once(item))
23    }
24
25    /// Creates an iterator that will create none, one or many values.
26    pub fn new_vec(vec: Vec<T>) -> Self {
27        if vec.is_empty() {
28            Self::Never
29        } else {
30            Self::Vec(vec.into_iter())
31        }
32    }
33}
34
35impl<T> Iterator for FlatteningIntoIterator<T> {
36    type Item = T;
37
38    fn next(&mut self) -> Option<Self::Item> {
39        match self {
40            Self::Never => None,
41            Self::Once(ref mut iter) => iter.next(),
42            Self::Vec(ref mut iter) => iter.next(),
43        }
44    }
45}