]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/hermit/mod.rs
Rollup merge of #101534 - rust-lang:notriddle/rustdoc-flex-direction, r=GuillaumeGomez
[rust.git] / library / std / src / sys / hermit / mod.rs
1 //! System bindings for HermitCore
2 //!
3 //! This module contains the facade (aka platform-specific) implementations of
4 //! OS level functionality for HermitCore.
5 //!
6 //! This is all super highly experimental and not actually intended for
7 //! wide/production use yet, it's still all in the experimental category. This
8 //! will likely change over time.
9 //!
10 //! Currently all functions here are basically stubs that immediately return
11 //! errors. The hope is that with a portability lint we can turn actually just
12 //! remove all this and just omit parts of the standard library if we're
13 //! compiling for wasm. That way it's a compile time error for something that's
14 //! guaranteed to be a runtime error!
15
16 #![allow(unsafe_op_in_unsafe_fn)]
17
18 use crate::intrinsics;
19 use crate::os::raw::c_char;
20
21 pub mod alloc;
22 pub mod args;
23 #[path = "../unix/cmath.rs"]
24 pub mod cmath;
25 pub mod env;
26 pub mod fd;
27 pub mod fs;
28 #[path = "../unsupported/io.rs"]
29 pub mod io;
30 pub mod memchr;
31 pub mod net;
32 pub mod os;
33 #[path = "../unix/os_str.rs"]
34 pub mod os_str;
35 #[path = "../unix/path.rs"]
36 pub mod path;
37 #[path = "../unsupported/pipe.rs"]
38 pub mod pipe;
39 #[path = "../unsupported/process.rs"]
40 pub mod process;
41 pub mod stdio;
42 pub mod thread;
43 pub mod thread_local_dtor;
44 #[path = "../unsupported/thread_local_key.rs"]
45 pub mod thread_local_key;
46 pub mod time;
47
48 mod condvar;
49 mod mutex;
50 mod rwlock;
51
52 pub mod locks {
53     pub use super::condvar::*;
54     pub use super::mutex::*;
55     pub use super::rwlock::*;
56 }
57
58 use crate::io::ErrorKind;
59
60 #[allow(unused_extern_crates)]
61 pub extern crate hermit_abi as abi;
62
63 pub fn unsupported<T>() -> crate::io::Result<T> {
64     Err(unsupported_err())
65 }
66
67 pub fn unsupported_err() -> crate::io::Error {
68     crate::io::const_io_error!(
69         crate::io::ErrorKind::Unsupported,
70         "operation not supported on HermitCore yet",
71     )
72 }
73
74 #[no_mangle]
75 pub extern "C" fn floor(x: f64) -> f64 {
76     unsafe { intrinsics::floorf64(x) }
77 }
78
79 pub fn abort_internal() -> ! {
80     unsafe {
81         abi::abort();
82     }
83 }
84
85 // FIXME: just a workaround to test the system
86 pub fn hashmap_random_keys() -> (u64, u64) {
87     (1, 2)
88 }
89
90 // This function is needed by the panic runtime. The symbol is named in
91 // pre-link args for the target specification, so keep that in sync.
92 #[cfg(not(test))]
93 #[no_mangle]
94 // NB. used by both libunwind and libpanic_abort
95 pub extern "C" fn __rust_abort() {
96     abort_internal();
97 }
98
99 // SAFETY: must be called only once during runtime initialization.
100 // NOTE: this is not guaranteed to run, for example when Rust code is called externally.
101 pub unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
102     let _ = net::init();
103     args::init(argc, argv);
104 }
105
106 // SAFETY: must be called only once during runtime cleanup.
107 // NOTE: this is not guaranteed to run, for example when the program aborts.
108 pub unsafe fn cleanup() {
109     args::cleanup();
110 }
111
112 #[cfg(not(test))]
113 #[no_mangle]
114 pub unsafe extern "C" fn runtime_entry(
115     argc: i32,
116     argv: *const *const c_char,
117     env: *const *const c_char,
118 ) -> ! {
119     use crate::sys::hermit::thread_local_dtor::run_dtors;
120     extern "C" {
121         fn main(argc: isize, argv: *const *const c_char) -> i32;
122     }
123
124     // initialize environment
125     os::init_environment(env as *const *const i8);
126
127     let result = main(argc as isize, argv);
128
129     run_dtors();
130     abi::exit(result);
131 }
132
133 pub fn decode_error_kind(errno: i32) -> ErrorKind {
134     match errno {
135         x if x == 13 as i32 => ErrorKind::PermissionDenied,
136         x if x == 98 as i32 => ErrorKind::AddrInUse,
137         x if x == 99 as i32 => ErrorKind::AddrNotAvailable,
138         x if x == 11 as i32 => ErrorKind::WouldBlock,
139         x if x == 103 as i32 => ErrorKind::ConnectionAborted,
140         x if x == 111 as i32 => ErrorKind::ConnectionRefused,
141         x if x == 104 as i32 => ErrorKind::ConnectionReset,
142         x if x == 17 as i32 => ErrorKind::AlreadyExists,
143         x if x == 4 as i32 => ErrorKind::Interrupted,
144         x if x == 22 as i32 => ErrorKind::InvalidInput,
145         x if x == 2 as i32 => ErrorKind::NotFound,
146         x if x == 107 as i32 => ErrorKind::NotConnected,
147         x if x == 1 as i32 => ErrorKind::PermissionDenied,
148         x if x == 32 as i32 => ErrorKind::BrokenPipe,
149         x if x == 110 as i32 => ErrorKind::TimedOut,
150         _ => ErrorKind::Uncategorized,
151     }
152 }
153
154 pub fn cvt(result: i32) -> crate::io::Result<usize> {
155     if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) }
156 }