Skip to content

Commit f9a3ca9

Browse files
authored
Merge pull request #203 from mwillsey/master
Make with_hasher const
2 parents 921df7b + 7e56983 commit f9a3ca9

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/map.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ impl<K, V, S> IndexMap<K, V, S> {
166166
#[inline]
167167
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self {
168168
if n == 0 {
169-
IndexMap {
170-
core: IndexMapCore::new(),
171-
hash_builder,
172-
}
169+
Self::with_hasher(hash_builder)
173170
} else {
174171
IndexMap {
175172
core: IndexMapCore::with_capacity(n),
@@ -178,9 +175,15 @@ impl<K, V, S> IndexMap<K, V, S> {
178175
}
179176
}
180177

181-
/// Create a new map with `hash_builder`
182-
pub fn with_hasher(hash_builder: S) -> Self {
183-
Self::with_capacity_and_hasher(0, hash_builder)
178+
/// Create a new map with `hash_builder`.
179+
///
180+
/// This function is `const`, so it
181+
/// can be called in `static` contexts.
182+
pub const fn with_hasher(hash_builder: S) -> Self {
183+
IndexMap {
184+
core: IndexMapCore::new(),
185+
hash_builder,
186+
}
184187
}
185188

186189
/// Computes in **O(1)** time.

src/map/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<K, V> Entries for IndexMapCore<K, V> {
120120

121121
impl<K, V> IndexMapCore<K, V> {
122122
#[inline]
123-
pub(crate) fn new() -> Self {
123+
pub(crate) const fn new() -> Self {
124124
IndexMapCore {
125125
indices: RawTable::new(),
126126
entries: Vec::new(),

src/set.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ impl<T, S> IndexSet<T, S> {
155155
}
156156
}
157157

158-
/// Create a new set with `hash_builder`
159-
pub fn with_hasher(hash_builder: S) -> Self {
158+
/// Create a new set with `hash_builder`.
159+
///
160+
/// This function is `const`, so it
161+
/// can be called in `static` contexts.
162+
pub const fn with_hasher(hash_builder: S) -> Self {
160163
IndexSet {
161164
map: IndexMap::with_hasher(hash_builder),
162165
}

0 commit comments

Comments
 (0)