diff --git a/near-sdk/src/store/lazy/impls.rs b/near-sdk/src/store/lazy/impls.rs index 582d24bba..cfb01ee11 100644 --- a/near-sdk/src/store/lazy/impls.rs +++ b/near-sdk/src/store/lazy/impls.rs @@ -89,3 +89,19 @@ where Self::get_mut(self) } } + +impl std::fmt::Debug for Lazy +where + T: std::fmt::Debug + BorshSerialize + BorshDeserialize, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if cfg!(feature = "expensive-debug") { + self.get().fmt(f) + } else { + f.debug_struct("Lazy") + .field("storage_key", &self.storage_key) + .field("cache", &self.cache.get()) + .finish() + } + } +} diff --git a/near-sdk/src/store/lazy/mod.rs b/near-sdk/src/store/lazy/mod.rs index 61f235074..4d0682bbb 100644 --- a/near-sdk/src/store/lazy/mod.rs +++ b/near-sdk/src/store/lazy/mod.rs @@ -60,7 +60,7 @@ where /// *a = "new string".to_string(); /// assert_eq!(a.get(), "new string"); /// ``` -#[derive(BorshSerialize, BorshDeserialize, Debug)] +#[derive(BorshSerialize, BorshDeserialize)] pub struct Lazy where T: BorshSerialize, @@ -183,4 +183,29 @@ mod tests { // be checked for equality. assert_eq!(lazy_loaded, b); } + + #[test] + pub fn test_debug() { + let mut lazy = Lazy::new(b"m", 8u8); + if cfg!(feature = "expensive-debug") { + assert_eq!(format!("{:?}", lazy), "8"); + } else { + assert_eq!(format!("{:?}", lazy), "Lazy { storage_key: [109], cache: Some(CacheEntry { value: Some(8), state: Modified }) }"); + } + + lazy.flush(); + if !cfg!(feature = "expensive-debug") { + assert_eq!(format!("{:?}", lazy), "Lazy { storage_key: [109], cache: Some(CacheEntry { value: Some(8), state: Cached }) }"); + } + + // Serialize and deserialize to simulate storing and loading. + let serialized = borsh::to_vec(&lazy).unwrap(); + drop(lazy); + let lazy = Lazy::::try_from_slice(&serialized).unwrap(); + if cfg!(feature = "expensive-debug") { + assert_eq!(format!("{:?}", lazy), "8"); + } else { + assert_eq!(format!("{:?}", lazy), "Lazy { storage_key: [109], cache: None }"); + } + } } diff --git a/near-sdk/src/store/lazy_option/impls.rs b/near-sdk/src/store/lazy_option/impls.rs index d140629b5..2fdbb2bff 100644 --- a/near-sdk/src/store/lazy_option/impls.rs +++ b/near-sdk/src/store/lazy_option/impls.rs @@ -39,7 +39,10 @@ where if cfg!(feature = "expensive-debug") { self.get().fmt(f) } else { - f.debug_struct("LazyOption").field("storage_key", &self.prefix).finish() + f.debug_struct("LazyOption") + .field("storage_key", &self.prefix) + .field("cache", &self.cache.get()) + .finish() } } } diff --git a/near-sdk/src/store/lazy_option/mod.rs b/near-sdk/src/store/lazy_option/mod.rs index 6ba96bbe9..f5383204d 100644 --- a/near-sdk/src/store/lazy_option/mod.rs +++ b/near-sdk/src/store/lazy_option/mod.rs @@ -167,14 +167,33 @@ mod tests { if cfg!(feature = "expensive-debug") { assert_eq!(format!("{:?}", lazy_option), "None"); } else { - assert_eq!(format!("{:?}", lazy_option), "LazyOption { storage_key: [109] }"); + assert_eq!( + format!("{:?}", lazy_option), + "LazyOption { storage_key: [109], cache: Some(CacheEntry { value: None, state: Cached }) }" + ); } - lazy_option.set(Some(1u64)); + *lazy_option = Some(1u8); if cfg!(feature = "expensive-debug") { assert_eq!(format!("{:?}", lazy_option), "Some(1)"); } else { - assert_eq!(format!("{:?}", lazy_option), "LazyOption { storage_key: [109] }"); + assert_eq!( + format!("{:?}", lazy_option), + "LazyOption { storage_key: [109], cache: Some(CacheEntry { value: Some(1), state: Modified }) }" + ); + } + + // Serialize and deserialize to simulate storing and loading. + let serialized = borsh::to_vec(&lazy_option).unwrap(); + drop(lazy_option); + let lazy_option = LazyOption::::try_from_slice(&serialized).unwrap(); + if cfg!(feature = "expensive-debug") { + assert_eq!(format!("{:?}", lazy_option), "Some(1)"); + } else { + assert_eq!( + format!("{:?}", lazy_option), + "LazyOption { storage_key: [109], cache: None }" + ); } } }