Skip to content

Commit e674100

Browse files
committed
Auto merge of #355 - TennyZhuang:make-with-hasher-in-const, r=Amanieu
make with_hasher_in const (consistent with with_hasher) I found that the following four methods have additional comments about their allocation: * HashMap::with_hasher * HashMap::with_hasher_in * HashSet::with_hasher * HashSet::with_hasher_in There is no reason to make such a difference; in this PR, I make them consistent as the following behavior: * All methods clarify that they will not allocate memory before the first insertion * All methods are marked as const function. We also bump the MSRV to 1.61.0 for the const_fn_trait_bound feature. Currently, 1.64.0 has been released, and I guess it's acceptable to break the 1.61.0 users. Signed-off-by: TennyZhuang <[email protected]>
2 parents c0247fa + 92618d7 commit e674100

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

.github/workflows/rust.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
thumbv6m-none-eabi,
5151
x86_64-pc-windows-gnu,
5252
]
53-
channel: [1.58.1, nightly]
53+
channel: [1.61.0, nightly]
5454
include:
5555
- os: macos-latest
5656
target: x86_64-apple-darwin
@@ -60,13 +60,13 @@ jobs:
6060
channel: nightly
6161
- os: macos-latest
6262
target: x86_64-apple-darwin
63-
channel: 1.58.1
63+
channel: 1.61.0
6464
- os: windows-latest
6565
target: x86_64-pc-windows-msvc
66-
channel: 1.58.1
66+
channel: 1.61.0
6767
- os: ubuntu-latest
6868
target: x86_64-unknown-linux-gnu
69-
channel: 1.56.1
69+
channel: 1.61.0
7070
- os: ubuntu-latest
7171
target: x86_64-unknown-linux-gnu
7272
channel: beta

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ hashbrown
44
[![Build Status](https://github.com/rust-lang/hashbrown/actions/workflows/rust.yml/badge.svg)](https://github.com/rust-lang/hashbrown/actions)
55
[![Crates.io](https://img.shields.io/crates/v/hashbrown.svg)](https://crates.io/crates/hashbrown)
66
[![Documentation](https://docs.rs/hashbrown/badge.svg)](https://docs.rs/hashbrown)
7-
[![Rust](https://img.shields.io/badge/rust-1.56.1%2B-blue.svg?maxAge=3600)](https://github.com/rust-lang/hashbrown)
7+
[![Rust](https://img.shields.io/badge/rust-1.61.0%2B-blue.svg?maxAge=3600)](https://github.com/rust-lang/hashbrown)
88

99
This crate is a Rust port of Google's high-performance [SwissTable] hash
1010
map, adapted to make it a drop-in replacement for Rust's standard `HashMap`

src/map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
429429
/// Creates an empty `HashMap` which will use the given hash builder to hash
430430
/// keys. It will be allocated with the given allocator.
431431
///
432-
/// The created map has the default initial capacity.
432+
/// The hash map is initially created with a capacity of 0, so it will not
433+
/// allocate until it is first inserted into.
433434
///
434435
/// Warning: `hash_builder` is normally randomly generated, and
435436
/// is designed to allow HashMaps to be resistant to attacks that
@@ -447,7 +448,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
447448
/// map.insert(1, 2);
448449
/// ```
449450
#[cfg_attr(feature = "inline-more", inline)]
450-
pub fn with_hasher_in(hash_builder: S, alloc: A) -> Self {
451+
pub const fn with_hasher_in(hash_builder: S, alloc: A) -> Self {
451452
Self {
452453
hash_builder,
453454
table: RawTable::new_in(alloc),

src/raw/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
426426
/// leave the data pointer dangling since that bucket is never written to
427427
/// due to our load factor forcing us to always have at least 1 free bucket.
428428
#[inline]
429-
pub fn new_in(alloc: A) -> Self {
429+
pub const fn new_in(alloc: A) -> Self {
430430
Self {
431431
table: RawTableInner::new_in(alloc),
432432
marker: PhantomData,

src/set.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ impl<T, S> HashSet<T, S, Global> {
387387
/// Creates a new empty hash set which will use the given hasher to hash
388388
/// keys.
389389
///
390-
/// The hash set is also created with the default initial capacity.
390+
/// The hash set is initially created with a capacity of 0, so it will not
391+
/// allocate until it is first inserted into.
391392
///
392393
/// Warning: `hasher` is normally randomly generated, and
393394
/// is designed to allow `HashSet`s to be resistant to attacks that
@@ -464,7 +465,8 @@ where
464465
/// Creates a new empty hash set which will use the given hasher to hash
465466
/// keys.
466467
///
467-
/// The hash set is also created with the default initial capacity.
468+
/// The hash set is initially created with a capacity of 0, so it will not
469+
/// allocate until it is first inserted into.
468470
///
469471
/// Warning: `hasher` is normally randomly generated, and
470472
/// is designed to allow `HashSet`s to be resistant to attacks that
@@ -482,7 +484,7 @@ where
482484
/// set.insert(2);
483485
/// ```
484486
#[cfg_attr(feature = "inline-more", inline)]
485-
pub fn with_hasher_in(hasher: S, alloc: A) -> Self {
487+
pub const fn with_hasher_in(hasher: S, alloc: A) -> Self {
486488
Self {
487489
map: HashMap::with_hasher_in(hasher, alloc),
488490
}

0 commit comments

Comments
 (0)