1
Fork 0

Dogfood 'str_split_once()` in the std lib

This commit is contained in:
Eric Arellano 2020-12-07 14:24:05 -07:00
parent 85e9ea0152
commit d2de69da2e
4 changed files with 12 additions and 18 deletions

View file

@ -312,6 +312,7 @@
#![feature(stdsimd)] #![feature(stdsimd)]
#![feature(stmt_expr_attributes)] #![feature(stmt_expr_attributes)]
#![feature(str_internals)] #![feature(str_internals)]
#![feature(str_split_once)]
#![feature(test)] #![feature(test)]
#![feature(thread_local)] #![feature(thread_local)]
#![feature(thread_local_internals)] #![feature(thread_local_internals)]

View file

@ -177,9 +177,7 @@ impl TryFrom<&str> for LookupHost {
} }
// split the string by ':' and convert the second part to u16 // split the string by ':' and convert the second part to u16
let mut parts_iter = s.rsplitn(2, ':'); let (port_str, host) = try_opt!(s.rsplit_once(':'), "invalid socket address");
let port_str = try_opt!(parts_iter.next(), "invalid socket address");
let host = try_opt!(parts_iter.next(), "invalid socket address");
let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value"); let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
(host, port).try_into() (host, port).try_into()

View file

@ -30,6 +30,7 @@
#![feature(termination_trait_lib)] #![feature(termination_trait_lib)]
#![feature(test)] #![feature(test)]
#![feature(total_cmp)] #![feature(total_cmp)]
#![feature(str_split_once)]
// Public reexports // Public reexports
pub use self::bench::{black_box, Bencher}; pub use self::bench::{black_box, Bencher};

View file

@ -105,30 +105,24 @@ impl TimeThreshold {
/// value. /// value.
pub fn from_env_var(env_var_name: &str) -> Option<Self> { pub fn from_env_var(env_var_name: &str) -> Option<Self> {
let durations_str = env::var(env_var_name).ok()?; let durations_str = env::var(env_var_name).ok()?;
let (warn_str, critical_str) = durations_str.split_once(',').unwrap_or_else(|| {
panic!(
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
env_var_name, durations_str
)
});
// Split string into 2 substrings by comma and try to parse numbers. let parse_u64 = |v| {
let mut durations = durations_str.splitn(2, ',').map(|v| {
u64::from_str(v).unwrap_or_else(|_| { u64::from_str(v).unwrap_or_else(|_| {
panic!( panic!(
"Duration value in variable {} is expected to be a number, but got {}", "Duration value in variable {} is expected to be a number, but got {}",
env_var_name, v env_var_name, v
) )
}) })
});
// Callback to be called if the environment variable has unexpected structure.
let panic_on_incorrect_value = || {
panic!(
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
env_var_name, durations_str
);
}; };
let (warn, critical) = ( let warn = parse_u64(warn_str);
durations.next().unwrap_or_else(panic_on_incorrect_value), let critical = parse_u64(critical_str);
durations.next().unwrap_or_else(panic_on_incorrect_value),
);
if warn > critical { if warn > critical {
panic!("Test execution warn time should be less or equal to the critical time"); panic!("Test execution warn time should be less or equal to the critical time");
} }