diff --git a/CHANGELOG.md b/CHANGELOG.md index 6422824..7e3f02c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,15 @@ All notable changes to this project will be documented in this file. ### New features -* Implement `Singleflight` pattern for deduplicating concurrent requests. +* `singleflight::Group` now supports custom hashers for keys. -## [0.6.0] - 2025-01-04 +## [0.6.1] - 2026-01-11 + +### New features + +* Implement `singleflight` pattern for deduplicating concurrent requests. + +## [0.6.0] - 2026-01-04 ### Breaking changes diff --git a/mea/src/singleflight/mod.rs b/mea/src/singleflight/mod.rs index 9c335d2..8d5ab1a 100644 --- a/mea/src/singleflight/mod.rs +++ b/mea/src/singleflight/mod.rs @@ -15,7 +15,9 @@ //! Singleflight provides a duplicate function call suppression mechanism. use std::collections::HashMap; +use std::hash::BuildHasher; use std::hash::Hash; +use std::hash::RandomState; use std::sync::Arc; use crate::internal::Mutex; @@ -27,31 +29,46 @@ mod tests; /// Group represents a class of work and forms a namespace in which /// units of work can be executed with duplicate suppression. #[derive(Debug)] -pub struct Group { - map: Mutex>>>, +pub struct Group { + map: Mutex>, S>>, } -impl Default for Group +impl Default for Group where K: Eq + Hash + Clone, V: Clone, + S: BuildHasher + Clone + Default, { fn default() -> Self { - Self::new() + Self::with_hasher(S::default()) } } -impl Group +impl Group where K: Eq + Hash + Clone, V: Clone, { - /// Creates a new Group. + /// Creates a new Group with the default hasher. pub fn new() -> Self { Self { map: Mutex::new(HashMap::new()), } } +} + +impl Group +where + K: Eq + Hash + Clone, + V: Clone, + S: BuildHasher + Clone, +{ + /// Creates a new Group with the given hasher. + pub fn with_hasher(hasher: S) -> Self { + Self { + map: Mutex::new(HashMap::with_hasher(hasher)), + } + } /// Executes and returns the results of the given function, making sure that only one execution /// is in-flight for a given key at a time.