1 /// The underlying OsString/OsStr implementation on Unix systems: just
2 /// a `Vec<u8>`/`[u8]`.
10 use sys_common::{AsInner, IntoInner};
11 use sys_common::bytestring::debug_fmt_bytestring;
12 use core::str::lossy::Utf8Lossy;
14 #[derive(Clone, Hash)]
23 impl fmt::Debug for Slice {
24 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
25 debug_fmt_bytestring(&self.inner, formatter)
29 impl fmt::Display for Slice {
30 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
31 fmt::Display::fmt(&Utf8Lossy::from_bytes(&self.inner), formatter)
35 impl fmt::Debug for Buf {
36 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
37 fmt::Debug::fmt(self.as_slice(), formatter)
41 impl fmt::Display for Buf {
42 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
43 fmt::Display::fmt(self.as_slice(), formatter)
47 impl IntoInner<Vec<u8>> for Buf {
48 fn into_inner(self) -> Vec<u8> {
53 impl AsInner<[u8]> for Buf {
54 fn as_inner(&self) -> &[u8] {
61 pub fn from_string(s: String) -> Buf {
62 Buf { inner: s.into_bytes() }
66 pub fn with_capacity(capacity: usize) -> Buf {
68 inner: Vec::with_capacity(capacity)
73 pub fn clear(&mut self) {
78 pub fn capacity(&self) -> usize {
83 pub fn reserve(&mut self, additional: usize) {
84 self.inner.reserve(additional)
88 pub fn reserve_exact(&mut self, additional: usize) {
89 self.inner.reserve_exact(additional)
93 pub fn shrink_to_fit(&mut self) {
94 self.inner.shrink_to_fit()
98 pub fn shrink_to(&mut self, min_capacity: usize) {
99 self.inner.shrink_to(min_capacity)
102 pub fn as_slice(&self) -> &Slice {
103 unsafe { mem::transmute(&*self.inner) }
106 pub fn into_string(self) -> Result<String, Buf> {
107 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
110 pub fn push_slice(&mut self, s: &Slice) {
111 self.inner.extend_from_slice(&s.inner)
115 pub fn into_box(self) -> Box<Slice> {
116 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
120 pub fn from_box(boxed: Box<Slice>) -> Buf {
121 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
122 Buf { inner: inner.into_vec() }
126 pub fn into_arc(&self) -> Arc<Slice> {
127 self.as_slice().into_arc()
131 pub fn into_rc(&self) -> Rc<Slice> {
132 self.as_slice().into_rc()
137 fn from_u8_slice(s: &[u8]) -> &Slice {
138 unsafe { mem::transmute(s) }
141 pub fn from_str(s: &str) -> &Slice {
142 Slice::from_u8_slice(s.as_bytes())
145 pub fn to_str(&self) -> Option<&str> {
146 str::from_utf8(&self.inner).ok()
149 pub fn to_string_lossy(&self) -> Cow<str> {
150 String::from_utf8_lossy(&self.inner)
153 pub fn to_owned(&self) -> Buf {
154 Buf { inner: self.inner.to_vec() }
158 pub fn into_box(&self) -> Box<Slice> {
159 let boxed: Box<[u8]> = self.inner.into();
160 unsafe { mem::transmute(boxed) }
163 pub fn empty_box() -> Box<Slice> {
164 let boxed: Box<[u8]> = Default::default();
165 unsafe { mem::transmute(boxed) }
169 pub fn into_arc(&self) -> Arc<Slice> {
170 let arc: Arc<[u8]> = Arc::from(&self.inner);
171 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
175 pub fn into_rc(&self) -> Rc<Slice> {
176 let rc: Rc<[u8]> = Rc::from(&self.inner);
177 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }