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 = "netbsd"), allow(unused))] // Not used on all platforms.
7mod c_opaque;
8#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
9mod small_c_string;
10#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
11mod wstr;
12
13#[cfg(test)]
14mod tests;
15
16#[cfg_attr(not(target_os = "netbsd"), allow(unused))] // Not used on all platforms.
17pub use c_opaque::COpaque;
18#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
19pub use small_c_string::{run_path_with_cstr, run_with_cstr};
20#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
21pub use wstr::WStrUnits;
22
23/// Computes `(value*numerator)/denom` without overflow, as long as both
24/// `numerator*denom` and the overall result fit into `u64` (which is the case
25/// for our time conversions).
26#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
27pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
28    let q = value / denom;
29    let r = value % denom;
30    // Decompose value as (value/denom*denom + value%denom),
31    // substitute into (value*numerator)/denom and simplify.
32    // r < denom, so (denom*numerator) is the upper bound of (r*numerator)
33    q * numerator + r * numerator / denom
34}
35
36#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
37pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
38    match result {
39        Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
40        Ok(_) => Ok(()),
41        Err(err) => Err(err),
42    }
43}