#[doc = crate::_tags!(data lifetime)]
#[doc = crate::_doc_meta!{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_meta!{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![<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) -> ConstListIter<'_, T> {
ConstListIter { target: self }
}
}
impl<'a, T> IntoIterator for &'a ConstList<'a, T> {
type Item = &'a T;
type IntoIter = ConstListIter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
ConstListIter { target: self }
}
}
#[doc = crate::_tags!(iterator lifetime)]
#[doc = crate::_doc_meta!{location("data/topol")}]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ConstListIter<'a, T> {
target: &'a ConstList<'a, T>,
}
impl<'a, T> ConstListIter<'a, T> {
pub const fn next(&mut self) -> Option<&'a T> {
let (first, rest) = self.target.pop();
self.target = rest;
first
}
}
impl<'a, T> Iterator for ConstListIter<'a, T> {
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
self.next()
}
}