Skip to main content

std/sys/helpers/
mod.rs

1//! Small helper functions used inside `sys`.
2//!
3//! If any of these have uses outside of `sys`, please move them to a different
4//! module.
5
6#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
7mod small_c_string;
8#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
9mod wstr;
10
11#[cfg(test)]
12mod tests;
13
14#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
15pub use small_c_string::{run_path_with_cstr, run_with_cstr};
16#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
17pub use wstr::WStrUnits;
18
19/// Computes `(value*numerator)/denom` without overflow, as long as both
20/// `numerator*denom` and the overall result fit into `u64` (which is the case
21/// for our time conversions).
22#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
23pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
24    let q = value / denom;
25    let r = value % denom;
26    // Decompose value as (value/denom*denom + value%denom),
27    // substitute into (value*numerator)/denom and simplify.
28    // r < denom, so (denom*numerator) is the upper bound of (r*numerator)
29    q * numerator + r * numerator / denom
30}
31
32#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
33pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
34    match result {
35        Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
36        Ok(_) => Ok(()),
37        Err(err) => Err(err),
38    }
39}