]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
1 | /// The underlying OsString/OsStr implementation on Windows is a |
2 | /// wrapper around the "WTF-8" encoding; see the `wtf8` module for more. | |
532ac7d7 | 3 | use crate::borrow::Cow; |
a2a8927a | 4 | use crate::collections::TryReserveError; |
532ac7d7 | 5 | use crate::fmt; |
532ac7d7 XL |
6 | use crate::mem; |
7 | use crate::rc::Rc; | |
8 | use crate::sync::Arc; | |
60c5eb7d XL |
9 | use crate::sys_common::wtf8::{Wtf8, Wtf8Buf}; |
10 | use crate::sys_common::{AsInner, FromInner, IntoInner}; | |
85aaf69f SL |
11 | |
12 | #[derive(Clone, Hash)] | |
13 | pub struct Buf { | |
60c5eb7d | 14 | pub inner: Wtf8Buf, |
85aaf69f SL |
15 | } |
16 | ||
7453a54e SL |
17 | impl IntoInner<Wtf8Buf> for Buf { |
18 | fn into_inner(self) -> Wtf8Buf { | |
19 | self.inner | |
20 | } | |
21 | } | |
22 | ||
ff7c6d11 XL |
23 | impl FromInner<Wtf8Buf> for Buf { |
24 | fn from_inner(inner: Wtf8Buf) -> Self { | |
25 | Buf { inner } | |
26 | } | |
27 | } | |
28 | ||
7453a54e SL |
29 | impl AsInner<Wtf8> for Buf { |
30 | fn as_inner(&self) -> &Wtf8 { | |
31 | &self.inner | |
32 | } | |
33 | } | |
34 | ||
041b39d2 | 35 | impl fmt::Debug for Buf { |
532ac7d7 | 36 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 XL |
37 | fmt::Debug::fmt(self.as_slice(), formatter) |
38 | } | |
39 | } | |
40 | ||
41 | impl fmt::Display for Buf { | |
532ac7d7 | 42 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 | 43 | fmt::Display::fmt(self.as_slice(), formatter) |
85aaf69f SL |
44 | } |
45 | } | |
46 | ||
04454e1e | 47 | #[repr(transparent)] |
85aaf69f | 48 | pub struct Slice { |
60c5eb7d | 49 | pub inner: Wtf8, |
85aaf69f SL |
50 | } |
51 | ||
041b39d2 | 52 | impl fmt::Debug for Slice { |
532ac7d7 | 53 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 XL |
54 | fmt::Debug::fmt(&self.inner, formatter) |
55 | } | |
56 | } | |
57 | ||
58 | impl fmt::Display for Slice { | |
532ac7d7 | 59 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
041b39d2 | 60 | fmt::Display::fmt(&self.inner, formatter) |
85aaf69f SL |
61 | } |
62 | } | |
63 | ||
64 | impl Buf { | |
7453a54e | 65 | pub fn with_capacity(capacity: usize) -> Buf { |
60c5eb7d | 66 | Buf { inner: Wtf8Buf::with_capacity(capacity) } |
7453a54e SL |
67 | } |
68 | ||
69 | pub fn clear(&mut self) { | |
70 | self.inner.clear() | |
71 | } | |
72 | ||
73 | pub fn capacity(&self) -> usize { | |
74 | self.inner.capacity() | |
75 | } | |
76 | ||
85aaf69f SL |
77 | pub fn from_string(s: String) -> Buf { |
78 | Buf { inner: Wtf8Buf::from_string(s) } | |
79 | } | |
80 | ||
85aaf69f | 81 | pub fn as_slice(&self) -> &Slice { |
1b1a35ee | 82 | // SAFETY: Slice is just a wrapper for Wtf8, |
ba9703b0 XL |
83 | // and self.inner.as_slice() returns &Wtf8. |
84 | // Therefore, transmuting &Wtf8 to &Slice is safe. | |
85aaf69f SL |
85 | unsafe { mem::transmute(self.inner.as_slice()) } |
86 | } | |
87 | ||
ba9703b0 | 88 | pub fn as_mut_slice(&mut self) -> &mut Slice { |
1b1a35ee | 89 | // SAFETY: Slice is just a wrapper for Wtf8, |
ba9703b0 XL |
90 | // and self.inner.as_mut_slice() returns &mut Wtf8. |
91 | // Therefore, transmuting &mut Wtf8 to &mut Slice is safe. | |
92 | // Additionally, care should be taken to ensure the slice | |
93 | // is always valid Wtf8. | |
94 | unsafe { mem::transmute(self.inner.as_mut_slice()) } | |
95 | } | |
96 | ||
85aaf69f SL |
97 | pub fn into_string(self) -> Result<String, Buf> { |
98 | self.inner.into_string().map_err(|buf| Buf { inner: buf }) | |
99 | } | |
100 | ||
101 | pub fn push_slice(&mut self, s: &Slice) { | |
102 | self.inner.push_wtf8(&s.inner) | |
103 | } | |
7453a54e SL |
104 | |
105 | pub fn reserve(&mut self, additional: usize) { | |
106 | self.inner.reserve(additional) | |
107 | } | |
108 | ||
a2a8927a XL |
109 | pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { |
110 | self.inner.try_reserve(additional) | |
111 | } | |
112 | ||
7453a54e SL |
113 | pub fn reserve_exact(&mut self, additional: usize) { |
114 | self.inner.reserve_exact(additional) | |
115 | } | |
8bb4bdeb | 116 | |
a2a8927a XL |
117 | pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { |
118 | self.inner.try_reserve_exact(additional) | |
119 | } | |
120 | ||
8bb4bdeb XL |
121 | pub fn shrink_to_fit(&mut self) { |
122 | self.inner.shrink_to_fit() | |
123 | } | |
124 | ||
0531ce1d XL |
125 | #[inline] |
126 | pub fn shrink_to(&mut self, min_capacity: usize) { | |
127 | self.inner.shrink_to(min_capacity) | |
128 | } | |
129 | ||
8bb4bdeb XL |
130 | #[inline] |
131 | pub fn into_box(self) -> Box<Slice> { | |
132 | unsafe { mem::transmute(self.inner.into_box()) } | |
133 | } | |
cc61c64b XL |
134 | |
135 | #[inline] | |
136 | pub fn from_box(boxed: Box<Slice>) -> Buf { | |
137 | let inner: Box<Wtf8> = unsafe { mem::transmute(boxed) }; | |
138 | Buf { inner: Wtf8Buf::from_box(inner) } | |
139 | } | |
ff7c6d11 XL |
140 | |
141 | #[inline] | |
142 | pub fn into_arc(&self) -> Arc<Slice> { | |
143 | self.as_slice().into_arc() | |
144 | } | |
145 | ||
146 | #[inline] | |
147 | pub fn into_rc(&self) -> Rc<Slice> { | |
148 | self.as_slice().into_rc() | |
149 | } | |
85aaf69f SL |
150 | } |
151 | ||
152 | impl Slice { | |
60c5eb7d | 153 | #[inline] |
85aaf69f SL |
154 | pub fn from_str(s: &str) -> &Slice { |
155 | unsafe { mem::transmute(Wtf8::from_str(s)) } | |
156 | } | |
157 | ||
158 | pub fn to_str(&self) -> Option<&str> { | |
159 | self.inner.as_str() | |
160 | } | |
161 | ||
532ac7d7 | 162 | pub fn to_string_lossy(&self) -> Cow<'_, str> { |
85aaf69f SL |
163 | self.inner.to_string_lossy() |
164 | } | |
165 | ||
166 | pub fn to_owned(&self) -> Buf { | |
167 | let mut buf = Wtf8Buf::with_capacity(self.inner.len()); | |
168 | buf.push_wtf8(&self.inner); | |
169 | Buf { inner: buf } | |
170 | } | |
8bb4bdeb | 171 | |
ba9703b0 XL |
172 | pub fn clone_into(&self, buf: &mut Buf) { |
173 | self.inner.clone_into(&mut buf.inner) | |
174 | } | |
175 | ||
8bb4bdeb XL |
176 | #[inline] |
177 | pub fn into_box(&self) -> Box<Slice> { | |
178 | unsafe { mem::transmute(self.inner.into_box()) } | |
179 | } | |
180 | ||
181 | pub fn empty_box() -> Box<Slice> { | |
182 | unsafe { mem::transmute(Wtf8::empty_box()) } | |
183 | } | |
ff7c6d11 XL |
184 | |
185 | #[inline] | |
186 | pub fn into_arc(&self) -> Arc<Slice> { | |
187 | let arc = self.inner.into_arc(); | |
188 | unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) } | |
189 | } | |
190 | ||
191 | #[inline] | |
192 | pub fn into_rc(&self) -> Rc<Slice> { | |
193 | let rc = self.inner.into_rc(); | |
194 | unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) } | |
195 | } | |
ba9703b0 XL |
196 | |
197 | #[inline] | |
198 | pub fn make_ascii_lowercase(&mut self) { | |
199 | self.inner.make_ascii_lowercase() | |
200 | } | |
201 | ||
202 | #[inline] | |
203 | pub fn make_ascii_uppercase(&mut self) { | |
204 | self.inner.make_ascii_uppercase() | |
205 | } | |
206 | ||
207 | #[inline] | |
208 | pub fn to_ascii_lowercase(&self) -> Buf { | |
209 | Buf { inner: self.inner.to_ascii_lowercase() } | |
210 | } | |
211 | ||
212 | #[inline] | |
213 | pub fn to_ascii_uppercase(&self) -> Buf { | |
214 | Buf { inner: self.inner.to_ascii_uppercase() } | |
215 | } | |
216 | ||
217 | #[inline] | |
218 | pub fn is_ascii(&self) -> bool { | |
219 | self.inner.is_ascii() | |
220 | } | |
221 | ||
222 | #[inline] | |
223 | pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool { | |
224 | self.inner.eq_ignore_ascii_case(&other.inner) | |
225 | } | |
85aaf69f | 226 | } |