Skip to content

Commit

Permalink
improve coverage on LazyIndexMap
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jan 17, 2024
1 parent 553769d commit 497a5ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lazy_index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ use ahash::AHashMap;
use smallvec::SmallVec;

/// Like [IndexMap](https://docs.rs/indexmap/latest/indexmap/) but only builds the lookup map when it's needed.
#[derive(Default)]
pub struct LazyIndexMap<K, V> {
vec: SmallVec<[(K, V); 8]>,
map: OnceLock<AHashMap<K, usize>>,
last_find: AtomicUsize,
}

impl<K, V> Default for LazyIndexMap<K, V>
where
K: Clone + fmt::Debug + Eq + Hash,
V: fmt::Debug,
{
fn default() -> Self {
Self::new()
}
}

impl<K: Clone, V: Clone> Clone for LazyIndexMap<K, V> {
fn clone(&self) -> Self {
Self {
Expand Down
21 changes: 21 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,27 @@ fn lazy_index_map_big_get() {
assert_eq!(map.get("other"), None);
}

#[test]
fn lazy_index_map_clone() {
let mut map = LazyIndexMap::default();

map.insert("foo".to_string(), JsonValue::Str("bar".to_string()));
map.insert("spam".to_string(), JsonValue::Null);

assert_eq!(map.get("foo"), Some(&JsonValue::Str("bar".to_string())));
assert_eq!(map.get("spam"), Some(&JsonValue::Null));
assert_eq!(map.get("spam"), Some(&JsonValue::Null));
assert_eq!(map.get("foo"), Some(&JsonValue::Str("bar".to_string())));
assert_eq!(map.get("other"), None);

let map2 = map.clone();
assert_eq!(map2.get("foo"), Some(&JsonValue::Str("bar".to_string())));
assert_eq!(map2.get("spam"), Some(&JsonValue::Null));
assert_eq!(map2.get("spam"), Some(&JsonValue::Null));
assert_eq!(map2.get("foo"), Some(&JsonValue::Str("bar".to_string())));
assert_eq!(map2.get("other"), None);
}

#[test]
fn readme_jiter() {
let json_data = r#"
Expand Down

0 comments on commit 497a5ad

Please sign in to comment.