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