]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasi/ext/ffi.rs
Add a new wasm32-unknown-wasi target
[rust.git] / src / libstd / sys / wasi / ext / ffi.rs
1 //! WASI-specific extension to the primitives in the `std::ffi` module
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use crate::ffi::{OsStr, OsString};
6 use crate::mem;
7 use crate::sys::os_str::Buf;
8 use crate::sys_common::{FromInner, IntoInner, AsInner};
9
10 /// WASI-specific extensions to [`OsString`].
11 ///
12 /// [`OsString`]: ../../../../std/ffi/struct.OsString.html
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub trait OsStringExt {
15     /// Creates an `OsString` from a byte vector.
16     #[stable(feature = "rust1", since = "1.0.0")]
17     fn from_vec(vec: Vec<u8>) -> Self;
18
19     /// Yields the underlying byte vector of this `OsString`.
20     #[stable(feature = "rust1", since = "1.0.0")]
21     fn into_vec(self) -> Vec<u8>;
22 }
23
24 #[stable(feature = "rust1", since = "1.0.0")]
25 impl OsStringExt for OsString {
26     fn from_vec(vec: Vec<u8>) -> OsString {
27         FromInner::from_inner(Buf { inner: vec })
28     }
29     fn into_vec(self) -> Vec<u8> {
30         self.into_inner().inner
31     }
32 }
33
34 /// WASI-specific extensions to [`OsStr`].
35 ///
36 /// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub trait OsStrExt {
39     #[stable(feature = "rust1", since = "1.0.0")]
40     /// Creates an [`OsStr`] from a byte slice.
41     ///
42     /// [`OsStr`]: ../../../ffi/struct.OsStr.html
43     fn from_bytes(slice: &[u8]) -> &Self;
44
45     /// Gets the underlying byte view of the [`OsStr`] slice.
46     ///
47     /// [`OsStr`]: ../../../ffi/struct.OsStr.html
48     #[stable(feature = "rust1", since = "1.0.0")]
49     fn as_bytes(&self) -> &[u8];
50 }
51
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl OsStrExt for OsStr {
54     fn from_bytes(slice: &[u8]) -> &OsStr {
55         unsafe { mem::transmute(slice) }
56     }
57     fn as_bytes(&self) -> &[u8] {
58         &self.as_inner().inner
59     }
60 }
61