]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/mod.rs
Rollup merge of #79344 - JRF63:fix_install_script_win, r=Mark-Simulacrum
[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 //! Ideally `sys_common` would be split into two and the dependencies
12 //! between them all would form a dag, facilitating the extraction of
13 //! `std::sys` from the standard library.
14
15 #![allow(missing_docs)]
16 #![allow(missing_debug_implementations)]
17
18 #[cfg(test)]
19 mod tests;
20
21 use crate::sync::Once;
22 use crate::sys;
23
24 macro_rules! rtabort {
25     ($($t:tt)*) => (crate::sys_common::util::abort(format_args!($($t)*)))
26 }
27
28 macro_rules! rtassert {
29     ($e:expr) => {
30         if !$e {
31             rtabort!(concat!("assertion failed: ", stringify!($e)));
32         }
33     };
34 }
35
36 #[allow(unused_macros)] // not used on all platforms
37 macro_rules! rtunwrap {
38     ($ok:ident, $e:expr) => {
39         match $e {
40             $ok(v) => v,
41             ref err => {
42                 let err = err.as_ref().map(drop); // map Ok/Some which might not be Debug
43                 rtabort!(concat!("unwrap failed: ", stringify!($e), " = {:?}"), err)
44             }
45         }
46     };
47 }
48
49 pub mod alloc;
50 pub mod at_exit_imp;
51 pub mod backtrace;
52 pub mod bytestring;
53 pub mod condvar;
54 pub mod fs;
55 pub mod io;
56 pub mod mutex;
57 // `doc` is required because `sys/mod.rs` imports `unix/ext/mod.rs` on Windows
58 // when generating documentation.
59 #[cfg(any(doc, not(windows)))]
60 pub mod os_str_bytes;
61 pub mod poison;
62 pub mod process;
63 pub mod remutex;
64 pub mod rwlock;
65 pub mod thread;
66 pub mod thread_info;
67 pub mod thread_local_dtor;
68 pub mod thread_local_key;
69 pub mod thread_parker;
70 pub mod util;
71 pub mod wtf8;
72
73 cfg_if::cfg_if! {
74     if #[cfg(any(target_os = "l4re",
75                  target_os = "hermit",
76                  feature = "restricted-std",
77                  all(target_arch = "wasm32", not(target_os = "emscripten")),
78                  all(target_vendor = "fortanix", target_env = "sgx")))] {
79         pub use crate::sys::net;
80     } else {
81         pub mod net;
82     }
83 }
84
85 // common error constructors
86
87 /// A trait for viewing representations from std types
88 #[doc(hidden)]
89 pub trait AsInner<Inner: ?Sized> {
90     fn as_inner(&self) -> &Inner;
91 }
92
93 /// A trait for viewing representations from std types
94 #[doc(hidden)]
95 pub trait AsInnerMut<Inner: ?Sized> {
96     fn as_inner_mut(&mut self) -> &mut Inner;
97 }
98
99 /// A trait for extracting representations from std types
100 #[doc(hidden)]
101 pub trait IntoInner<Inner> {
102     fn into_inner(self) -> Inner;
103 }
104
105 /// A trait for creating std types from internal representations
106 #[doc(hidden)]
107 pub trait FromInner<Inner> {
108     fn from_inner(inner: Inner) -> Self;
109 }
110
111 /// Enqueues a procedure to run when the main thread exits.
112 ///
113 /// Currently these closures are only run once the main *Rust* thread exits.
114 /// Once the `at_exit` handlers begin running, more may be enqueued, but not
115 /// infinitely so. Eventually a handler registration will be forced to fail.
116 ///
117 /// Returns `Ok` if the handler was successfully registered, meaning that the
118 /// closure will be run once the main thread exits. Returns `Err` to indicate
119 /// that the closure could not be registered, meaning that it is not scheduled
120 /// to be run.
121 pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
122     if at_exit_imp::push(Box::new(f)) { Ok(()) } else { Err(()) }
123 }
124
125 /// One-time runtime cleanup.
126 pub fn cleanup() {
127     static CLEANUP: Once = Once::new();
128     CLEANUP.call_once(|| unsafe {
129         sys::args::cleanup();
130         sys::stack_overflow::cleanup();
131         at_exit_imp::cleanup();
132     });
133 }
134
135 // Computes (value*numer)/denom without overflow, as long as both
136 // (numer*denom) and the overall result fit into i64 (which is the case
137 // for our time conversions).
138 #[allow(dead_code)] // not used on all platforms
139 pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
140     let q = value / denom;
141     let r = value % denom;
142     // Decompose value as (value/denom*denom + value%denom),
143     // substitute into (value*numer)/denom and simplify.
144     // r < denom, so (denom*numer) is the upper bound of (r*numer)
145     q * numer + r * numer / denom
146 }