#[derive(Debug, PartialOrd, Ord, Clone, Copy, Default)] pub struct Once(Option); impl Once { pub fn new() -> Self { Self(None) } #[inline] pub fn get(self) -> Option { self.0 } /// # Panics /// If the value is None #[inline] #[track_caller] pub fn unwrap(self) -> T { self.0.unwrap() } /// # Panics /// If the value is already set #[inline] #[track_caller] pub fn set(&mut self, value: T) { assert!(self.0.is_none(), "already set"); self.0 = Some(value); } } impl PartialEq for Once { fn eq(&self, other: &T) -> bool { self.0.as_ref() == Some(other) } } impl PartialEq for Once { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl Eq for Once {}