Skip to content

Commit

Permalink
Avoid restricting impls by RandomState
Browse files Browse the repository at this point in the history
Now the `S` type parameter is only required to implement `Default`.
  • Loading branch information
Pablo Escobar Gaviria authored and jonhoo committed Jan 30, 2020
1 parent dbcb6af commit 17d138f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,32 @@ pub struct HashMap<K, V, S = RandomState> {
build_hasher: S,
}

impl<K, V> Default for HashMap<K, V, RandomState>
impl<K, V, S> Default for HashMap<K, V, S>
where
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher + Default,
{
fn default() -> Self {
Self::new()
}
}

impl<K, V> HashMap<K, V, RandomState>
impl<K, V, S> HashMap<K, V, S>
where
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher + Default,
{
/// Creates a new, empty map with the default initial table size (16).
pub fn new() -> Self {
Self::with_hasher(RandomState::new())
Self::with_hasher(S::default())
}

/// Creates a new, empty map with an initial table size accommodating the specified number of
/// elements without the need to dynamically resize.
pub fn with_capacity(n: usize) -> Self {
Self::with_capacity_and_hasher(RandomState::new(), n)
Self::with_capacity_and_hasher(S::default(), n)
}
}

Expand Down Expand Up @@ -1448,10 +1450,11 @@ where
}
}

impl<K, V> FromIterator<(K, V)> for HashMap<K, V, RandomState>
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher + Default,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut iter = iter.into_iter();
Expand All @@ -1473,21 +1476,23 @@ where
}
}

impl<'a, K, V> FromIterator<(&'a K, &'a V)> for HashMap<K, V, RandomState>
impl<'a, K, V, S> FromIterator<(&'a K, &'a V)> for HashMap<K, V, S>
where
K: Sync + Send + Copy + Hash + Eq,
V: Sync + Send + Copy,
S: BuildHasher + Default,
{
#[inline]
fn from_iter<T: IntoIterator<Item = (&'a K, &'a V)>>(iter: T) -> Self {
Self::from_iter(iter.into_iter().map(|(&k, &v)| (k, v)))
}
}

impl<'a, K, V> FromIterator<&'a (K, V)> for HashMap<K, V, RandomState>
impl<'a, K, V, S> FromIterator<&'a (K, V)> for HashMap<K, V, S>
where
K: Sync + Send + Copy + Hash + Eq,
V: Sync + Send + Copy,
S: BuildHasher + Default,
{
#[inline]
fn from_iter<T: IntoIterator<Item = &'a (K, V)>>(iter: T) -> Self {
Expand Down Expand Up @@ -1549,6 +1554,7 @@ fn capacity() {
// The table has been resized once (and it's capacity doubled),
// since we inserted more elements than it can hold
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion tests/jsr166.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ITER: [(usize, &'static str); 5] = [(1, "A"), (2, "B"), (3, "C"), (4, "D")
fn test_from_iter() {
let guard = unsafe { crossbeam_epoch::unprotected() };
let map1 = from_iter_contron();
let map2 = HashMap::from_iter(ITER.iter());
let map2: HashMap<_, _> = HashMap::from_iter(ITER.iter());

// TODO: improve when `Map: Eq`
let mut fst: Vec<_> = map1.iter(&guard).collect();
Expand Down

0 comments on commit 17d138f

Please sign in to comment.