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