Skip to content

Commit f169c25

Browse files
committed
Auto merge of #298 - 4LT:master, r=Amanieu
Issue #297: Implement From<array> on HashSet and HashMap Implemented From<[T; N]> on HashSet and From<[(K, V); N]> on HashMap. In both cases, the default hasher was used to match the implementations from the std crate (see https://doc.rust-lang.org/src/std/collections/hash/map.rs.html#1161-1190) Edition was updated to 2021 to take advantage of the change in array's `.into_iter` method from iterating over `&T` to iterating over `T`. Hopefully the implementations are trivial enough that tests aren't necessary ;)
2 parents 612057b + ba8b56c commit f169c25

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

.github/workflows/rust.yml

Lines changed: 3 additions & 3 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.49.0, nightly]
53+
channel: [1.56.1, nightly]
5454
include:
5555
- os: macos-latest
5656
target: x86_64-apple-darwin
@@ -60,10 +60,10 @@ jobs:
6060
channel: nightly
6161
- os: macos-latest
6262
target: x86_64-apple-darwin
63-
channel: 1.49.0
63+
channel: 1.56.1
6464
- os: windows-latest
6565
target: x86_64-pc-windows-msvc
66-
channel: 1.49.0
66+
channel: 1.56.1
6767
- os: ubuntu-latest
6868
target: x86_64-unknown-linux-gnu
6969
channel: beta

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10+
- Bumped minimum Rust version to 1.65.1 and edition to 2021
11+
- Added `From<[T; N]>` and `From<[(K, V); N]>` for `HashSet` and `HashMap` respectively (#297)
12+
1013
## [v0.11.2] - 2021-03-25
1114

1215
## Fixed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ readme = "README.md"
99
keywords = ["hash", "no_std", "hashmap", "swisstable"]
1010
categories = ["data-structures", "no-std"]
1111
exclude = [".github", "bors.toml", "/ci/*"]
12-
edition = "2018"
12+
edition = "2021"
1313

1414
[dependencies]
1515
# For the default hasher

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.49.0%2B-blue.svg?maxAge=3600)](https://github.com/rust-lang/hashbrown)
7+
[![Rust](https://img.shields.io/badge/rust-1.56.1%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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,27 @@ where
15801580
}
15811581
}
15821582

1583+
// The default hasher is used to match the std implementation signature
1584+
#[cfg(feature = "ahash")]
1585+
impl<K, V, A, const N: usize> From<[(K, V); N]> for HashMap<K, V, DefaultHashBuilder, A>
1586+
where
1587+
K: Eq + Hash,
1588+
A: Default + Allocator + Clone,
1589+
{
1590+
/// # Examples
1591+
///
1592+
/// ```
1593+
/// use hashbrown::HashMap;
1594+
///
1595+
/// let map1 = HashMap::from([(1, 2), (3, 4)]);
1596+
/// let map2: HashMap<_, _> = [(1, 2), (3, 4)].into();
1597+
/// assert_eq!(map1, map2);
1598+
/// ```
1599+
fn from(arr: [(K, V); N]) -> Self {
1600+
arr.into_iter().collect()
1601+
}
1602+
}
1603+
15831604
/// An iterator over the entries of a `HashMap`.
15841605
///
15851606
/// This `struct` is created by the [`iter`] method on [`HashMap`]. See its

src/set.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,27 @@ where
11591159
}
11601160
}
11611161

1162+
// The default hasher is used to match the std implementation signature
1163+
#[cfg(feature = "ahash")]
1164+
impl<T, A, const N: usize> From<[T; N]> for HashSet<T, DefaultHashBuilder, A>
1165+
where
1166+
T: Eq + Hash,
1167+
A: Default + Allocator + Clone,
1168+
{
1169+
/// # Examples
1170+
///
1171+
/// ```
1172+
/// use hashbrown::HashSet;
1173+
///
1174+
/// let set1 = HashSet::from([1, 2, 3, 4]);
1175+
/// let set2: HashSet<_> = [1, 2, 3, 4].into();
1176+
/// assert_eq!(set1, set2);
1177+
/// ```
1178+
fn from(arr: [T; N]) -> Self {
1179+
arr.into_iter().collect()
1180+
}
1181+
}
1182+
11621183
impl<T, S, A> Extend<T> for HashSet<T, S, A>
11631184
where
11641185
T: Eq + Hash,

0 commit comments

Comments
 (0)