1//! Platform-dependent platform abstraction.
2//!
3//! The `std::sys` module is the abstracted interface through which
4//! `std` talks to the underlying operating system. It has different
5//! implementations for different operating system families, today
6//! just Unix and Windows, and initial support for Redox.
7//!
8//! The centralization of platform-specific code in this module is
9//! enforced by the "platform abstraction layer" tidy script in
10//! `tools/tidy/src/pal.rs`.
11//!
12//! This module is closely related to the platform-independent system
13//! integration code in `std::sys_common`. See that module's
14//! documentation for details.
15//!
16//! In the future it would be desirable for the independent
17//! implementations of this module to be extracted to their own crates
18//! that `std` can link to, thus enabling their implementation
19//! out-of-tree via crate replacement. Though due to the complex
20//! inter-dependencies within `std` that will be a challenging goal to
21//! achieve.
22
23#![allow(missing_debug_implementations)]
24
25pub mod common;
26
27cfg_if::cfg_if! {
28 if #[cfg(unix)] {
29 mod unix;
30 pub use self::unix::*;
31 } else if #[cfg(windows)] {
32 mod windows;
33 pub use self::windows::*;
34 } else if #[cfg(target_os = "solid_asp3")] {
35 mod solid;
36 pub use self::solid::*;
37 } else if #[cfg(target_os = "hermit")] {
38 mod hermit;
39 pub use self::hermit::*;
40 } else if #[cfg(target_os = "wasi")] {
41 mod wasi;
42 pub use self::wasi::*;
43 } else if #[cfg(target_family = "wasm")] {
44 mod wasm;
45 pub use self::wasm::*;
46 } else if #[cfg(target_os = "xous")] {
47 mod xous;
48 pub use self::xous::*;
49 } else if #[cfg(target_os = "uefi")] {
50 mod uefi;
51 pub use self::uefi::*;
52 } else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
53 mod sgx;
54 pub use self::sgx::*;
55 } else if #[cfg(target_os = "teeos")] {
56 mod teeos;
57 pub use self::teeos::*;
58 } else if #[cfg(target_os = "zkvm")] {
59 mod zkvm;
60 pub use self::zkvm::*;
61 } else {
62 mod unsupported;
63 pub use self::unsupported::*;
64 }
65}
66
67cfg_if::cfg_if! {
68 // Fuchsia components default to full backtrace.
69 if #[cfg(target_os = "fuchsia")] {
70 pub const FULL_BACKTRACE_DEFAULT: bool = true;
71 } else {
72 pub const FULL_BACKTRACE_DEFAULT: bool = false;
73 }
74}
75
76#[cfg(not(test))]
77cfg_if::cfg_if! {
78 if #[cfg(target_os = "android")] {
79 pub use self::android::log2f32;
80 pub use self::android::log2f64;
81 } else {
82 #[inline]
83 pub fn log2f32(n: f32) -> f32 {
84 unsafe { crate::intrinsics::log2f32(n) }
85 }
86
87 #[inline]
88 pub fn log2f64(n: f64) -> f64 {
89 unsafe { crate::intrinsics::log2f64(n) }
90 }
91 }
92}
93
94// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
95// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
96// of expected NaN).
97#[cfg(not(test))]
98#[cfg(any(target_os = "solaris", target_os = "illumos"))]
99#[inline]
100pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
101 if n.is_finite() {
102 if n > 0.0 {
103 log_fn(n)
104 } else if n == 0.0 {
105 f64::NEG_INFINITY // log(0) = -Inf
106 } else {
107 f64::NAN // log(-n) = NaN
108 }
109 } else if n.is_nan() {
110 n // log(NaN) = NaN
111 } else if n > 0.0 {
112 n // log(Inf) = Inf
113 } else {
114 f64::NAN // log(-Inf) = NaN
115 }
116}
117
118#[cfg(not(test))]
119#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
120#[inline]
121pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
122 log_fn(n)
123}
124
125#[cfg(not(target_os = "uefi"))]
126pub type RawOsError = i32;
127