]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/mod.rs
894440564b738d3772dd5c63bbf8a65ee2752c4d
[rust.git] / library / std / src / 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 #![allow(missing_debug_implementations)]
19
20 #[cfg(test)]
21 mod tests;
22
23 pub mod backtrace;
24 pub mod condvar;
25 pub mod fs;
26 pub mod io;
27 pub mod memchr;
28 pub mod mutex;
29 pub mod process;
30 pub mod remutex;
31 #[macro_use]
32 pub mod rt;
33 pub mod rwlock;
34 pub mod thread;
35 pub mod thread_info;
36 pub mod thread_local_dtor;
37 pub mod thread_local_key;
38 pub mod thread_parker;
39 pub mod wtf8;
40
41 cfg_if::cfg_if! {
42     if #[cfg(any(target_os = "l4re",
43                  target_os = "hermit",
44                  feature = "restricted-std",
45                  all(target_arch = "wasm32", not(target_os = "emscripten")),
46                  all(target_vendor = "fortanix", target_env = "sgx")))] {
47         pub use crate::sys::net;
48     } else {
49         pub mod net;
50     }
51 }
52
53 // common error constructors
54
55 /// A trait for viewing representations from std types
56 #[doc(hidden)]
57 pub trait AsInner<Inner: ?Sized> {
58     fn as_inner(&self) -> &Inner;
59 }
60
61 /// A trait for viewing representations from std types
62 #[doc(hidden)]
63 pub trait AsInnerMut<Inner: ?Sized> {
64     fn as_inner_mut(&mut self) -> &mut Inner;
65 }
66
67 /// A trait for extracting representations from std types
68 #[doc(hidden)]
69 pub trait IntoInner<Inner> {
70     fn into_inner(self) -> Inner;
71 }
72
73 /// A trait for creating std types from internal representations
74 #[doc(hidden)]
75 pub trait FromInner<Inner> {
76     fn from_inner(inner: Inner) -> Self;
77 }
78
79 // Computes (value*numer)/denom without overflow, as long as both
80 // (numer*denom) and the overall result fit into i64 (which is the case
81 // for our time conversions).
82 #[allow(dead_code)] // not used on all platforms
83 pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
84     let q = value / denom;
85     let r = value % denom;
86     // Decompose value as (value/denom*denom + value%denom),
87     // substitute into (value*numer)/denom and simplify.
88     // r < denom, so (denom*numer) is the upper bound of (r*numer)
89     q * numer + r * numer / denom
90 }