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::{FromInner, IntoInner, AsInner};
12 use crate::sys_common::bytestring::debug_fmt_bytestring;
14 use core::str::lossy::Utf8Lossy;
16 #[derive(Clone, Hash)]
17 pub(crate) struct Buf {
21 pub(crate) struct Slice {
25 impl fmt::Debug for Slice {
26 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
27 debug_fmt_bytestring(&self.inner, formatter)
31 impl fmt::Display for Slice {
32 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
33 fmt::Display::fmt(&Utf8Lossy::from_bytes(&self.inner), formatter)
37 impl fmt::Debug for Buf {
38 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
39 fmt::Debug::fmt(self.as_slice(), formatter)
43 impl fmt::Display for Buf {
44 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
45 fmt::Display::fmt(self.as_slice(), formatter)
49 impl IntoInner<Vec<u8>> for Buf {
50 fn into_inner(self) -> Vec<u8> {
55 impl AsInner<[u8]> for Buf {
56 fn as_inner(&self) -> &[u8] {
63 pub fn from_string(s: String) -> Buf {
64 Buf { inner: s.into_bytes() }
68 pub fn with_capacity(capacity: usize) -> Buf {
70 inner: Vec::with_capacity(capacity)
75 pub fn clear(&mut self) {
80 pub fn capacity(&self) -> usize {
85 pub fn reserve(&mut self, additional: usize) {
86 self.inner.reserve(additional)
90 pub fn reserve_exact(&mut self, additional: usize) {
91 self.inner.reserve_exact(additional)
95 pub fn shrink_to_fit(&mut self) {
96 self.inner.shrink_to_fit()
100 pub fn shrink_to(&mut self, min_capacity: usize) {
101 self.inner.shrink_to(min_capacity)
104 pub fn as_slice(&self) -> &Slice {
105 unsafe { mem::transmute(&*self.inner) }
108 pub fn into_string(self) -> Result<String, Buf> {
109 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
112 pub fn push_slice(&mut self, s: &Slice) {
113 self.inner.extend_from_slice(&s.inner)
117 pub fn into_box(self) -> Box<Slice> {
118 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
122 pub fn from_box(boxed: Box<Slice>) -> Buf {
123 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
124 Buf { inner: inner.into_vec() }
128 pub fn into_arc(&self) -> Arc<Slice> {
129 self.as_slice().into_arc()
133 pub fn into_rc(&self) -> Rc<Slice> {
134 self.as_slice().into_rc()
139 fn from_u8_slice(s: &[u8]) -> &Slice {
140 unsafe { mem::transmute(s) }
143 pub fn from_str(s: &str) -> &Slice {
144 Slice::from_u8_slice(s.as_bytes())
147 pub fn to_str(&self) -> Option<&str> {
148 str::from_utf8(&self.inner).ok()
151 pub fn to_string_lossy(&self) -> Cow<str> {
152 String::from_utf8_lossy(&self.inner)
155 pub fn to_owned(&self) -> Buf {
156 Buf { inner: self.inner.to_vec() }
160 pub fn into_box(&self) -> Box<Slice> {
161 let boxed: Box<[u8]> = self.inner.into();
162 unsafe { mem::transmute(boxed) }
165 pub fn empty_box() -> Box<Slice> {
166 let boxed: Box<[u8]> = Default::default();
167 unsafe { mem::transmute(boxed) }
171 pub fn into_arc(&self) -> Arc<Slice> {
172 let arc: Arc<[u8]> = Arc::from(&self.inner);
173 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
177 pub fn into_rc(&self) -> Rc<Slice> {
178 let rc: Rc<[u8]> = Rc::from(&self.inner);
179 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }
183 /// Platform-specific extensions to [`OsString`].
185 /// [`OsString`]: ../../../../std/ffi/struct.OsString.html
186 #[stable(feature = "rust1", since = "1.0.0")]
187 pub trait OsStringExt {
188 /// Creates an [`OsString`] from a byte vector.
190 /// See the module docmentation for an example.
192 /// [`OsString`]: ../../../ffi/struct.OsString.html
193 #[stable(feature = "rust1", since = "1.0.0")]
194 fn from_vec(vec: Vec<u8>) -> Self;
196 /// Yields the underlying byte vector of this [`OsString`].
198 /// See the module docmentation for an example.
200 /// [`OsString`]: ../../../ffi/struct.OsString.html
201 #[stable(feature = "rust1", since = "1.0.0")]
202 fn into_vec(self) -> Vec<u8>;
205 #[stable(feature = "rust1", since = "1.0.0")]
206 impl OsStringExt for OsString {
207 fn from_vec(vec: Vec<u8>) -> OsString {
208 FromInner::from_inner(Buf { inner: vec })
210 fn into_vec(self) -> Vec<u8> {
211 self.into_inner().inner
215 /// Platform-specific extensions to [`OsStr`].
217 /// [`OsStr`]: ../../../../std/ffi/struct.OsStr.html
218 #[stable(feature = "rust1", since = "1.0.0")]
220 #[stable(feature = "rust1", since = "1.0.0")]
221 /// Creates an [`OsStr`] from a byte slice.
223 /// See the module docmentation for an example.
225 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
226 fn from_bytes(slice: &[u8]) -> &Self;
228 /// Gets the underlying byte view of the [`OsStr`] slice.
230 /// See the module docmentation for an example.
232 /// [`OsStr`]: ../../../ffi/struct.OsStr.html
233 #[stable(feature = "rust1", since = "1.0.0")]
234 fn as_bytes(&self) -> &[u8];
237 #[stable(feature = "rust1", since = "1.0.0")]
238 impl OsStrExt for OsStr {
239 fn from_bytes(slice: &[u8]) -> &OsStr {
240 unsafe { mem::transmute(slice) }
242 fn as_bytes(&self) -> &[u8] {
243 &self.as_inner().inner