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