mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-18 06:58:10 -05:00
Use the thread builder and handle the spawn error
This commit is contained in:
parent
9faa5d3aa4
commit
2d26358602
4 changed files with 34 additions and 13 deletions
|
@ -10,4 +10,7 @@ disallowed-methods = [
|
||||||
"std::collections::HashSet::with_capacity",
|
"std::collections::HashSet::with_capacity",
|
||||||
# Inefficient. Use `.queue(…)` instead.
|
# Inefficient. Use `.queue(…)` instead.
|
||||||
"crossterm::style::style",
|
"crossterm::style::style",
|
||||||
|
# Use `thread::Builder::spawn` instead and handle the error.
|
||||||
|
"std::thread::spawn",
|
||||||
|
"std::thread::Scope::spawn",
|
||||||
]
|
]
|
||||||
|
|
|
@ -388,13 +388,20 @@ impl AppState {
|
||||||
let handles = self
|
let handles = self
|
||||||
.exercises
|
.exercises
|
||||||
.iter()
|
.iter()
|
||||||
.map(|exercise| s.spawn(|| exercise.run_exercise(None, &self.cmd_runner)))
|
.map(|exercise| {
|
||||||
|
thread::Builder::new()
|
||||||
|
.spawn_scoped(s, || exercise.run_exercise(None, &self.cmd_runner))
|
||||||
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
for (exercise_ind, handle) in handles.into_iter().enumerate() {
|
for (exercise_ind, spawn_res) in handles.into_iter().enumerate() {
|
||||||
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
write!(stdout, "\rProgress: {exercise_ind}/{n_exercises}")?;
|
||||||
stdout.flush()?;
|
stdout.flush()?;
|
||||||
|
|
||||||
|
let Ok(handle) = spawn_res else {
|
||||||
|
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
|
||||||
|
};
|
||||||
|
|
||||||
let Ok(success) = handle.join().unwrap() else {
|
let Ok(success) = handle.join().unwrap() else {
|
||||||
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
|
return Ok(AllExercisesCheck::CheckedUntil(exercise_ind));
|
||||||
};
|
};
|
||||||
|
|
|
@ -185,12 +185,14 @@ fn check_exercises_unsolved(
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some((
|
Some(
|
||||||
exercise_info.name.as_str(),
|
thread::Builder::new()
|
||||||
thread::spawn(|| exercise_info.run_exercise(None, cmd_runner)),
|
.spawn(|| exercise_info.run_exercise(None, cmd_runner))
|
||||||
))
|
.map(|handle| (exercise_info.name.as_str(), handle)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Result<Vec<_>, _>>()
|
||||||
|
.context("Failed to spawn a thread to check if an exercise is already solved")?;
|
||||||
|
|
||||||
let n_handles = handles.len();
|
let n_handles = handles.len();
|
||||||
write!(stdout, "Progress: 0/{n_handles}")?;
|
write!(stdout, "Progress: 0/{n_handles}")?;
|
||||||
|
@ -226,7 +228,9 @@ fn check_exercises(info_file: &'static InfoFile, cmd_runner: &'static CmdRunner)
|
||||||
Ordering::Equal => (),
|
Ordering::Equal => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
let handle = thread::spawn(move || check_exercises_unsolved(info_file, cmd_runner));
|
let handle = thread::Builder::new()
|
||||||
|
.spawn(move || check_exercises_unsolved(info_file, cmd_runner))
|
||||||
|
.context("Failed to spawn a thread to check if any exercise is already solved")?;
|
||||||
|
|
||||||
let info_file_paths = check_info_file_exercises(info_file)?;
|
let info_file_paths = check_info_file_exercises(info_file)?;
|
||||||
check_unexpected_files("exercises", &info_file_paths)?;
|
check_unexpected_files("exercises", &info_file_paths)?;
|
||||||
|
@ -253,7 +257,7 @@ fn check_solutions(
|
||||||
.exercises
|
.exercises
|
||||||
.iter()
|
.iter()
|
||||||
.map(|exercise_info| {
|
.map(|exercise_info| {
|
||||||
thread::spawn(move || {
|
thread::Builder::new().spawn(move || {
|
||||||
let sol_path = exercise_info.sol_path();
|
let sol_path = exercise_info.sol_path();
|
||||||
if !Path::new(&sol_path).exists() {
|
if !Path::new(&sol_path).exists() {
|
||||||
if require_solutions {
|
if require_solutions {
|
||||||
|
@ -274,7 +278,8 @@ fn check_solutions(
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Result<Vec<_>, _>>()
|
||||||
|
.context("Failed to spawn a thread to check a solution")?;
|
||||||
|
|
||||||
let mut sol_paths = hash_set_with_capacity(info_file.exercises.len());
|
let mut sol_paths = hash_set_with_capacity(info_file.exercises.len());
|
||||||
let mut fmt_cmd = Command::new("rustfmt");
|
let mut fmt_cmd = Command::new("rustfmt");
|
||||||
|
@ -322,7 +327,11 @@ fn check_solutions(
|
||||||
}
|
}
|
||||||
stdout.write_all(b"\n")?;
|
stdout.write_all(b"\n")?;
|
||||||
|
|
||||||
let handle = thread::spawn(move || check_unexpected_files("solutions", &sol_paths));
|
let handle = thread::Builder::new()
|
||||||
|
.spawn(move || check_unexpected_files("solutions", &sol_paths))
|
||||||
|
.context(
|
||||||
|
"Failed to spawn a thread to check for unexpected files in the solutions directory",
|
||||||
|
)?;
|
||||||
|
|
||||||
if !fmt_cmd
|
if !fmt_cmd
|
||||||
.status()
|
.status()
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Context, Error, Result};
|
||||||
use notify_debouncer_mini::{
|
use notify_debouncer_mini::{
|
||||||
new_debouncer,
|
new_debouncer,
|
||||||
notify::{self, RecursiveMode},
|
notify::{self, RecursiveMode},
|
||||||
|
@ -77,7 +77,9 @@ fn run_watch(
|
||||||
let mut stdout = io::stdout().lock();
|
let mut stdout = io::stdout().lock();
|
||||||
watch_state.run_current_exercise(&mut stdout)?;
|
watch_state.run_current_exercise(&mut stdout)?;
|
||||||
|
|
||||||
thread::spawn(move || terminal_event_handler(tx, manual_run));
|
thread::Builder::new()
|
||||||
|
.spawn(move || terminal_event_handler(tx, manual_run))
|
||||||
|
.context("Failed to spawn a thread to handle terminal events")?;
|
||||||
|
|
||||||
while let Ok(event) = rx.recv() {
|
while let Ok(event) = rx.recv() {
|
||||||
match event {
|
match event {
|
||||||
|
|
Loading…
Reference in a new issue