use crate::stdlib::*; pub struct Cached { val: T, changed: bool, } impl Cached { pub fn get(&self) -> &T { &self.val } pub fn new(v: impl Into) -> Self { Self { val: v.into(), changed: true } } pub fn replace(self, v: impl Into) -> Self { Self { val: v.into(), changed: true } } pub fn changed(&mut self) -> bool { mem::replace(&mut self.changed, false) } } impl AsRef for Cached { fn as_ref(&self) -> &T { &self.val } } impl Borrow for Cached { fn borrow(&self) -> &T { &self.val } } impl ops::Deref for Cached { type Target = T; fn deref(&self) -> &T { &self.val } } impl Default for Cached { fn default() -> Self { Self { val: T::default(), changed: true } } } impl Clone for Cached { fn clone(&self) -> Self { Self { val: self.val.clone(), changed: true } } } impl fmt::Debug for Cached { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.val) } } impl fmt::Display for Cached { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.val) } } impl Eq for Cached {} impl PartialEq for Cached { fn eq(&self, r: &Self) -> bool { self.val == r.val } } #[cfg(feature = "adv_fs")] mod serde { use {super::*, crate::ser::*}; impl Serialize for Cached { fn serialize(&self, s: S) -> Result { self.val.serialize(s) } } impl<'de, T: Deserialize<'de>> Deserialize<'de> for Cached { fn deserialize>(d: D) -> Result { let val = T::deserialize(d)?; Ok(Self { val, changed: true }) } } }