From 625d379da106a9587dd724db52266c309e50a34d Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Wed, 24 Aug 2022 13:52:42 -0400 Subject: [PATCH 1/3] fix: Make Lazy and LazyOption debug impls match --- near-sdk/src/store/lazy/impls.rs | 16 ++++++++++++++++ near-sdk/src/store/lazy/mod.rs | 22 +++++++++++++++++++++- near-sdk/src/store/lazy_option/impls.rs | 5 ++++- near-sdk/src/store/lazy_option/mod.rs | 25 ++++++++++++++++++++++--- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/near-sdk/src/store/lazy/impls.rs b/near-sdk/src/store/lazy/impls.rs index 582d24bba..17c435f6d 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("cached_value", &self.cache.get().and_then(|v| v.value().as_ref())) + .finish() + } + } +} diff --git a/near-sdk/src/store/lazy/mod.rs b/near-sdk/src/store/lazy/mod.rs index f293e1236..c6c8f229d 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, @@ -178,4 +178,24 @@ mod tests { // be checked for equality. assert_eq!(lazy_loaded, b); } + + #[test] + pub fn test_debug() { + let 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], cached_value: Some(8) }"); + } + + // 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], cached_value: None }"); + } + } } diff --git a/near-sdk/src/store/lazy_option/impls.rs b/near-sdk/src/store/lazy_option/impls.rs index 33742acda..c443222f3 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.storage_key).finish() + f.debug_struct("LazyOption") + .field("storage_key", &self.storage_key) + .field("cached_value", &self.cache.get().map(|v| v.value())) + .finish() } } } diff --git a/near-sdk/src/store/lazy_option/mod.rs b/near-sdk/src/store/lazy_option/mod.rs index 4cf742e07..015c1c992 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], cached_value: Some(None) }" + ); } - 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], cached_value: Some(Some(1)) }" + ); + } + + // 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], cached_value: None }" + ); } } } From c867e89171e4ee3b28198c8da2c156c4ad9129cd Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Thu, 25 Aug 2022 15:26:17 -0400 Subject: [PATCH 2/3] Include extra data in debug impl --- near-sdk/src/store/lazy/impls.rs | 2 +- near-sdk/src/store/lazy/mod.rs | 11 ++++++++--- near-sdk/src/store/lazy_option/impls.rs | 2 +- near-sdk/src/store/lazy_option/mod.rs | 6 +++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/near-sdk/src/store/lazy/impls.rs b/near-sdk/src/store/lazy/impls.rs index 17c435f6d..cfb01ee11 100644 --- a/near-sdk/src/store/lazy/impls.rs +++ b/near-sdk/src/store/lazy/impls.rs @@ -100,7 +100,7 @@ where } else { f.debug_struct("Lazy") .field("storage_key", &self.storage_key) - .field("cached_value", &self.cache.get().and_then(|v| v.value().as_ref())) + .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 c6c8f229d..b6885cad1 100644 --- a/near-sdk/src/store/lazy/mod.rs +++ b/near-sdk/src/store/lazy/mod.rs @@ -181,11 +181,16 @@ mod tests { #[test] pub fn test_debug() { - let lazy = Lazy::new(b"m", 8u8); + 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], cached_value: Some(8) }"); + 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. @@ -195,7 +200,7 @@ mod tests { if cfg!(feature = "expensive-debug") { assert_eq!(format!("{:?}", lazy), "8"); } else { - assert_eq!(format!("{:?}", lazy), "Lazy { storage_key: [109], cached_value: None }"); + 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 c443222f3..0dfacedd1 100644 --- a/near-sdk/src/store/lazy_option/impls.rs +++ b/near-sdk/src/store/lazy_option/impls.rs @@ -41,7 +41,7 @@ where } else { f.debug_struct("LazyOption") .field("storage_key", &self.storage_key) - .field("cached_value", &self.cache.get().map(|v| v.value())) + .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 015c1c992..8a3af02e1 100644 --- a/near-sdk/src/store/lazy_option/mod.rs +++ b/near-sdk/src/store/lazy_option/mod.rs @@ -169,7 +169,7 @@ mod tests { } else { assert_eq!( format!("{:?}", lazy_option), - "LazyOption { storage_key: [109], cached_value: Some(None) }" + "LazyOption { storage_key: [109], cache: Some(CacheEntry { value: None, state: Cached }) }" ); } @@ -179,7 +179,7 @@ mod tests { } else { assert_eq!( format!("{:?}", lazy_option), - "LazyOption { storage_key: [109], cached_value: Some(Some(1)) }" + "LazyOption { storage_key: [109], cache: Some(CacheEntry { value: Some(1), state: Modified }) }" ); } @@ -192,7 +192,7 @@ mod tests { } else { assert_eq!( format!("{:?}", lazy_option), - "LazyOption { storage_key: [109], cached_value: None }" + "LazyOption { storage_key: [109], cache: None }" ); } } From 098e67b6a1c7c58f11cb86df6b2ddc35fc19d83e Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Mon, 29 Aug 2022 21:15:49 -0400 Subject: [PATCH 3/3] fix field name --- near-sdk/src/store/lazy_option/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/near-sdk/src/store/lazy_option/impls.rs b/near-sdk/src/store/lazy_option/impls.rs index 0dfacedd1..2fdbb2bff 100644 --- a/near-sdk/src/store/lazy_option/impls.rs +++ b/near-sdk/src/store/lazy_option/impls.rs @@ -40,7 +40,7 @@ where self.get().fmt(f) } else { f.debug_struct("LazyOption") - .field("storage_key", &self.storage_key) + .field("storage_key", &self.prefix) .field("cache", &self.cache.get()) .finish() }