]>
Commit | Line | Data |
---|---|---|
532ac7d7 XL |
1 | //! The underlying OsString/OsStr implementation on Unix and many other |
2 | //! systems: just a `Vec<u8>`/`[u8]`. | |
3 | ||
4 | use crate::borrow::Cow; | |
5 | use crate::ffi::{OsStr, OsString}; | |
6 | use crate::fmt; | |
7 | use crate::str; | |
8 | use crate::mem; | |
9 | use crate::rc::Rc; | |
10 | use crate::sync::Arc; | |
11 | use crate::sys_common::{FromInner, IntoInner, AsInner}; | |
12 | use crate::sys_common::bytestring::debug_fmt_bytestring; | |
13 | ||
83c7162d | 14 | use core::str::lossy::Utf8Lossy; |
476ff2be SL |
15 | |
16 | #[derive(Clone, Hash)] | |
532ac7d7 | 17 | pub(crate) struct Buf { |
476ff2be SL |
18 | pub inner: Vec<u8> |
19 | } | |
20 | ||
416331ca XL |
21 | // FIXME: |
22 | // `Buf::as_slice` current implementation relies | |
23 | // on `Slice` being layout-compatible with `[u8]`. | |
24 | // When attribute privacy is implemented, `Slice` should be annotated as `#[repr(transparent)]`. | |
25 | // Anyway, `Slice` representation and layout are considered implementation detail, are | |
26 | // not documented and must not be relied upon. | |
532ac7d7 | 27 | pub(crate) struct Slice { |
476ff2be SL |
28 | pub inner: [u8] |
29 | } | |
30 | ||
041b39d2 | 31 | impl fmt::Debug for Slice { |
532ac7d7 | 32 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
ff7c6d11 | 33 | debug_fmt_bytestring(&self.inner, formatter) |
476ff2be SL |
34 | } |
35 | } | |
36 | ||
041b39d2 | 37 | impl fmt::Display for Slice { |
532ac7d7 | 38 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 XL |
39 | fmt::Display::fmt(&Utf8Lossy::from_bytes(&self.inner), formatter) |
40 | } | |
41 | } | |
42 | ||
43 | impl fmt::Debug for Buf { | |
532ac7d7 | 44 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 XL |
45 | fmt::Debug::fmt(self.as_slice(), formatter) |
46 | } | |
47 | } | |
48 | ||
49 | impl fmt::Display for Buf { | |
532ac7d7 | 50 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 | 51 | fmt::Display::fmt(self.as_slice(), formatter) |
476ff2be SL |
52 | } |
53 | } | |
54 | ||
55 | impl IntoInner<Vec<u8>> for Buf { | |
56 | fn into_inner(self) -> Vec<u8> { | |
57 | self.inner | |
58 | } | |
59 | } | |
60 | ||
61 | impl AsInner<[u8]> for Buf { | |
62 | fn as_inner(&self) -> &[u8] { | |
63 | &self.inner | |
64 | } | |
65 | } | |
66 | ||
67 | ||
68 | impl Buf { | |
69 | pub fn from_string(s: String) -> Buf { | |
70 | Buf { inner: s.into_bytes() } | |
71 | } | |
72 | ||
73 | #[inline] | |
74 | pub fn with_capacity(capacity: usize) -> Buf { | |
75 | Buf { | |
76 | inner: Vec::with_capacity(capacity) | |
77 | } | |
78 | } | |
79 | ||
80 | #[inline] | |
81 | pub fn clear(&mut self) { | |
82 | self.inner.clear() | |
83 | } | |
84 | ||
85 | #[inline] | |
86 | pub fn capacity(&self) -> usize { | |
87 | self.inner.capacity() | |
88 | } | |
89 | ||
90 | #[inline] | |
91 | pub fn reserve(&mut self, additional: usize) { | |
92 | self.inner.reserve(additional) | |
93 | } | |
94 | ||
95 | #[inline] | |
96 | pub fn reserve_exact(&mut self, additional: usize) { | |
97 | self.inner.reserve_exact(additional) | |
98 | } | |
99 | ||
8bb4bdeb XL |
100 | #[inline] |
101 | pub fn shrink_to_fit(&mut self) { | |
102 | self.inner.shrink_to_fit() | |
103 | } | |
104 | ||
0531ce1d XL |
105 | #[inline] |
106 | pub fn shrink_to(&mut self, min_capacity: usize) { | |
107 | self.inner.shrink_to(min_capacity) | |
108 | } | |
109 | ||
476ff2be SL |
110 | pub fn as_slice(&self) -> &Slice { |
111 | unsafe { mem::transmute(&*self.inner) } | |
112 | } | |
113 | ||
114 | pub fn into_string(self) -> Result<String, Buf> { | |
115 | String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } ) | |
116 | } | |
117 | ||
118 | pub fn push_slice(&mut self, s: &Slice) { | |
119 | self.inner.extend_from_slice(&s.inner) | |
120 | } | |
8bb4bdeb XL |
121 | |
122 | #[inline] | |
123 | pub fn into_box(self) -> Box<Slice> { | |
124 | unsafe { mem::transmute(self.inner.into_boxed_slice()) } | |
125 | } | |
cc61c64b XL |
126 | |
127 | #[inline] | |
128 | pub fn from_box(boxed: Box<Slice>) -> Buf { | |
129 | let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; | |
130 | Buf { inner: inner.into_vec() } | |
131 | } | |
ff7c6d11 XL |
132 | |
133 | #[inline] | |
134 | pub fn into_arc(&self) -> Arc<Slice> { | |
135 | self.as_slice().into_arc() | |
136 | } | |
137 | ||
138 | #[inline] | |
139 | pub fn into_rc(&self) -> Rc<Slice> { | |
140 | self.as_slice().into_rc() | |
141 | } | |
476ff2be SL |
142 | } |
143 | ||
144 | impl Slice { | |
145 | fn from_u8_slice(s: &[u8]) -> &Slice { | |
146 | unsafe { mem::transmute(s) } | |
147 | } | |
148 | ||
149 | pub fn from_str(s: &str) -> &Slice { | |
150 | Slice::from_u8_slice(s.as_bytes()) | |
151 | } | |
152 | ||
153 | pub fn to_str(&self) -> Option<&str> { | |
154 | str::from_utf8(&self.inner).ok() | |
155 | } | |
156 | ||
532ac7d7 | 157 | pub fn to_string_lossy(&self) -> Cow<'_, str> { |
476ff2be SL |
158 | String::from_utf8_lossy(&self.inner) |
159 | } | |
160 | ||
161 | pub fn to_owned(&self) -> Buf { | |
162 | Buf { inner: self.inner.to_vec() } | |
163 | } | |
8bb4bdeb XL |
164 | |
165 | #[inline] | |
166 | pub fn into_box(&self) -> Box<Slice> { | |
167 | let boxed: Box<[u8]> = self.inner.into(); | |
168 | unsafe { mem::transmute(boxed) } | |
169 | } | |
170 | ||
171 | pub fn empty_box() -> Box<Slice> { | |
172 | let boxed: Box<[u8]> = Default::default(); | |
173 | unsafe { mem::transmute(boxed) } | |
174 | } | |
ff7c6d11 XL |
175 | |
176 | #[inline] | |
177 | pub fn into_arc(&self) -> Arc<Slice> { | |
178 | let arc: Arc<[u8]> = Arc::from(&self.inner); | |
179 | unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) } | |
180 | } | |
181 | ||
182 | #[inline] | |
183 | pub fn into_rc(&self) -> Rc<Slice> { | |
184 | let rc: Rc<[u8]> = Rc::from(&self.inner); | |
185 | unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) } | |
186 | } | |
476ff2be | 187 | } |
532ac7d7 XL |
188 | |
189 | /// Platform-specific extensions to [`OsString`]. | |
190 | /// | |
191 | /// [`OsString`]: ../../../../std/ffi/struct.OsString.html | |
192 | #[stable(feature = "rust1", since = "1.0.0")] | |
193 | pub trait OsStringExt { | |
194 | /// Creates an [`OsString`] from a byte vector. | |
195 | /// | |
e74abb32 | 196 | /// See the module documentation for an example. |
532ac7d7 XL |
197 | /// |
198 | /// [`OsString`]: ../../../ffi/struct.OsString.html | |
199 | #[stable(feature = "rust1", since = "1.0.0")] | |
200 | fn from_vec(vec: Vec<u8>) -> Self; | |
201 | ||
202 | /// Yields the underlying byte vector of this [`OsString`]. | |
203 | /// | |
e74abb32 | 204 | /// See the module documentation for an example. |
532ac7d7 XL |
205 | /// |
206 | /// [`OsString`]: ../../../ffi/struct.OsString.html | |
207 | #[stable(feature = "rust1", since = "1.0.0")] | |
208 | fn into_vec(self) -> Vec<u8>; | |
209 | } | |
210 | ||
211 | #[stable(feature = "rust1", since = "1.0.0")] | |
212 | impl OsStringExt for OsString { | |
213 | fn from_vec(vec: Vec<u8>) -> OsString { | |
214 | FromInner::from_inner(Buf { inner: vec }) | |
215 | } | |
216 | fn into_vec(self) -> Vec<u8> { | |
217 | self.into_inner().inner | |
218 | } | |
219 | } | |
220 | ||
221 | /// Platform-specific extensions to [`OsStr`]. | |
222 | /// | |
223 | /// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html | |
224 | #[stable(feature = "rust1", since = "1.0.0")] | |
225 | pub trait OsStrExt { | |
226 | #[stable(feature = "rust1", since = "1.0.0")] | |
227 | /// Creates an [`OsStr`] from a byte slice. | |
228 | /// | |
e74abb32 | 229 | /// See the module documentation for an example. |
532ac7d7 XL |
230 | /// |
231 | /// [`OsStr`]: ../../../ffi/struct.OsStr.html | |
232 | fn from_bytes(slice: &[u8]) -> &Self; | |
233 | ||
234 | /// Gets the underlying byte view of the [`OsStr`] slice. | |
235 | /// | |
e74abb32 | 236 | /// See the module documentation for an example. |
532ac7d7 XL |
237 | /// |
238 | /// [`OsStr`]: ../../../ffi/struct.OsStr.html | |
239 | #[stable(feature = "rust1", since = "1.0.0")] | |
240 | fn as_bytes(&self) -> &[u8]; | |
241 | } | |
242 | ||
243 | #[stable(feature = "rust1", since = "1.0.0")] | |
244 | impl OsStrExt for OsStr { | |
245 | #[inline] | |
246 | fn from_bytes(slice: &[u8]) -> &OsStr { | |
247 | unsafe { mem::transmute(slice) } | |
248 | } | |
249 | #[inline] | |
250 | fn as_bytes(&self) -> &[u8] { | |
251 | &self.as_inner().inner | |
252 | } | |
253 | } |