Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make Lazy and LazyOption debug impls match #897

Merged
merged 5 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions near-sdk/src/store/lazy/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ where
Self::get_mut(self)
}
}

impl<T> std::fmt::Debug for Lazy<T>
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()
}
}
}
27 changes: 26 additions & 1 deletion near-sdk/src/store/lazy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
where
T: BorshSerialize,
Expand Down Expand Up @@ -178,4 +178,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::<u8>::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 }");
}
}
}
5 changes: 4 additions & 1 deletion near-sdk/src/store/lazy_option/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("cache", &self.cache.get())
.finish()
}
}
}
25 changes: 22 additions & 3 deletions near-sdk/src/store/lazy_option/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>::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 }"
);
}
}
}