]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/hermit/mod.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.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 pub mod cmath;
24 pub mod condvar;
25 pub mod env;
26 pub mod ext;
27 pub mod fd;
28 pub mod fs;
29 pub mod io;
30 pub mod memchr;
31 pub mod mutex;
32 pub mod net;
33 pub mod os;
34 pub mod path;
35 #[path = "../unsupported/pipe.rs"]
36 pub mod pipe;
37 #[path = "../unsupported/process.rs"]
38 pub mod process;
39 pub mod rwlock;
40 pub mod stack_overflow;
41 pub mod stdio;
42 pub mod thread;
43 pub mod thread_local_dtor;
44 pub mod thread_local_key;
45 pub mod time;
46
47 use crate::io::ErrorKind;
48 pub use crate::sys_common::os_str_bytes as os_str;
49
50 #[allow(unused_extern_crates)]
51 pub extern crate hermit_abi as abi;
52
53 pub fn unsupported<T>() -> crate::io::Result<T> {
54 Err(unsupported_err())
55 }
56
57 pub fn unsupported_err() -> crate::io::Error {
58 crate::io::Error::new(crate::io::ErrorKind::Other, "operation not supported on HermitCore yet")
59 }
60
61 // This enum is used as the storage for a bunch of types which can't actually
62 // exist.
63 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
64 pub enum Void {}
65
66 pub unsafe fn strlen(start: *const c_char) -> usize {
67 let mut str = start;
68
69 while *str != 0 {
70 str = str.offset(1);
71 }
72
73 (str as usize) - (start as usize)
74 }
75
76 #[no_mangle]
77 pub extern "C" fn floor(x: f64) -> f64 {
78 unsafe { intrinsics::floorf64(x) }
79 }
80
81 pub fn abort_internal() -> ! {
82 unsafe {
83 abi::abort();
84 }
85 }
86
87 // FIXME: just a workaround to test the system
88 pub fn hashmap_random_keys() -> (u64, u64) {
89 (1, 2)
90 }
91
92 // This function is needed by the panic runtime. The symbol is named in
93 // pre-link args for the target specification, so keep that in sync.
94 #[cfg(not(test))]
95 #[no_mangle]
96 // NB. used by both libunwind and libpanic_abort
97 pub extern "C" fn __rust_abort() {
98 abort_internal();
99 }
100
101 #[cfg(not(test))]
102 pub fn init() {
103 let _ = net::init();
104 }
105
106 #[cfg(not(test))]
107 #[no_mangle]
108 pub unsafe extern "C" fn runtime_entry(
109 argc: i32,
110 argv: *const *const c_char,
111 env: *const *const c_char,
112 ) -> ! {
113 use crate::sys::hermit::thread_local_dtor::run_dtors;
114 extern "C" {
115 fn main(argc: isize, argv: *const *const c_char) -> i32;
116 }
117
118 // initialize environment
119 os::init_environment(env as *const *const i8);
120
121 let result = main(argc as isize, argv);
122
123 run_dtors();
124 abi::exit(result);
125 }
126
127 pub fn decode_error_kind(errno: i32) -> ErrorKind {
128 match errno {
129 x if x == 13 as i32 => ErrorKind::PermissionDenied,
130 x if x == 98 as i32 => ErrorKind::AddrInUse,
131 x if x == 99 as i32 => ErrorKind::AddrNotAvailable,
132 x if x == 11 as i32 => ErrorKind::WouldBlock,
133 x if x == 103 as i32 => ErrorKind::ConnectionAborted,
134 x if x == 111 as i32 => ErrorKind::ConnectionRefused,
135 x if x == 104 as i32 => ErrorKind::ConnectionReset,
136 x if x == 17 as i32 => ErrorKind::AlreadyExists,
137 x if x == 4 as i32 => ErrorKind::Interrupted,
138 x if x == 22 as i32 => ErrorKind::InvalidInput,
139 x if x == 2 as i32 => ErrorKind::NotFound,
140 x if x == 107 as i32 => ErrorKind::NotConnected,
141 x if x == 1 as i32 => ErrorKind::PermissionDenied,
142 x if x == 32 as i32 => ErrorKind::BrokenPipe,
143 x if x == 110 as i32 => ErrorKind::TimedOut,
144 _ => ErrorKind::Other,
145 }
146 }
147
148 pub fn cvt(result: i32) -> crate::io::Result<usize> {
149 if result < 0 { Err(crate::io::Error::from_raw_os_error(-result)) } else { Ok(result as usize) }
150 }