]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/mod.rs
Reuse `unix::cmath`
[rust.git] / library / std / src / sys / wasi / mod.rs
1 //! System bindings for the wasm/web platform
2 //!
3 //! This module contains the facade (aka platform-specific) implementations of
4 //! OS level functionality for wasm. Note that this wasm is *not* the emscripten
5 //! wasm, so we have no runtime here.
6 //!
7 //! This is all super highly experimental and not actually intended for
8 //! wide/production use yet, it's still all in the experimental category. This
9 //! will likely change over time.
10 //!
11 //! Currently all functions here are basically stubs that immediately return
12 //! errors. The hope is that with a portability lint we can turn actually just
13 //! remove all this and just omit parts of the standard library if we're
14 //! compiling for wasm. That way it's a compile time error for something that's
15 //! guaranteed to be a runtime error!
16
17 use crate::io as std_io;
18 use crate::mem;
19
20 #[path = "../unix/alloc.rs"]
21 pub mod alloc;
22 pub mod args;
23 #[path = "../unix/cmath.rs"]
24 pub mod cmath;
25 #[path = "../unsupported/condvar.rs"]
26 pub mod condvar;
27 pub mod env;
28 pub mod fd;
29 pub mod fs;
30 pub mod io;
31 #[path = "../unsupported/mutex.rs"]
32 pub mod mutex;
33 pub mod net;
34 pub mod os;
35 pub use crate::sys_common::os_str_bytes as os_str;
36 pub mod ext;
37 #[path = "../unix/path.rs"]
38 pub mod path;
39 #[path = "../unsupported/pipe.rs"]
40 pub mod pipe;
41 #[path = "../unsupported/process.rs"]
42 pub mod process;
43 #[path = "../unsupported/rwlock.rs"]
44 pub mod rwlock;
45 #[path = "../unsupported/stack_overflow.rs"]
46 pub mod stack_overflow;
47 pub mod stdio;
48 pub mod thread;
49 #[path = "../unsupported/thread_local_dtor.rs"]
50 pub mod thread_local_dtor;
51 #[path = "../unsupported/thread_local_key.rs"]
52 pub mod thread_local_key;
53 pub mod time;
54
55 #[path = "../unsupported/common.rs"]
56 #[deny(unsafe_op_in_unsafe_fn)]
57 #[allow(unused)]
58 mod common;
59 pub use common::*;
60
61 pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind {
62     use std_io::ErrorKind::*;
63     if errno > u16::MAX as i32 || errno < 0 {
64         return Other;
65     }
66     match errno as u16 {
67         wasi::ERRNO_CONNREFUSED => ConnectionRefused,
68         wasi::ERRNO_CONNRESET => ConnectionReset,
69         wasi::ERRNO_PERM | wasi::ERRNO_ACCES => PermissionDenied,
70         wasi::ERRNO_PIPE => BrokenPipe,
71         wasi::ERRNO_NOTCONN => NotConnected,
72         wasi::ERRNO_CONNABORTED => ConnectionAborted,
73         wasi::ERRNO_ADDRNOTAVAIL => AddrNotAvailable,
74         wasi::ERRNO_ADDRINUSE => AddrInUse,
75         wasi::ERRNO_NOENT => NotFound,
76         wasi::ERRNO_INTR => Interrupted,
77         wasi::ERRNO_INVAL => InvalidInput,
78         wasi::ERRNO_TIMEDOUT => TimedOut,
79         wasi::ERRNO_EXIST => AlreadyExists,
80         wasi::ERRNO_AGAIN => WouldBlock,
81         wasi::ERRNO_NOSYS => Unsupported,
82         _ => Other,
83     }
84 }
85
86 pub fn abort_internal() -> ! {
87     unsafe { libc::abort() }
88 }
89
90 pub fn hashmap_random_keys() -> (u64, u64) {
91     let mut ret = (0u64, 0u64);
92     unsafe {
93         let base = &mut ret as *mut (u64, u64) as *mut u8;
94         let len = mem::size_of_val(&ret);
95         wasi::random_get(base, len).expect("random_get failure");
96     }
97     return ret;
98 }
99
100 fn err2io(err: wasi::Error) -> std_io::Error {
101     std_io::Error::from_raw_os_error(err.raw_error().into())
102 }