summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThayne McCombs <astrothayne@gmail.com>2023-10-25 23:56:59 -0600
committerThayne McCombs <astrothayne@gmail.com>2023-10-25 23:56:59 -0600
commit1d57b3a06453d2602fc90e019a30c1ed9b792748 (patch)
tree144e0e986e91a28615cac706a1b02221de625703 /src
parent325d419e39733a8511471438ef021a8b4116c12f (diff)
Simplify parsing number of threads.
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs24
-rw-r--r--src/main.rs2
2 files changed, 10 insertions, 16 deletions
diff --git a/src/cli.rs b/src/cli.rs
index a8cfb3d..abb5598 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -498,8 +498,8 @@ pub struct Opts {
/// Set number of threads to use for searching & executing (default: number
/// of available CPU cores)
- #[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = clap::value_parser!(u32).range(1..))]
- pub threads: Option<u32>,
+ #[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = str::parse::<NonZeroUsize>)]
+ pub threads: Option<NonZeroUsize>,
/// Milliseconds to buffer before streaming search results to console
///
@@ -688,15 +688,8 @@ impl Opts {
self.min_depth.or(self.exact_depth)
}
- pub fn threads(&self) -> usize {
- // This will panic if the number of threads passed in is more than usize::MAX in an environment
- // where usize is less than 32 bits (for example 16-bit architectures). It's pretty
- // unlikely fd will be running in such an environment, and even more unlikely someone would
- // be trying to use that many threads on such an environment, so I think panicing is an
- // appropriate way to handle that.
- self.threads.map_or_else(default_num_threads, |n| {
- std::cmp::max(n.try_into().expect("too many threads"), 1)
- })
+ pub fn threads(&self) -> NonZeroUsize {
+ self.threads.unwrap_or_else(default_num_threads)
}
pub fn max_results(&self) -> Option<usize> {
@@ -719,13 +712,14 @@ impl Opts {
}
/// Get the default number of threads to use, if not explicitly specified.
-fn default_num_threads() -> usize {
+fn default_num_threads() -> NonZeroUsize {
// If we can't get the amount of parallelism for some reason, then
// default to a single thread, because that is safe.
+ // Note that the minimum value for a NonZeroUsize is 1.
+ // Unfortunately, we can't do `NonZeroUsize::new(1).unwrap()`
+ // in a const context.
const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;
- std::thread::available_parallelism()
- .unwrap_or(FALLBACK_PARALLELISM)
- .get()
+ std::thread::available_parallelism().unwrap_or(FALLBACK_PARALLELISM)
}
#[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]
diff --git a/src/main.rs b/src/main.rs
index a1bad2b..b661f67 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -252,7 +252,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
max_depth: opts.max_depth(),
min_depth: opts.min_depth(),
prune: opts.prune,
- threads: opts.threads(),
+ threads: opts.threads().get(),
max_buffer_time: opts.max_buffer_time,
ls_colors,
interactive_terminal,