1 //! The underlying OsString/OsStr implementation on Unix and many other
2 //! systems: just a `Vec<u8>`/`[u8]`.
4 use crate::borrow::Cow;
5 use crate::ffi::{OsStr, OsString};
11 use crate::sys_common::bytestring::debug_fmt_bytestring;
12 use crate::sys_common::{AsInner, FromInner, IntoInner};
14 use core::str::lossy::Utf8Lossy;
16 #[derive(Clone, Hash)]
17 pub(crate) struct Buf {
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.
27 pub(crate) struct Slice {
31 impl fmt::Debug for Slice {
32 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33 debug_fmt_bytestring(&self.inner, formatter)
37 impl fmt::Display for Slice {
38 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39 fmt::Display::fmt(&Utf8Lossy::from_bytes(&self.inner), formatter)
43 impl fmt::Debug for Buf {
44 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
45 fmt::Debug::fmt(self.as_slice(), formatter)
49 impl fmt::Display for Buf {
50 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
51 fmt::Display::fmt(self.as_slice(), formatter)
55 impl IntoInner<Vec<u8>> for Buf {
56 fn into_inner(self) -> Vec<u8> {
61 impl AsInner<[u8]> for Buf {
62 fn as_inner(&self) -> &[u8] {
68 pub fn from_string(s: String) -> Buf {
69 Buf { inner: s.into_bytes() }
73 pub fn with_capacity(capacity: usize) -> Buf {
74 Buf { inner: Vec::with_capacity(capacity) }
78 pub fn clear(&mut self) {
83 pub fn capacity(&self) -> usize {
88 pub fn reserve(&mut self, additional: usize) {
89 self.inner.reserve(additional)
93 pub fn reserve_exact(&mut self, additional: usize) {
94 self.inner.reserve_exact(additional)
98 pub fn shrink_to_fit(&mut self) {
99 self.inner.shrink_to_fit()
103 pub fn shrink_to(&mut self, min_capacity: usize) {
104 self.inner.shrink_to(min_capacity)
108 pub fn as_slice(&self) -> &Slice {
109 unsafe { mem::transmute(&*self.inner) }
112 pub fn into_string(self) -> Result<String, Buf> {
113 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() })
116 pub fn push_slice(&mut self, s: &Slice) {
117 self.inner.extend_from_slice(&s.inner)
121 pub fn into_box(self) -> Box<Slice> {
122 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
126 pub fn from_box(boxed: Box<Slice>) -> Buf {
127 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
128 Buf { inner: inner.into_vec() }
132 pub fn into_arc(&self) -> Arc<Slice> {
133 self.as_slice().into_arc()
137 pub fn into_rc(&self) -> Rc<Slice> {
138 self.as_slice().into_rc()
144 fn from_u8_slice(s: &[u8]) -> &Slice {
145 unsafe { mem::transmute(s) }
149 pub fn from_str(s: &str) -> &Slice {
150 Slice::from_u8_slice(s.as_bytes())
153 pub fn to_str(&self) -> Option<&str> {
154 str::from_utf8(&self.inner).ok()
157 pub fn to_string_lossy(&self) -> Cow<'_, str> {
158 String::from_utf8_lossy(&self.inner)
161 pub fn to_owned(&self) -> Buf {
162 Buf { inner: self.inner.to_vec() }
166 pub fn into_box(&self) -> Box<Slice> {
167 let boxed: Box<[u8]> = self.inner.into();
168 unsafe { mem::transmute(boxed) }
171 pub fn empty_box() -> Box<Slice> {
172 let boxed: Box<[u8]> = Default::default();
173 unsafe { mem::transmute(boxed) }
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) }
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) }
189 /// Platform-specific extensions to [`OsString`].
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.
196 /// See the module documentation for an example.
198 /// [`OsString`]: ../../../ffi/struct.OsString.html
199 #[stable(feature = "rust1", since = "1.0.0")]
200 fn from_vec(vec: Vec<u8>) -> Self;
202 /// Yields the underlying byte vector of this [`OsString`].
204 /// See the module documentation for an example.
206 /// [`OsString`]: ../../../ffi/struct.OsString.html
207 #[stable(feature = "rust1", since = "1.0.0")]
208 fn into_vec(self) -> Vec<u8>;
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 })
216 fn into_vec(self) -> Vec<u8> {
217 self.into_inner().inner
221 /// Platform-specific extensions to [`OsStr`].
223 /// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
224 #[stable(feature = "rust1", since = "1.0.0")]
226 #[stable(feature = "rust1", since = "1.0.0")]
227 /// Creates an [`OsStr`] from a byte slice.
229 /// See the module documentation for an example.
231 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
232 fn from_bytes(slice: &[u8]) -> &Self;
234 /// Gets the underlying byte view of the [`OsStr`] slice.
236 /// See the module documentation for an example.
238 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
239 #[stable(feature = "rust1", since = "1.0.0")]
240 fn as_bytes(&self) -> &[u8];
243 #[stable(feature = "rust1", since = "1.0.0")]
244 impl OsStrExt for OsStr {
246 fn from_bytes(slice: &[u8]) -> &OsStr {
247 unsafe { mem::transmute(slice) }
250 fn as_bytes(&self) -> &[u8] {
251 &self.as_inner().inner