Skip to content

Commit

Permalink
Add a few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Aug 28, 2024
1 parent 37dc475 commit 9110a39
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,16 @@ where
}
}

/// A map where values are automatically filled at access.
///
/// This type can be shared across threads.
///
/// ```
/// let map = once_map::LazyMap::new(|x: &i32| x.to_string());
///
/// assert_eq!(&map[&3], "3");
/// assert_eq!(map.get(&-67), "-67");
/// ```
pub struct LazyMap<K, V, S = crate::RandomState, F = fn(&K) -> V> {
map: OnceMap<K, V, S>,
init: F,
Expand All @@ -941,6 +951,7 @@ impl<K, V, S, F> LazyMap<K, V, S, F> {
}
}

/// Removes all entries from the map.
pub fn clear(&mut self) {
self.map.clear();
}
Expand Down
12 changes: 12 additions & 0 deletions src/unsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,17 @@ where
}
}

/// A map where values are automatically filled at access.
///
/// This type has less overhead than [`crate::sync::LazyMap`] but it cannot be
/// shared across threads.
///
/// ```
/// let map = once_map::unsync::LazyMap::new(|x: &i32| x.to_string());
///
/// assert_eq!(&map[&3], "3");
/// assert_eq!(map.get(&-67), "-67");
/// ```
pub struct LazyMap<K, V, S = crate::RandomState, F = fn(&K) -> V> {
map: OnceMap<K, V, S>,
init: F,
Expand All @@ -505,6 +516,7 @@ impl<K, V, S, F> LazyMap<K, V, S, F> {
}
}

/// Removes all entries from the map.
pub fn clear(&mut self) {
self.map.clear();
}
Expand Down

0 comments on commit 9110a39

Please sign in to comment.