use std::ops::{Deref, DerefMut}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct Steal(pub Option); impl Steal { pub fn new(value: T) -> Steal { Steal(Some(value)) } pub fn steal(&mut self) -> T { self.0 .take() .expect("Attempted to steal already stoken value") } } impl From for Steal { fn from(value: T) -> Self { Steal::new(value) } } impl Deref for Steal { type Target = T; fn deref(&self) -> &Self::Target { self.0 .as_ref() .expect("Attempted to dereference stolen value") } } impl DerefMut for Steal { fn deref_mut(&mut self) -> &mut Self::Target { self.0 .as_mut() .expect("Attempted to dereference stolen value") } }