Rust at the boundary
A small example of using types to make invalid runtime configuration harder to express.
1 min readRust · Engineering Notes
Note / example: the code below illustrates a boundary; it is not production benchmark data.
Rust is especially useful where configuration turns into a resource.
fn bounded_timeout(seconds: f64) -> Result<std::time::Duration, &'static str> {
if !seconds.is_finite() || seconds <= 0.0 {
return Err("timeout must be positive and finite");
}
std::time::Duration::try_from_secs_f64(seconds).map_err(|_| "timeout is out of range")
}
The fallible conversion keeps the failure close to the transport boundary.