pub trait Location: Clone {
fn new<'a>(stack: impl Iterator<Item = &'a Self> + Clone, len: usize) -> Self
where
Self: 'a;
}
#[derive(Clone, Default, Debug, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct DefaultLocation;
impl Location for DefaultLocation {
fn new<'a>(_stack: impl Iterator<Item = &'a Self> + Clone, _len: usize) -> Self {
DefaultLocation
}
}
impl<T> Location for std::ops::Range<T>
where
T: Clone + Default + Ord,
{
fn new<'a>(mut stack: impl Iterator<Item = &'a Self> + Clone, len: usize) -> Self
where
Self: 'a,
{
if len == 0 {
if let Some(last) = stack.next() {
let end = last.end.clone();
end.clone()..end
} else {
T::default()..T::default()
}
} else {
let mut stack = stack.take(len);
let last = stack.next().unwrap();
let first = stack.last().unwrap_or(last);
first.start.clone()..last.end.clone()
}
}
}