Simplify parsing number of threads.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by Thayne McCombs · parent 325d419 · Browse files · 2 files·+10 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+10 added−16 removed
- other:+10 added−16 removed
Provenance
- Commit
1d57b3a- Tree
144e0e9- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
Thayne McCombs <astrothayne@gmail.com>- Parent
325d419
2 files changed+10 added−16 removed
modified:src/cli.rs+9 added-15 removed
496 unmodified lines497 pub color: ColorWhen,498 499 /// Set number of threads to use for searching & executing (default: number500 /// of available CPU cores)501- #[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = clap::value_parser!(u32).range(1..))]502- pub threads: Option<u32>,501+ #[arg(long, short = 'j', value_name = "num", hide_short_help = true, value_parser = str::parse::<NonZeroUsize>)]502+ pub threads: Option<NonZeroUsize>,503 504 /// Milliseconds to buffer before streaming search results to console505 ///506 /// Amount of time in milliseconds to buffer, before streaming the search180 unmodified lines687 pub fn min_depth(&self) -> Option<usize> {688 self.min_depth.or(self.exact_depth)689 }690 691- pub fn threads(&self) -> usize {692- // This will panic if the number of threads passed in is more than usize::MAX in an environment693- // where usize is less than 32 bits (for example 16-bit architectures). It's pretty694- // unlikely fd will be running in such an environment, and even more unlikely someone would695- // be trying to use that many threads on such an environment, so I think panicing is an696- // appropriate way to handle that.697- self.threads.map_or_else(default_num_threads, |n| {698- std::cmp::max(n.try_into().expect("too many threads"), 1)699- })691+ pub fn threads(&self) -> NonZeroUsize {692+ self.threads.unwrap_or_else(default_num_threads)693 }694 695 pub fn max_results(&self) -> Option<usize> {696 self.max_results14 unmodified lines711 }712 }713 714 /// Get the default number of threads to use, if not explicitly specified.722-fn default_num_threads() -> usize {715+fn default_num_threads() -> NonZeroUsize {716 // If we can't get the amount of parallelism for some reason, then717 // default to a single thread, because that is safe.718+ // Note that the minimum value for a NonZeroUsize is 1.719+ // Unfortunately, we can't do `NonZeroUsize::new(1).unwrap()`720+ // in a const context.721 const FALLBACK_PARALLELISM: NonZeroUsize = NonZeroUsize::MIN;726- std::thread::available_parallelism()727- .unwrap_or(FALLBACK_PARALLELISM)728- .get()722+ std::thread::available_parallelism().unwrap_or(FALLBACK_PARALLELISM)723 }724 725 #[derive(Copy, Clone, PartialEq, Eq, ValueEnum)]726 pub enum FileType {156 unmodified linesmodified:src/main.rs+1 added-1 removed
250 unmodified lines251 quiet: opts.quiet,252 max_depth: opts.max_depth(),253 min_depth: opts.min_depth(),254 prune: opts.prune,255- threads: opts.threads(),255+ threads: opts.threads().get(),256 max_buffer_time: opts.max_buffer_time,257 ls_colors,258 interactive_terminal,259 file_types: opts.filetype.as_ref().map(|values| {214 unmodified lines