]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/mod.rs
Rollup merge of #55754 - spastorino:fix-process-output-docs, r=alexcrichton
[rust.git] / src / libstd / sys_common / mod.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Platform-independent platform abstraction
12 //!
13 //! This is the platform-independent portion of the standard library's
14 //! platform abstraction layer, whereas `std::sys` is the
15 //! platform-specific portion.
16 //!
17 //! The relationship between `std::sys_common`, `std::sys` and the
18 //! rest of `std` is complex, with dependencies going in all
19 //! directions: `std` depending on `sys_common`, `sys_common`
20 //! depending on `sys`, and `sys` depending on `sys_common` and `std`.
21 //! Ideally `sys_common` would be split into two and the dependencies
22 //! between them all would form a dag, facilitating the extraction of
23 //! `std::sys` from the standard library.
24
25 #![allow(missing_docs)]
26 #![allow(missing_debug_implementations)]
27
28 use sync::Once;
29 use sys;
30
31 macro_rules! rtabort {
32     ($($t:tt)*) => (::sys_common::util::abort(format_args!($($t)*)))
33 }
34
35 macro_rules! rtassert {
36     ($e:expr) => (if !$e {
37         rtabort!(concat!("assertion failed: ", stringify!($e)));
38     })
39 }
40
41 pub mod alloc;
42 pub mod at_exit_imp;
43 #[cfg(feature = "backtrace")]
44 pub mod backtrace;
45 pub mod condvar;
46 pub mod io;
47 pub mod mutex;
48 pub mod poison;
49 pub mod remutex;
50 pub mod rwlock;
51 pub mod thread;
52 pub mod thread_info;
53 pub mod thread_local;
54 pub mod util;
55 pub mod wtf8;
56 pub mod bytestring;
57 pub mod process;
58
59 cfg_if! {
60     if #[cfg(any(target_os = "cloudabi", target_os = "l4re", target_os = "redox"))] {
61         pub use sys::net;
62     } else if #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] {
63         pub use sys::net;
64     } else {
65         pub mod net;
66     }
67 }
68
69 #[cfg(feature = "backtrace")]
70 #[cfg(any(all(unix, not(target_os = "emscripten")),
71           all(windows, target_env = "gnu"),
72           target_os = "redox"))]
73 pub mod gnu;
74
75 // common error constructors
76
77 /// A trait for viewing representations from std types
78 #[doc(hidden)]
79 pub trait AsInner<Inner: ?Sized> {
80     fn as_inner(&self) -> &Inner;
81 }
82
83 /// A trait for viewing representations from std types
84 #[doc(hidden)]
85 pub trait AsInnerMut<Inner: ?Sized> {
86     fn as_inner_mut(&mut self) -> &mut Inner;
87 }
88
89 /// A trait for extracting representations from std types
90 #[doc(hidden)]
91 pub trait IntoInner<Inner> {
92     fn into_inner(self) -> Inner;
93 }
94
95 /// A trait for creating std types from internal representations
96 #[doc(hidden)]
97 pub trait FromInner<Inner> {
98     fn from_inner(inner: Inner) -> Self;
99 }
100
101 /// Enqueues a procedure to run when the main thread exits.
102 ///
103 /// Currently these closures are only run once the main *Rust* thread exits.
104 /// Once the `at_exit` handlers begin running, more may be enqueued, but not
105 /// infinitely so. Eventually a handler registration will be forced to fail.
106 ///
107 /// Returns `Ok` if the handler was successfully registered, meaning that the
108 /// closure will be run once the main thread exits. Returns `Err` to indicate
109 /// that the closure could not be registered, meaning that it is not scheduled
110 /// to be run.
111 pub fn at_exit<F: FnOnce() + Send + 'static>(f: F) -> Result<(), ()> {
112     if at_exit_imp::push(Box::new(f)) {Ok(())} else {Err(())}
113 }
114
115 /// One-time runtime cleanup.
116 pub fn cleanup() {
117     static CLEANUP: Once = Once::new();
118     CLEANUP.call_once(|| unsafe {
119         sys::args::cleanup();
120         sys::stack_overflow::cleanup();
121         at_exit_imp::cleanup();
122     });
123 }
124
125 // Computes (value*numer)/denom without overflow, as long as both
126 // (numer*denom) and the overall result fit into i64 (which is the case
127 // for our time conversions).
128 #[allow(dead_code)] // not used on all platforms
129 pub fn mul_div_u64(value: u64, numer: u64, denom: u64) -> u64 {
130     let q = value / denom;
131     let r = value % denom;
132     // Decompose value as (value/denom*denom + value%denom),
133     // substitute into (value*numer)/denom and simplify.
134     // r < denom, so (denom*numer) is the upper bound of (r*numer)
135     q * numer + r * numer / denom
136 }
137
138 #[test]
139 fn test_muldiv() {
140     assert_eq!(mul_div_u64( 1_000_000_000_001, 1_000_000_000, 1_000_000),
141                1_000_000_000_001_000);
142 }