]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys_common/os_str_bytes.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys_common / os_str_bytes.rs
CommitLineData
532ac7d7
XL
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::ffi::{OsStr, OsString};
6use crate::fmt;
532ac7d7
XL
7use crate::mem;
8use crate::rc::Rc;
60c5eb7d 9use crate::str;
532ac7d7 10use crate::sync::Arc;
532ac7d7 11use crate::sys_common::bytestring::debug_fmt_bytestring;
60c5eb7d 12use crate::sys_common::{AsInner, FromInner, IntoInner};
532ac7d7 13
83c7162d 14use core::str::lossy::Utf8Lossy;
476ff2be
SL
15
16#[derive(Clone, Hash)]
532ac7d7 17pub(crate) struct Buf {
60c5eb7d 18 pub inner: Vec<u8>,
476ff2be
SL
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 27pub(crate) struct Slice {
60c5eb7d 28 pub inner: [u8],
476ff2be
SL
29}
30
041b39d2 31impl 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 37impl 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
43impl 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
49impl 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
55impl IntoInner<Vec<u8>> for Buf {
56 fn into_inner(self) -> Vec<u8> {
57 self.inner
58 }
59}
60
61impl AsInner<[u8]> for Buf {
62 fn as_inner(&self) -> &[u8] {
63 &self.inner
64 }
65}
66
476ff2be
SL
67impl Buf {
68 pub fn from_string(s: String) -> Buf {
69 Buf { inner: s.into_bytes() }
70 }
71
72 #[inline]
73 pub fn with_capacity(capacity: usize) -> Buf {
60c5eb7d 74 Buf { inner: Vec::with_capacity(capacity) }
476ff2be
SL
75 }
76
77 #[inline]
78 pub fn clear(&mut self) {
79 self.inner.clear()
80 }
81
82 #[inline]
83 pub fn capacity(&self) -> usize {
84 self.inner.capacity()
85 }
86
87 #[inline]
88 pub fn reserve(&mut self, additional: usize) {
89 self.inner.reserve(additional)
90 }
91
92 #[inline]
93 pub fn reserve_exact(&mut self, additional: usize) {
94 self.inner.reserve_exact(additional)
95 }
96
8bb4bdeb
XL
97 #[inline]
98 pub fn shrink_to_fit(&mut self) {
99 self.inner.shrink_to_fit()
100 }
101
0531ce1d
XL
102 #[inline]
103 pub fn shrink_to(&mut self, min_capacity: usize) {
104 self.inner.shrink_to(min_capacity)
105 }
106
476ff2be
SL
107 pub fn as_slice(&self) -> &Slice {
108 unsafe { mem::transmute(&*self.inner) }
109 }
110
111 pub fn into_string(self) -> Result<String, Buf> {
60c5eb7d 112 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() })
476ff2be
SL
113 }
114
115 pub fn push_slice(&mut self, s: &Slice) {
116 self.inner.extend_from_slice(&s.inner)
117 }
8bb4bdeb
XL
118
119 #[inline]
120 pub fn into_box(self) -> Box<Slice> {
121 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
122 }
cc61c64b
XL
123
124 #[inline]
125 pub fn from_box(boxed: Box<Slice>) -> Buf {
126 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
127 Buf { inner: inner.into_vec() }
128 }
ff7c6d11
XL
129
130 #[inline]
131 pub fn into_arc(&self) -> Arc<Slice> {
132 self.as_slice().into_arc()
133 }
134
135 #[inline]
136 pub fn into_rc(&self) -> Rc<Slice> {
137 self.as_slice().into_rc()
138 }
476ff2be
SL
139}
140
141impl Slice {
60c5eb7d 142 #[inline]
476ff2be
SL
143 fn from_u8_slice(s: &[u8]) -> &Slice {
144 unsafe { mem::transmute(s) }
145 }
146
60c5eb7d 147 #[inline]
476ff2be
SL
148 pub fn from_str(s: &str) -> &Slice {
149 Slice::from_u8_slice(s.as_bytes())
150 }
151
152 pub fn to_str(&self) -> Option<&str> {
153 str::from_utf8(&self.inner).ok()
154 }
155
532ac7d7 156 pub fn to_string_lossy(&self) -> Cow<'_, str> {
476ff2be
SL
157 String::from_utf8_lossy(&self.inner)
158 }
159
160 pub fn to_owned(&self) -> Buf {
161 Buf { inner: self.inner.to_vec() }
162 }
8bb4bdeb
XL
163
164 #[inline]
165 pub fn into_box(&self) -> Box<Slice> {
166 let boxed: Box<[u8]> = self.inner.into();
167 unsafe { mem::transmute(boxed) }
168 }
169
170 pub fn empty_box() -> Box<Slice> {
171 let boxed: Box<[u8]> = Default::default();
172 unsafe { mem::transmute(boxed) }
173 }
ff7c6d11
XL
174
175 #[inline]
176 pub fn into_arc(&self) -> Arc<Slice> {
177 let arc: Arc<[u8]> = Arc::from(&self.inner);
178 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
179 }
180
181 #[inline]
182 pub fn into_rc(&self) -> Rc<Slice> {
183 let rc: Rc<[u8]> = Rc::from(&self.inner);
184 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }
185 }
476ff2be 186}
532ac7d7
XL
187
188/// Platform-specific extensions to [`OsString`].
189///
190/// [`OsString`]: ../../../../std/ffi/struct.OsString.html
191#[stable(feature = "rust1", since = "1.0.0")]
192pub trait OsStringExt {
193 /// Creates an [`OsString`] from a byte vector.
194 ///
e74abb32 195 /// See the module documentation for an example.
532ac7d7
XL
196 ///
197 /// [`OsString`]: ../../../ffi/struct.OsString.html
198 #[stable(feature = "rust1", since = "1.0.0")]
199 fn from_vec(vec: Vec<u8>) -> Self;
200
201 /// Yields the underlying byte vector of this [`OsString`].
202 ///
e74abb32 203 /// See the module documentation for an example.
532ac7d7
XL
204 ///
205 /// [`OsString`]: ../../../ffi/struct.OsString.html
206 #[stable(feature = "rust1", since = "1.0.0")]
207 fn into_vec(self) -> Vec<u8>;
208}
209
210#[stable(feature = "rust1", since = "1.0.0")]
211impl OsStringExt for OsString {
212 fn from_vec(vec: Vec<u8>) -> OsString {
213 FromInner::from_inner(Buf { inner: vec })
214 }
215 fn into_vec(self) -> Vec<u8> {
216 self.into_inner().inner
217 }
218}
219
220/// Platform-specific extensions to [`OsStr`].
221///
222/// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
223#[stable(feature = "rust1", since = "1.0.0")]
224pub trait OsStrExt {
225 #[stable(feature = "rust1", since = "1.0.0")]
226 /// Creates an [`OsStr`] from a byte slice.
227 ///
e74abb32 228 /// See the module documentation for an example.
532ac7d7
XL
229 ///
230 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
231 fn from_bytes(slice: &[u8]) -> &Self;
232
233 /// Gets the underlying byte view of the [`OsStr`] slice.
234 ///
e74abb32 235 /// See the module documentation for an example.
532ac7d7
XL
236 ///
237 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
238 #[stable(feature = "rust1", since = "1.0.0")]
239 fn as_bytes(&self) -> &[u8];
240}
241
242#[stable(feature = "rust1", since = "1.0.0")]
243impl OsStrExt for OsStr {
244 #[inline]
245 fn from_bytes(slice: &[u8]) -> &OsStr {
246 unsafe { mem::transmute(slice) }
247 }
248 #[inline]
249 fn as_bytes(&self) -> &[u8] {
250 &self.as_inner().inner
251 }
252}