Skip to content

Commit d2de69d

Browse files
committed
Dogfood 'str_split_once()` in the std lib
1 parent 85e9ea0 commit d2de69d

File tree

4 files changed

+12
-18
lines changed

4 files changed

+12
-18
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@
312312
#![feature(stdsimd)]
313313
#![feature(stmt_expr_attributes)]
314314
#![feature(str_internals)]
315+
#![feature(str_split_once)]
315316
#![feature(test)]
316317
#![feature(thread_local)]
317318
#![feature(thread_local_internals)]

library/std/src/sys_common/net.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ impl TryFrom<&str> for LookupHost {
177177
}
178178

179179
// split the string by ':' and convert the second part to u16
180-
let mut parts_iter = s.rsplitn(2, ':');
181-
let port_str = try_opt!(parts_iter.next(), "invalid socket address");
182-
let host = try_opt!(parts_iter.next(), "invalid socket address");
180+
let (port_str, host) = try_opt!(s.rsplit_once(':'), "invalid socket address");
183181
let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
184182

185183
(host, port).try_into()

library/test/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(termination_trait_lib)]
3131
#![feature(test)]
3232
#![feature(total_cmp)]
33+
#![feature(str_split_once)]
3334

3435
// Public reexports
3536
pub use self::bench::{black_box, Bencher};

library/test/src/time.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,24 @@ impl TimeThreshold {
105105
/// value.
106106
pub fn from_env_var(env_var_name: &str) -> Option<Self> {
107107
let durations_str = env::var(env_var_name).ok()?;
108+
let (warn_str, critical_str) = durations_str.split_once(',').unwrap_or_else(|| {
109+
panic!(
110+
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
111+
env_var_name, durations_str
112+
)
113+
});
108114

109-
// Split string into 2 substrings by comma and try to parse numbers.
110-
let mut durations = durations_str.splitn(2, ',').map(|v| {
115+
let parse_u64 = |v| {
111116
u64::from_str(v).unwrap_or_else(|_| {
112117
panic!(
113118
"Duration value in variable {} is expected to be a number, but got {}",
114119
env_var_name, v
115120
)
116121
})
117-
});
118-
119-
// Callback to be called if the environment variable has unexpected structure.
120-
let panic_on_incorrect_value = || {
121-
panic!(
122-
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
123-
env_var_name, durations_str
124-
);
125122
};
126123

127-
let (warn, critical) = (
128-
durations.next().unwrap_or_else(panic_on_incorrect_value),
129-
durations.next().unwrap_or_else(panic_on_incorrect_value),
130-
);
131-
124+
let warn = parse_u64(warn_str);
125+
let critical = parse_u64(critical_str);
132126
if warn > critical {
133127
panic!("Test execution warn time should be less or equal to the critical time");
134128
}

0 commit comments

Comments
 (0)