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