]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/ext/ffi.rs
671498bc39037e29fc813eb21637c8a40a7eff3c
[rust.git] / src / libstd / sys / redox / ext / ffi.rs
1 //! Redox-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 /// Redox-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 /// Redox-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     fn from_bytes(slice: &[u8]) -> &Self;
41
42     /// Gets the underlying byte view of the `OsStr` slice.
43     #[stable(feature = "rust1", since = "1.0.0")]
44     fn as_bytes(&self) -> &[u8];
45 }
46
47 #[stable(feature = "rust1", since = "1.0.0")]
48 impl OsStrExt for OsStr {
49     fn from_bytes(slice: &[u8]) -> &OsStr {
50         unsafe { mem::transmute(slice) }
51     }
52     fn as_bytes(&self) -> &[u8] {
53         &self.as_inner().inner
54     }
55 }