Skip to content

Commit

Permalink
Make get_executor fallible and fail on invalid value
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Jun 10, 2020
1 parent d4711b6 commit 5fcf13a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
20 changes: 8 additions & 12 deletions src/diskio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use crate::errors::{ErrorKind, Result, ResultExt};
use crate::process;
use crate::utils::notifications::Notification;

Expand Down Expand Up @@ -194,21 +195,16 @@ pub fn create_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// Get the executor for disk IO.
pub fn get_executor<'a>(
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
) -> Box<dyn Executor + 'a> {
) -> Result<Box<dyn Executor + 'a>> {
// If this gets lots of use, consider exposing via the config file.
let thread_count = match process()
.var("RUSTUP_IO_THREADS")
.map(|x| x.parse::<usize>())
{
Ok(Ok(n)) => n,
Ok(Err(_)) => {
eprintln!("Invalid value for RUSTUP_IO_THREADS. Has to be an integer");
num_cpus::get()
}
let thread_count = match process().var("RUSTUP_IO_THREADS") {
Err(_) => num_cpus::get(),
Ok(n) => n
.parse::<usize>()
.chain_err(|| ErrorKind::InvalidIoThreadsCount)?,
};
match thread_count {
0 | 1 => Box::new(immediate::ImmediateUnpacker::new()),
thread_count => Box::new(threaded::Threaded::new(notify_handler, thread_count)),
0 | 1 => Ok(Box::new(immediate::ImmediateUnpacker::new())),
n => Ok(Box::new(threaded::Threaded::new(notify_handler, n))),
}
}
2 changes: 1 addition & 1 deletion src/dist/component/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn unpack_without_first_dir<'a, R: Read>(
path: &Path,
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
) -> Result<()> {
let mut io_executor: Box<dyn Executor> = get_executor(notify_handler);
let mut io_executor: Box<dyn Executor> = get_executor(notify_handler)?;
let entries = archive
.entries()
.chain_err(|| ErrorKind::ExtractingPackage)?;
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ error_chain! {
BrokenPartialFile {
description("partially downloaded file may have been damaged and was removed, please try again")
}
InvalidIoThreadsCount {
description("invalid value in RUSTUP_IO_THREADS. Must be a natural number")
}
}
}

Expand Down

0 comments on commit 5fcf13a

Please sign in to comment.