Skip to content

Commit e41c3a7

Browse files
committed
Use fixed seeds with ahash
1 parent 1b9faa4 commit e41c3a7

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

Cargo.lock

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ include = [
4646
]
4747

4848
[dependencies]
49-
ahash = "0.8.11"
49+
ahash = { version = "0.8.11", default-features = false }
5050
anyhow = "1.0.86"
5151
clap = { version = "4.5.13", features = ["derive"] }
5252
notify-debouncer-mini = { version = "0.4.1", default-features = false }

src/app_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use ahash::{HashSet, HashSetExt};
21
use anyhow::{bail, Context, Error, Result};
32
use std::{
43
fs::{self, File},
@@ -11,6 +10,7 @@ use std::{
1110
use crate::{
1211
clear_terminal,
1312
cmd::CmdRunner,
13+
collections::hash_set_with_capacity,
1414
embedded::EMBEDDED_FILES,
1515
exercise::{Exercise, RunnableExercise},
1616
info_file::ExerciseInfo,
@@ -70,7 +70,7 @@ impl AppState {
7070
return StateFileStatus::NotRead;
7171
}
7272

73-
let mut done_exercises = HashSet::with_capacity(self.exercises.len());
73+
let mut done_exercises = hash_set_with_capacity(self.exercises.len());
7474

7575
for done_exerise_name in lines {
7676
if done_exerise_name.is_empty() {

src/collections.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use ahash::AHasher;
2+
use std::hash::BuildHasherDefault;
3+
4+
/// DOS attacks aren't a concern for Rustlings. Therefore, we use `ahash` with fixed seeds.
5+
pub type HashSet<T> = std::collections::HashSet<T, BuildHasherDefault<AHasher>>;
6+
7+
#[inline]
8+
pub fn hash_set_with_capacity<T>(capacity: usize) -> HashSet<T> {
9+
HashSet::with_capacity_and_hasher(capacity, BuildHasherDefault::<AHasher>::default())
10+
}

src/dev/check.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use ahash::{HashSet, HashSetExt};
21
use anyhow::{anyhow, bail, Context, Error, Result};
32
use std::{
43
cmp::Ordering,
@@ -12,6 +11,7 @@ use std::{
1211
use crate::{
1312
cargo_toml::{append_bins, bins_start_end_ind, BINS_BUFFER_CAPACITY},
1413
cmd::CmdRunner,
14+
collections::{hash_set_with_capacity, HashSet},
1515
exercise::{RunnableExercise, OUTPUT_CAPACITY},
1616
info_file::{ExerciseInfo, InfoFile},
1717
CURRENT_FORMAT_VERSION,
@@ -50,8 +50,8 @@ fn check_cargo_toml(
5050

5151
// Check the info of all exercises and return their paths in a set.
5252
fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
53-
let mut names = HashSet::with_capacity(info_file.exercises.len());
54-
let mut paths = HashSet::with_capacity(info_file.exercises.len());
53+
let mut names = hash_set_with_capacity(info_file.exercises.len());
54+
let mut paths = hash_set_with_capacity(info_file.exercises.len());
5555

5656
let mut file_buf = String::with_capacity(1 << 14);
5757
for exercise_info in &info_file.exercises {
@@ -251,7 +251,7 @@ fn check_solutions(
251251
})
252252
.collect::<Vec<_>>();
253253

254-
let mut sol_paths = HashSet::with_capacity(info_file.exercises.len());
254+
let mut sol_paths = hash_set_with_capacity(info_file.exercises.len());
255255
let mut fmt_cmd = Command::new("rustfmt");
256256
fmt_cmd
257257
.arg("--check")

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::Wa
1313
mod app_state;
1414
mod cargo_toml;
1515
mod cmd;
16+
mod collections;
1617
mod dev;
1718
mod embedded;
1819
mod exercise;

0 commit comments

Comments
 (0)