1//! The underlying OsString/OsStr implementation on Unix and many other
2//! systems: just a `Vec<u8>`/`[u8]`.
3
4use crate::borrow::Cow;
5use crate::collections::TryReserveError;
6use crate::fmt;
7use crate::fmt::Write;
8use crate::mem;
9use crate::rc::Rc;
10use crate::str;
11use crate::sync::Arc;
12use crate::sys_common::{AsInner, IntoInner};
13
14use core::str::Utf8Chunks;
15
16#[cfg(test)]
17mod tests;
18
19#[derive(Hash)]
20#[repr(transparent)]
21pub struct Buf {
22 pub inner: Vec<u8>,
23}
24
25#[repr(transparent)]
26pub struct Slice {
27 pub inner: [u8],
28}
29
30impl fmt::Debug for Slice {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 fmt::Debug::fmt(&Utf8Chunks::new(&self.inner).debug(), f)
33 }
34}
35
36impl fmt::Display for Slice {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 // If we're the empty string then our iterator won't actually yield
39 // anything, so perform the formatting manually
40 if self.inner.is_empty() {
41 return "".fmt(f);
42 }
43
44 for chunk: Utf8Chunk<'_> in Utf8Chunks::new(&self.inner) {
45 let valid: &str = chunk.valid();
46 // If we successfully decoded the whole chunk as a valid string then
47 // we can return a direct formatting of the string which will also
48 // respect various formatting flags if possible.
49 if chunk.invalid().is_empty() {
50 return valid.fmt(f);
51 }
52
53 f.write_str(data:valid)?;
54 f.write_char(char::REPLACEMENT_CHARACTER)?;
55 }
56 Ok(())
57 }
58}
59
60impl fmt::Debug for Buf {
61 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
62 fmt::Debug::fmt(self.as_slice(), f:formatter)
63 }
64}
65
66impl fmt::Display for Buf {
67 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
68 fmt::Display::fmt(self.as_slice(), f:formatter)
69 }
70}
71
72impl Clone for Buf {
73 #[inline]
74 fn clone(&self) -> Self {
75 Buf { inner: self.inner.clone() }
76 }
77
78 #[inline]
79 fn clone_from(&mut self, source: &Self) {
80 self.inner.clone_from(&source.inner)
81 }
82}
83
84impl IntoInner<Vec<u8>> for Buf {
85 fn into_inner(self) -> Vec<u8> {
86 self.inner
87 }
88}
89
90impl AsInner<[u8]> for Buf {
91 #[inline]
92 fn as_inner(&self) -> &[u8] {
93 &self.inner
94 }
95}
96
97impl Buf {
98 #[inline]
99 pub fn into_encoded_bytes(self) -> Vec<u8> {
100 self.inner
101 }
102
103 #[inline]
104 pub unsafe fn from_encoded_bytes_unchecked(s: Vec<u8>) -> Self {
105 Self { inner: s }
106 }
107
108 pub fn from_string(s: String) -> Buf {
109 Buf { inner: s.into_bytes() }
110 }
111
112 #[inline]
113 pub fn with_capacity(capacity: usize) -> Buf {
114 Buf { inner: Vec::with_capacity(capacity) }
115 }
116
117 #[inline]
118 pub fn clear(&mut self) {
119 self.inner.clear()
120 }
121
122 #[inline]
123 pub fn capacity(&self) -> usize {
124 self.inner.capacity()
125 }
126
127 #[inline]
128 pub fn reserve(&mut self, additional: usize) {
129 self.inner.reserve(additional)
130 }
131
132 #[inline]
133 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
134 self.inner.try_reserve(additional)
135 }
136
137 #[inline]
138 pub fn reserve_exact(&mut self, additional: usize) {
139 self.inner.reserve_exact(additional)
140 }
141
142 #[inline]
143 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
144 self.inner.try_reserve_exact(additional)
145 }
146
147 #[inline]
148 pub fn shrink_to_fit(&mut self) {
149 self.inner.shrink_to_fit()
150 }
151
152 #[inline]
153 pub fn shrink_to(&mut self, min_capacity: usize) {
154 self.inner.shrink_to(min_capacity)
155 }
156
157 #[inline]
158 pub fn as_slice(&self) -> &Slice {
159 // SAFETY: Slice just wraps [u8],
160 // and &*self.inner is &[u8], therefore
161 // transmuting &[u8] to &Slice is safe.
162 unsafe { mem::transmute(&*self.inner) }
163 }
164
165 #[inline]
166 pub fn as_mut_slice(&mut self) -> &mut Slice {
167 // SAFETY: Slice just wraps [u8],
168 // and &mut *self.inner is &mut [u8], therefore
169 // transmuting &mut [u8] to &mut Slice is safe.
170 unsafe { mem::transmute(&mut *self.inner) }
171 }
172
173 pub fn into_string(self) -> Result<String, Buf> {
174 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() })
175 }
176
177 pub fn push_slice(&mut self, s: &Slice) {
178 self.inner.extend_from_slice(&s.inner)
179 }
180
181 #[inline]
182 pub fn into_box(self) -> Box<Slice> {
183 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
184 }
185
186 #[inline]
187 pub fn from_box(boxed: Box<Slice>) -> Buf {
188 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
189 Buf { inner: inner.into_vec() }
190 }
191
192 #[inline]
193 pub fn into_arc(&self) -> Arc<Slice> {
194 self.as_slice().into_arc()
195 }
196
197 #[inline]
198 pub fn into_rc(&self) -> Rc<Slice> {
199 self.as_slice().into_rc()
200 }
201}
202
203impl Slice {
204 #[inline]
205 pub fn as_encoded_bytes(&self) -> &[u8] {
206 &self.inner
207 }
208
209 #[inline]
210 pub unsafe fn from_encoded_bytes_unchecked(s: &[u8]) -> &Slice {
211 unsafe { mem::transmute(s) }
212 }
213
214 #[inline]
215 pub fn from_str(s: &str) -> &Slice {
216 unsafe { Slice::from_encoded_bytes_unchecked(s.as_bytes()) }
217 }
218
219 pub fn to_str(&self) -> Result<&str, crate::str::Utf8Error> {
220 str::from_utf8(&self.inner)
221 }
222
223 pub fn to_string_lossy(&self) -> Cow<'_, str> {
224 String::from_utf8_lossy(&self.inner)
225 }
226
227 pub fn to_owned(&self) -> Buf {
228 Buf { inner: self.inner.to_vec() }
229 }
230
231 pub fn clone_into(&self, buf: &mut Buf) {
232 self.inner.clone_into(&mut buf.inner)
233 }
234
235 #[inline]
236 pub fn into_box(&self) -> Box<Slice> {
237 let boxed: Box<[u8]> = self.inner.into();
238 unsafe { mem::transmute(boxed) }
239 }
240
241 pub fn empty_box() -> Box<Slice> {
242 let boxed: Box<[u8]> = Default::default();
243 unsafe { mem::transmute(boxed) }
244 }
245
246 #[inline]
247 pub fn into_arc(&self) -> Arc<Slice> {
248 let arc: Arc<[u8]> = Arc::from(&self.inner);
249 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
250 }
251
252 #[inline]
253 pub fn into_rc(&self) -> Rc<Slice> {
254 let rc: Rc<[u8]> = Rc::from(&self.inner);
255 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }
256 }
257
258 #[inline]
259 pub fn make_ascii_lowercase(&mut self) {
260 self.inner.make_ascii_lowercase()
261 }
262
263 #[inline]
264 pub fn make_ascii_uppercase(&mut self) {
265 self.inner.make_ascii_uppercase()
266 }
267
268 #[inline]
269 pub fn to_ascii_lowercase(&self) -> Buf {
270 Buf { inner: self.inner.to_ascii_lowercase() }
271 }
272
273 #[inline]
274 pub fn to_ascii_uppercase(&self) -> Buf {
275 Buf { inner: self.inner.to_ascii_uppercase() }
276 }
277
278 #[inline]
279 pub fn is_ascii(&self) -> bool {
280 self.inner.is_ascii()
281 }
282
283 #[inline]
284 pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
285 self.inner.eq_ignore_ascii_case(&other.inner)
286 }
287}
288