1 /// The underlying OsString/OsStr implementation on Unix systems: just
2 /// a `Vec<u8>`/`[u8]`.
4 use crate::borrow::Cow;
10 use crate::sys_common::{AsInner, IntoInner};
11 use crate::sys_common::bytestring::debug_fmt_bytestring;
13 use core::str::lossy::Utf8Lossy;
15 #[derive(Clone, Hash)]
24 impl fmt::Debug for Slice {
25 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
26 debug_fmt_bytestring(&self.inner, formatter)
30 impl fmt::Display for Slice {
31 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
32 fmt::Display::fmt(&Utf8Lossy::from_bytes(&self.inner), formatter)
36 impl fmt::Debug for Buf {
37 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
38 fmt::Debug::fmt(self.as_slice(), formatter)
42 impl fmt::Display for Buf {
43 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
44 fmt::Display::fmt(self.as_slice(), formatter)
48 impl IntoInner<Vec<u8>> for Buf {
49 fn into_inner(self) -> Vec<u8> {
54 impl AsInner<[u8]> for Buf {
55 fn as_inner(&self) -> &[u8] {
62 pub fn from_string(s: String) -> Buf {
63 Buf { inner: s.into_bytes() }
67 pub fn with_capacity(capacity: usize) -> Buf {
69 inner: Vec::with_capacity(capacity)
74 pub fn clear(&mut self) {
79 pub fn capacity(&self) -> usize {
84 pub fn reserve(&mut self, additional: usize) {
85 self.inner.reserve(additional)
89 pub fn reserve_exact(&mut self, additional: usize) {
90 self.inner.reserve_exact(additional)
94 pub fn shrink_to_fit(&mut self) {
95 self.inner.shrink_to_fit()
99 pub fn shrink_to(&mut self, min_capacity: usize) {
100 self.inner.shrink_to(min_capacity)
103 pub fn as_slice(&self) -> &Slice {
104 unsafe { mem::transmute(&*self.inner) }
107 pub fn into_string(self) -> Result<String, Buf> {
108 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
111 pub fn push_slice(&mut self, s: &Slice) {
112 self.inner.extend_from_slice(&s.inner)
116 pub fn into_box(self) -> Box<Slice> {
117 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
121 pub fn from_box(boxed: Box<Slice>) -> Buf {
122 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
123 Buf { inner: inner.into_vec() }
127 pub fn into_arc(&self) -> Arc<Slice> {
128 self.as_slice().into_arc()
132 pub fn into_rc(&self) -> Rc<Slice> {
133 self.as_slice().into_rc()
138 fn from_u8_slice(s: &[u8]) -> &Slice {
139 unsafe { mem::transmute(s) }
142 pub fn from_str(s: &str) -> &Slice {
143 Slice::from_u8_slice(s.as_bytes())
146 pub fn to_str(&self) -> Option<&str> {
147 str::from_utf8(&self.inner).ok()
150 pub fn to_string_lossy(&self) -> Cow<str> {
151 String::from_utf8_lossy(&self.inner)
154 pub fn to_owned(&self) -> Buf {
155 Buf { inner: self.inner.to_vec() }
159 pub fn into_box(&self) -> Box<Slice> {
160 let boxed: Box<[u8]> = self.inner.into();
161 unsafe { mem::transmute(boxed) }
164 pub fn empty_box() -> Box<Slice> {
165 let boxed: Box<[u8]> = Default::default();
166 unsafe { mem::transmute(boxed) }
170 pub fn into_arc(&self) -> Arc<Slice> {
171 let arc: Arc<[u8]> = Arc::from(&self.inner);
172 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
176 pub fn into_rc(&self) -> Rc<Slice> {
177 let rc: Rc<[u8]> = Rc::from(&self.inner);
178 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }