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/// A trait for viewing representations from std types
27#[doc(hidden)]
28#[allow(dead_code)] // not used on all platforms
29pub trait AsInner<Inner: ?Sized> {
30    fn as_inner(&self) -> &Inner;
31}
32
33/// A trait for viewing representations from std types
34#[doc(hidden)]
35#[allow(dead_code)] // not used on all platforms
36pub trait AsInnerMut<Inner: ?Sized> {
37    fn as_inner_mut(&mut self) -> &mut Inner;
38}
39
40/// A trait for extracting representations from std types
41#[doc(hidden)]
42pub trait IntoInner<Inner> {
43    fn into_inner(self) -> Inner;
44}
45
46/// A trait for creating std types from internal representations
47#[doc(hidden)]
48pub trait FromInner<Inner> {
49    fn from_inner(inner: Inner) -> Self;
50}
51
52// Computes (value*numerator)/denom without overflow, as long as both (numerator*denom) and the
53// overall result fit into i64 (which is the case for our time conversions).
54#[allow(dead_code)] // not used on all platforms
55pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
56    let q = value / denom;
57    let r = value % denom;
58    // Decompose value as (value/denom*denom + value%denom),
59    // substitute into (value*numerator)/denom and simplify.
60    // r < denom, so (denom*numerator) is the upper bound of (r*numerator)
61    q * numerator + r * numerator / denom
62}
63
64pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
65    match result {
66        Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
67        Ok(_) => Ok(()),
68        Err(err) => Err(err),
69    }
70}