1 //! WASI-specific extension to the primitives in the `std::ffi` module
3 #![stable(feature = "rust1", since = "1.0.0")]
5 use crate::ffi::{OsStr, OsString};
7 use crate::sys::os_str::Buf;
8 use crate::sys_common::{FromInner, IntoInner, AsInner};
10 /// WASI-specific extensions to [`OsString`].
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;
19 /// Yields the underlying byte vector of this `OsString`.
20 #[stable(feature = "rust1", since = "1.0.0")]
21 fn into_vec(self) -> Vec<u8>;
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 })
29 fn into_vec(self) -> Vec<u8> {
30 self.into_inner().inner
34 /// WASI-specific extensions to [`OsStr`].
36 /// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
37 #[stable(feature = "rust1", since = "1.0.0")]
39 #[stable(feature = "rust1", since = "1.0.0")]
40 /// Creates an [`OsStr`] from a byte slice.
42 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
43 fn from_bytes(slice: &[u8]) -> &Self;
45 /// Gets the underlying byte view of the [`OsStr`] slice.
47 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
48 #[stable(feature = "rust1", since = "1.0.0")]
49 fn as_bytes(&self) -> &[u8];
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) }
57 fn as_bytes(&self) -> &[u8] {
58 &self.as_inner().inner