]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/os_str.rs
Don't default on std crate when manipulating browser history
[rust.git] / src / libstd / sys / sgx / os_str.rs
1 /// The underlying OsString/OsStr implementation on Unix systems: just
2 /// a `Vec<u8>`/`[u8]`.
3
4 use borrow::Cow;
5 use fmt;
6 use str;
7 use mem;
8 use rc::Rc;
9 use sync::Arc;
10 use sys_common::{AsInner, IntoInner};
11 use sys_common::bytestring::debug_fmt_bytestring;
12 use core::str::lossy::Utf8Lossy;
13
14 #[derive(Clone, Hash)]
15 pub struct Buf {
16     pub inner: Vec<u8>
17 }
18
19 pub struct Slice {
20     pub inner: [u8]
21 }
22
23 impl fmt::Debug for Slice {
24     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
25         debug_fmt_bytestring(&self.inner, formatter)
26     }
27 }
28
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)
32     }
33 }
34
35 impl fmt::Debug for Buf {
36     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
37         fmt::Debug::fmt(self.as_slice(), formatter)
38     }
39 }
40
41 impl fmt::Display for Buf {
42     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
43         fmt::Display::fmt(self.as_slice(), formatter)
44     }
45 }
46
47 impl IntoInner<Vec<u8>> for Buf {
48     fn into_inner(self) -> Vec<u8> {
49         self.inner
50     }
51 }
52
53 impl AsInner<[u8]> for Buf {
54     fn as_inner(&self) -> &[u8] {
55         &self.inner
56     }
57 }
58
59
60 impl Buf {
61     pub fn from_string(s: String) -> Buf {
62         Buf { inner: s.into_bytes() }
63     }
64
65     #[inline]
66     pub fn with_capacity(capacity: usize) -> Buf {
67         Buf {
68             inner: Vec::with_capacity(capacity)
69         }
70     }
71
72     #[inline]
73     pub fn clear(&mut self) {
74         self.inner.clear()
75     }
76
77     #[inline]
78     pub fn capacity(&self) -> usize {
79         self.inner.capacity()
80     }
81
82     #[inline]
83     pub fn reserve(&mut self, additional: usize) {
84         self.inner.reserve(additional)
85     }
86
87     #[inline]
88     pub fn reserve_exact(&mut self, additional: usize) {
89         self.inner.reserve_exact(additional)
90     }
91
92     #[inline]
93     pub fn shrink_to_fit(&mut self) {
94         self.inner.shrink_to_fit()
95     }
96
97     #[inline]
98     pub fn shrink_to(&mut self, min_capacity: usize) {
99         self.inner.shrink_to(min_capacity)
100     }
101
102     pub fn as_slice(&self) -> &Slice {
103         unsafe { mem::transmute(&*self.inner) }
104     }
105
106     pub fn into_string(self) -> Result<String, Buf> {
107         String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
108     }
109
110     pub fn push_slice(&mut self, s: &Slice) {
111         self.inner.extend_from_slice(&s.inner)
112     }
113
114     #[inline]
115     pub fn into_box(self) -> Box<Slice> {
116         unsafe { mem::transmute(self.inner.into_boxed_slice()) }
117     }
118
119     #[inline]
120     pub fn from_box(boxed: Box<Slice>) -> Buf {
121         let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
122         Buf { inner: inner.into_vec() }
123     }
124
125     #[inline]
126     pub fn into_arc(&self) -> Arc<Slice> {
127         self.as_slice().into_arc()
128     }
129
130     #[inline]
131     pub fn into_rc(&self) -> Rc<Slice> {
132         self.as_slice().into_rc()
133     }
134 }
135
136 impl Slice {
137     fn from_u8_slice(s: &[u8]) -> &Slice {
138         unsafe { mem::transmute(s) }
139     }
140
141     pub fn from_str(s: &str) -> &Slice {
142         Slice::from_u8_slice(s.as_bytes())
143     }
144
145     pub fn to_str(&self) -> Option<&str> {
146         str::from_utf8(&self.inner).ok()
147     }
148
149     pub fn to_string_lossy(&self) -> Cow<str> {
150         String::from_utf8_lossy(&self.inner)
151     }
152
153     pub fn to_owned(&self) -> Buf {
154         Buf { inner: self.inner.to_vec() }
155     }
156
157     #[inline]
158     pub fn into_box(&self) -> Box<Slice> {
159         let boxed: Box<[u8]> = self.inner.into();
160         unsafe { mem::transmute(boxed) }
161     }
162
163     pub fn empty_box() -> Box<Slice> {
164         let boxed: Box<[u8]> = Default::default();
165         unsafe { mem::transmute(boxed) }
166     }
167
168     #[inline]
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) }
172     }
173
174     #[inline]
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) }
178     }
179 }