use std::sync::OnceLock;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OnceList<T> {
data: OnceLock<T>,
next: OnceLock<Box<OnceList<T>>>,
}
impl<T> Default for OnceList<T> {
fn default() -> Self {
OnceList::new()
}
}
impl<T> OnceList<T> {
pub const fn new() -> OnceList<T> {
OnceList { data: OnceLock::new(), next: OnceLock::new() }
}
pub fn push(&self, value: T) {
if let Err(value) = self.data.set(value) {
let next = self.next.get_or_init(|| Box::new(OnceList::new()));
next.push(value)
};
}
pub fn contains(&self, example: &T) -> bool
where
T: PartialEq,
{
self.data
.get()
.and_then(|item| (item == example).then_some(true))
.unwrap_or_else(|| self.next.get().map(|next| next.contains(example)).unwrap_or(false))
}
}