Skip to content

Commit 2d26358

Browse files
committed
Use the thread builder and handle the spawn error
1 parent 9faa5d3 commit 2d26358

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

clippy.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ disallowed-methods = [
1010
"std::collections::HashSet::with_capacity",
1111
# Inefficient. Use `.queue(…)` instead.
1212
"crossterm::style::style",
13+
# Use `thread::Builder::spawn` instead and handle the error.
14+
"std::thread::spawn",
15+
"std::thread::Scope::spawn",
1316
]

src/app_state.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,20 @@ impl AppState {
388388
let handles = self
389389
.exercises
390390
.iter()
391-
.map(|exercise| s.spawn(|| exercise.run_exercise(None, &self.cmd_runner)))
391+
.map(|exercise| {
392+
thread::Builder::new()
393+
.spawn_scoped(s, || exercise.run_exercise(None, &self.cmd_runner))
394+
})
392395
.collect::<Vec<_>>();
393396

394-
for (exercise_ind, handle) in handles.into_iter().enumerate() {
397+
for (exercise_ind, spawn_res) in handles.into_iter().enumerate() {
395398
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
396399
stdout.flush()?;
397400

401+
let Ok(handle) = spawn_res else {
402+
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
403+
};
404+
398405
let Ok(success) = handle.join().unwrap() else {
399406
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
400407
};

src/dev/check.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ fn check_exercises_unsolved(
185185
return None;
186186
}
187187

188-
Some((
189-
exercise_info.name.as_str(),
190-
thread::spawn(|| exercise_info.run_exercise(None, cmd_runner)),
191-
))
188+
Some(
189+
thread::Builder::new()
190+
.spawn(|| exercise_info.run_exercise(None, cmd_runner))
191+
.map(|handle| (exercise_info.name.as_str(), handle)),
192+
)
192193
})
193-
.collect::<Vec<_>>();
194+
.collect::<Result<Vec<_>, _>>()
195+
.context("Failed to spawn a thread to check if an exercise is already solved")?;
194196

195197
let n_handles = handles.len();
196198
write!(stdout, "Progress: 0/{n_handles}")?;
@@ -226,7 +228,9 @@ fn check_exercises(info_file: &'static InfoFile, cmd_runner: &'static CmdRunner)
226228
Ordering::Equal => (),
227229
}
228230

229-
let handle = thread::spawn(move || check_exercises_unsolved(info_file, cmd_runner));
231+
let handle = thread::Builder::new()
232+
.spawn(move || check_exercises_unsolved(info_file, cmd_runner))
233+
.context("Failed to spawn a thread to check if any exercise is already solved")?;
230234

231235
let info_file_paths = check_info_file_exercises(info_file)?;
232236
check_unexpected_files("exercises", &info_file_paths)?;
@@ -253,7 +257,7 @@ fn check_solutions(
253257
.exercises
254258
.iter()
255259
.map(|exercise_info| {
256-
thread::spawn(move || {
260+
thread::Builder::new().spawn(move || {
257261
let sol_path = exercise_info.sol_path();
258262
if !Path::new(&sol_path).exists() {
259263
if require_solutions {
@@ -274,7 +278,8 @@ fn check_solutions(
274278
}
275279
})
276280
})
277-
.collect::<Vec<_>>();
281+
.collect::<Result<Vec<_>, _>>()
282+
.context("Failed to spawn a thread to check a solution")?;
278283

279284
let mut sol_paths = hash_set_with_capacity(info_file.exercises.len());
280285
let mut fmt_cmd = Command::new("rustfmt");
@@ -322,7 +327,11 @@ fn check_solutions(
322327
}
323328
stdout.write_all(b"\n")?;
324329

325-
let handle = thread::spawn(move || check_unexpected_files("solutions", &sol_paths));
330+
let handle = thread::Builder::new()
331+
.spawn(move || check_unexpected_files("solutions", &sol_paths))
332+
.context(
333+
"Failed to spawn a thread to check for unexpected files in the solutions directory",
334+
)?;
326335

327336
if !fmt_cmd
328337
.status()

src/watch.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Error, Result};
1+
use anyhow::{Context, Error, Result};
22
use notify_debouncer_mini::{
33
new_debouncer,
44
notify::{self, RecursiveMode},
@@ -77,7 +77,9 @@ fn run_watch(
7777
let mut stdout = io::stdout().lock();
7878
watch_state.run_current_exercise(&mut stdout)?;
7979

80-
thread::spawn(move || terminal_event_handler(tx, manual_run));
80+
thread::Builder::new()
81+
.spawn(move || terminal_event_handler(tx, manual_run))
82+
.context("Failed to spawn a thread to handle terminal events")?;
8183

8284
while let Ok(event) = rx.recv() {
8385
match event {

0 commit comments

Comments
 (0)