#[doc = crate::_tags!(data lifetime)]
#[doc = crate::_doc_location!("data/topol")]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
struct ConstListItem<'a, T: 'a> {
first: T,
rest: &'a ConstList<'a, T>,
}
#[doc = crate::_tags!(data_structure lifetime)]
#[doc = crate::_doc_location!("data/topol")]
#[doc = crate::_doc!(vendor: "const_list")] #[must_use]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct ConstList<'a, T: 'a>(Option<ConstListItem<'a, T>>);
crate::_impl_init![ConstInit: <T> Self::new() => ConstList<'_, T>];
impl<'a, T: 'a> ConstList<'a, T> {
pub const fn new() -> Self {
Self(None)
}
#[must_use]
pub const fn get(&self, index: usize) -> Option<&T> {
if let Some(value) = &self.0 {
if index == 0 { Some(&value.first) } else { value.rest.get(index - 1) }
} else {
None
}
}
#[must_use]
pub const fn len(&self) -> usize {
if let Some(value) = &self.0 { value.rest.len() + 1 } else { 0 }
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.0.is_none()
}
pub const fn push(&'a self, value: T) -> Self {
ConstList(Some(ConstListItem { first: value, rest: self }))
}
pub const fn pop(&'a self) -> (Option<&'a T>, &'a Self) {
if let Some(value) = &self.0 {
(Some(&value.first), value.rest)
} else {
(None, self)
}
}
pub const fn iter(&self) -> ConstListIterator<'_, T> {
ConstListIterator { target: self }
}
}
impl<'a, T> IntoIterator for &'a ConstList<'a, T> {
type Item = &'a T;
type IntoIter = ConstListIterator<'a, T>;
fn into_iter(self) -> Self::IntoIter {
ConstListIterator { target: self }
}
}
#[doc = crate::_tags!(iterator lifetime)]
#[doc = crate::_doc_location!("data/topol")]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ConstListIterator<'a, T> {
target: &'a ConstList<'a, T>,
}
impl<'a, T> Iterator for ConstListIterator<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
let (first, rest) = self.target.pop();
self.target = rest;
first
}
}