std/sys_common/mod.rs
1//! Platform-independent platform abstraction
2//!
3//! This is the platform-independent portion of the standard library's
4//! platform abstraction layer, whereas `std::sys` is the
5//! platform-specific portion.
6//!
7//! The relationship between `std::sys_common`, `std::sys` and the
8//! rest of `std` is complex, with dependencies going in all
9//! directions: `std` depending on `sys_common`, `sys_common`
10//! depending on `sys`, and `sys` depending on `sys_common` and `std`.
11//! This is because `sys_common` not only contains platform-independent code,
12//! but also code that is shared between the different platforms in `sys`.
13//! Ideally all that shared code should be moved to `sys::common`,
14//! and the dependencies between `std`, `sys_common` and `sys` all would form a DAG.
15//! Progress on this is tracked in #84187.
16
17#![allow(missing_docs)]
18
19#[cfg(test)]
20mod tests;
21
22pub mod wstr;
23
24// common error constructors
25
26// Computes (value*numerator)/denom without overflow, as long as both (numerator*denom) and the
27// overall result fit into i64 (which is the case for our time conversions).
28#[allow(dead_code)] // not used on all platforms
29pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
30 let q = value / denom;
31 let r = value % denom;
32 // Decompose value as (value/denom*denom + value%denom),
33 // substitute into (value*numerator)/denom and simplify.
34 // r < denom, so (denom*numerator) is the upper bound of (r*numerator)
35 q * numerator + r * numerator / denom
36}
37
38pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
39 match result {
40 Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
41 Ok(_) => Ok(()),
42 Err(err) => Err(err),
43 }
44}