]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/ffi.rs
Auto merge of #57108 - Mark-Simulacrum:license-remove, r=pietroalbini
[rust.git] / src / libstd / sys / unix / ext / ffi.rs
1 //! Unix-specific extension to the primitives in the `std::ffi` module
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use ffi::{OsStr, OsString};
6 use mem;
7 use sys::os_str::Buf;
8 use sys_common::{FromInner, IntoInner, AsInner};
9
10 /// Unix-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     ///
17     /// # Examples
18     ///
19     /// ```
20     /// use std::ffi::OsString;
21     /// use std::os::unix::ffi::OsStringExt;
22     ///
23     /// let bytes = b"foo".to_vec();
24     /// let os_string = OsString::from_vec(bytes);
25     /// assert_eq!(os_string.to_str(), Some("foo"));
26     /// ```
27     ///
28     /// [`OsString`]: ../../../ffi/struct.OsString.html
29     #[stable(feature = "rust1", since = "1.0.0")]
30     fn from_vec(vec: Vec<u8>) -> Self;
31
32     /// Yields the underlying byte vector of this [`OsString`].
33     ///
34     /// # Examples
35     ///
36     /// ```
37     /// use std::ffi::OsString;
38     /// use std::os::unix::ffi::OsStringExt;
39     ///
40     /// let mut os_string = OsString::new();
41     /// os_string.push("foo");
42     /// let bytes = os_string.into_vec();
43     /// assert_eq!(bytes, b"foo");
44     /// ```
45     ///
46     /// [`OsString`]: ../../../ffi/struct.OsString.html
47     #[stable(feature = "rust1", since = "1.0.0")]
48     fn into_vec(self) -> Vec<u8>;
49 }
50
51 #[stable(feature = "rust1", since = "1.0.0")]
52 impl OsStringExt for OsString {
53     fn from_vec(vec: Vec<u8>) -> OsString {
54         FromInner::from_inner(Buf { inner: vec })
55     }
56     fn into_vec(self) -> Vec<u8> {
57         self.into_inner().inner
58     }
59 }
60
61 /// Unix-specific extensions to [`OsStr`].
62 ///
63 /// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub trait OsStrExt {
66     #[stable(feature = "rust1", since = "1.0.0")]
67     /// Creates an [`OsStr`] from a byte slice.
68     ///
69     /// # Examples
70     ///
71     /// ```
72     /// use std::ffi::OsStr;
73     /// use std::os::unix::ffi::OsStrExt;
74     ///
75     /// let bytes = b"foo";
76     /// let os_str = OsStr::from_bytes(bytes);
77     /// assert_eq!(os_str.to_str(), Some("foo"));
78     /// ```
79     ///
80     /// [`OsStr`]: ../../../ffi/struct.OsStr.html
81     fn from_bytes(slice: &[u8]) -> &Self;
82
83     /// Gets the underlying byte view of the [`OsStr`] slice.
84     ///
85     /// # Examples
86     ///
87     /// ```
88     /// use std::ffi::OsStr;
89     /// use std::os::unix::ffi::OsStrExt;
90     ///
91     /// let mut os_str = OsStr::new("foo");
92     /// let bytes = os_str.as_bytes();
93     /// assert_eq!(bytes, b"foo");
94     /// ```
95     ///
96     /// [`OsStr`]: ../../../ffi/struct.OsStr.html
97     #[stable(feature = "rust1", since = "1.0.0")]
98     fn as_bytes(&self) -> &[u8];
99 }
100
101 #[stable(feature = "rust1", since = "1.0.0")]
102 impl OsStrExt for OsStr {
103     fn from_bytes(slice: &[u8]) -> &OsStr {
104         unsafe { mem::transmute(slice) }
105     }
106     fn as_bytes(&self) -> &[u8] {
107         &self.as_inner().inner
108     }
109 }