Merge pull request #203 from mwillsey/master

Make with_hasher const
This commit is contained in:
Josh Stone
2022-01-06 10:27:26 -08:00
committed by GitHub
3 changed files with 16 additions and 10 deletions
+10 -7
View File
@@ -166,10 +166,7 @@ impl<K, V, S> IndexMap<K, V, S> {
#[inline]
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self {
if n == 0 {
IndexMap {
core: IndexMapCore::new(),
hash_builder,
}
Self::with_hasher(hash_builder)
} else {
IndexMap {
core: IndexMapCore::with_capacity(n),
@@ -178,9 +175,15 @@ impl<K, V, S> IndexMap<K, V, S> {
}
}
/// Create a new map with `hash_builder`
pub fn with_hasher(hash_builder: S) -> Self {
Self::with_capacity_and_hasher(0, hash_builder)
/// Create a new map with `hash_builder`.
///
/// This function is `const`, so it
/// can be called in `static` contexts.
pub const fn with_hasher(hash_builder: S) -> Self {
IndexMap {
core: IndexMapCore::new(),
hash_builder,
}
}
/// Computes in **O(1)** time.
+1 -1
View File
@@ -120,7 +120,7 @@ impl<K, V> Entries for IndexMapCore<K, V> {
impl<K, V> IndexMapCore<K, V> {
#[inline]
pub(crate) fn new() -> Self {
pub(crate) const fn new() -> Self {
IndexMapCore {
indices: RawTable::new(),
entries: Vec::new(),
+5 -2
View File
@@ -155,8 +155,11 @@ impl<T, S> IndexSet<T, S> {
}
}
/// Create a new set with `hash_builder`
pub fn with_hasher(hash_builder: S) -> Self {
/// Create a new set with `hash_builder`.
///
/// This function is `const`, so it
/// can be called in `static` contexts.
pub const fn with_hasher(hash_builder: S) -> Self {
IndexSet {
map: IndexMap::with_hasher(hash_builder),
}