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

Add Debug impl to the following caches #138

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::{
any::TypeId,
borrow::Borrow,
collections::hash_map::RandomState,
fmt,
future::Future,
hash::{BuildHasher, Hash},
sync::Arc,
Expand Down Expand Up @@ -314,6 +315,24 @@ impl<K, V, S> Clone for Cache<K, V, S> {
}
}

impl<K, V, S> fmt::Debug for Cache<K, V, S>
where
K: fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: fmt::Debug + Clone + Send + Sync + 'static,
// TODO: Remove these bounds from S.
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();

for (k, v) in self.iter() {
d_map.entry(&k, &v);
}

d_map.finish()
}
}

impl<K, V, S> Cache<K, V, S> {
/// Returns a read-only cache policy of this cache.
///
Expand Down Expand Up @@ -2137,4 +2156,19 @@ mod tests {
Ok(5)
);
}

#[tokio::test]
async fn test_debug_format() {
let cache = Cache::new(10);
cache.insert('a', "alice").await;
cache.insert('b', "bob").await;
cache.insert('c', "cindy").await;

let debug_str = format!("{:?}", cache);
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': "alice""#));
assert!(debug_str.contains(r#"'b': "bob""#));
assert!(debug_str.contains(r#"'c': "cindy""#));
assert!(debug_str.ends_with('}'));
}
}
34 changes: 34 additions & 0 deletions src/sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
any::TypeId,
borrow::Borrow,
collections::hash_map::RandomState,
fmt,
hash::{BuildHasher, Hash},
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -263,6 +264,24 @@ impl<K, V, S> Clone for Cache<K, V, S> {
}
}

impl<K, V, S> fmt::Debug for Cache<K, V, S>
where
K: fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: fmt::Debug + Clone + Send + Sync + 'static,
// TODO: Remove these bounds from S.
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();

for (k, v) in self.iter() {
d_map.entry(&k, &v);
}

d_map.finish()
}
}

impl<K, V, S> Cache<K, V, S> {
/// Returns a read-only cache policy of this cache.
///
Expand Down Expand Up @@ -1899,4 +1918,19 @@ mod tests {
Ok(5)
);
}

#[test]
fn test_debug_format() {
let cache = Cache::new(10);
cache.insert('a', "alice");
cache.insert('b', "bob");
cache.insert('c', "cindy");

let debug_str = format!("{:?}", cache);
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': "alice""#));
assert!(debug_str.contains(r#"'b': "bob""#));
assert!(debug_str.contains(r#"'c': "cindy""#));
assert!(debug_str.ends_with('}'));
}
}
34 changes: 34 additions & 0 deletions src/sync/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{Policy, PredicateError};
use std::{
borrow::Borrow,
collections::hash_map::RandomState,
fmt,
hash::{BuildHasher, Hash, Hasher},
sync::Arc,
time::Duration,
Expand Down Expand Up @@ -57,6 +58,24 @@ impl<K, V, S> Clone for SegmentedCache<K, V, S> {
}
}

impl<K, V, S> fmt::Debug for SegmentedCache<K, V, S>
where
K: fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: fmt::Debug + Clone + Send + Sync + 'static,
// TODO: Remove these bounds from S.
S: BuildHasher + Clone + Send + Sync + 'static,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();

for (k, v) in self.iter() {
d_map.entry(&k, &v);
}

d_map.finish()
}
}

impl<K, V> SegmentedCache<K, V, RandomState>
where
K: Hash + Eq + Send + Sync + 'static,
Expand Down Expand Up @@ -1407,4 +1426,19 @@ mod tests {
t.join().expect("Failed to join");
}
}

#[test]
fn test_debug_format() {
let cache = SegmentedCache::new(10, 4);
cache.insert('a', "alice");
cache.insert('b', "bob");
cache.insert('c', "cindy");

let debug_str = format!("{:?}", cache);
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': "alice""#));
assert!(debug_str.contains(r#"'b': "bob""#));
assert!(debug_str.contains(r#"'c': "cindy""#));
assert!(debug_str.ends_with('}'));
}
}
34 changes: 34 additions & 0 deletions src/unsync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use smallvec::SmallVec;
use std::{
borrow::Borrow,
collections::{hash_map::RandomState, HashMap},
fmt,
hash::{BuildHasher, Hash, Hasher},
ptr::NonNull,
rc::Rc,
Expand Down Expand Up @@ -178,6 +179,24 @@ pub struct Cache<K, V, S = RandomState> {
expiration_clock: Option<Clock>,
}

impl<K, V, S> fmt::Debug for Cache<K, V, S>
where
K: fmt::Debug + Eq + Hash,
V: fmt::Debug,
// TODO: Remove these bounds from S.
S: BuildHasher + Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d_map = f.debug_map();

for (k, v) in self.iter() {
d_map.entry(&k, &v);
}

d_map.finish()
}
}

impl<K, V> Cache<K, V, RandomState>
where
K: Hash + Eq,
Expand Down Expand Up @@ -1395,4 +1414,19 @@ mod tests {
}
};
}

#[test]
fn test_debug_format() {
let mut cache = Cache::new(10);
cache.insert('a', "alice");
cache.insert('b', "bob");
cache.insert('c', "cindy");

let debug_str = format!("{:?}", cache);
assert!(debug_str.starts_with('{'));
assert!(debug_str.contains(r#"'a': "alice""#));
assert!(debug_str.contains(r#"'b': "bob""#));
assert!(debug_str.contains(r#"'c': "cindy""#));
assert!(debug_str.ends_with('}'));
}
}