]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/wasi/mod.rs
Auto merge of #104977 - RalfJung:ptr-from-ref, r=dtolnay
[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 pub mod env;
26 pub mod fd;
27 pub mod fs;
28 #[allow(unused)]
29 #[path = "../wasm/atomics/futex.rs"]
30 pub mod futex;
31 pub mod io;
32 #[path = "../unsupported/locks/mod.rs"]
33 pub mod locks;
34 pub mod net;
35 #[path = "../unsupported/once.rs"]
36 pub mod once;
37 pub mod os;
38 #[path = "../unix/os_str.rs"]
39 pub mod os_str;
40 #[path = "../unix/path.rs"]
41 pub mod path;
42 #[path = "../unsupported/pipe.rs"]
43 pub mod pipe;
44 #[path = "../unsupported/process.rs"]
45 pub mod process;
46 pub mod stdio;
47 pub mod thread;
48 #[path = "../unsupported/thread_local_dtor.rs"]
49 pub mod thread_local_dtor;
50 #[path = "../unsupported/thread_local_key.rs"]
51 pub mod thread_local_key;
52 pub mod time;
53
54 #[path = "../unsupported/common.rs"]
55 #[deny(unsafe_op_in_unsafe_fn)]
56 #[allow(unused)]
57 mod common;
58 pub use common::*;
59
60 pub fn decode_error_kind(errno: i32) -> std_io::ErrorKind {
61     use std_io::ErrorKind::*;
62     if errno > u16::MAX as i32 || errno < 0 {
63         return Uncategorized;
64     }
65
66     match errno {
67         e if e == wasi::ERRNO_CONNREFUSED.raw().into() => ConnectionRefused,
68         e if e == wasi::ERRNO_CONNRESET.raw().into() => ConnectionReset,
69         e if e == wasi::ERRNO_PERM.raw().into() || e == wasi::ERRNO_ACCES.raw().into() => {
70             PermissionDenied
71         }
72         e if e == wasi::ERRNO_PIPE.raw().into() => BrokenPipe,
73         e if e == wasi::ERRNO_NOTCONN.raw().into() => NotConnected,
74         e if e == wasi::ERRNO_CONNABORTED.raw().into() => ConnectionAborted,
75         e if e == wasi::ERRNO_ADDRNOTAVAIL.raw().into() => AddrNotAvailable,
76         e if e == wasi::ERRNO_ADDRINUSE.raw().into() => AddrInUse,
77         e if e == wasi::ERRNO_NOENT.raw().into() => NotFound,
78         e if e == wasi::ERRNO_INTR.raw().into() => Interrupted,
79         e if e == wasi::ERRNO_INVAL.raw().into() => InvalidInput,
80         e if e == wasi::ERRNO_TIMEDOUT.raw().into() => TimedOut,
81         e if e == wasi::ERRNO_EXIST.raw().into() => AlreadyExists,
82         e if e == wasi::ERRNO_AGAIN.raw().into() => WouldBlock,
83         e if e == wasi::ERRNO_NOSYS.raw().into() => Unsupported,
84         e if e == wasi::ERRNO_NOMEM.raw().into() => OutOfMemory,
85         _ => Uncategorized,
86     }
87 }
88
89 pub fn abort_internal() -> ! {
90     unsafe { libc::abort() }
91 }
92
93 pub fn hashmap_random_keys() -> (u64, u64) {
94     let mut ret = (0u64, 0u64);
95     unsafe {
96         let base = &mut ret as *mut (u64, u64) as *mut u8;
97         let len = mem::size_of_val(&ret);
98         wasi::random_get(base, len).expect("random_get failure");
99     }
100     return ret;
101 }
102
103 fn err2io(err: wasi::Errno) -> std_io::Error {
104     std_io::Error::from_raw_os_error(err.raw().into())
105 }