-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy paththreads.rs
More file actions
42 lines (38 loc) · 1.02 KB
/
threads.rs
File metadata and controls
42 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use derive_more::{Display, Error};
use std::{
num::{NonZeroUsize, ParseIntError},
str::FromStr,
};
const AUTO: &str = "auto";
const MAX: &str = "max";
/// Number of rayon threads.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Display)]
pub enum Threads {
#[default]
#[display("{AUTO}")]
Auto,
#[display("{MAX}")]
Max,
Fixed(NonZeroUsize),
}
/// Error that occurs when parsing a string as [`Threads`].
#[derive(Debug, Display, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum FromStrError {
#[display("Value is neither {AUTO:?}, {MAX:?}, nor a number: {_0}")]
InvalidSyntax(ParseIntError),
}
impl FromStr for Threads {
type Err = FromStrError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let text = text.trim();
match text {
AUTO => return Ok(Threads::Auto),
MAX => return Ok(Threads::Max),
_ => {}
};
text.parse()
.map_err(FromStrError::InvalidSyntax)
.map(Threads::Fixed)
}
}