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