]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys/windows/os_str.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / sys / windows / os_str.rs
CommitLineData
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
XL
3use crate::borrow::Cow;
4use crate::fmt;
532ac7d7
XL
5use crate::mem;
6use crate::rc::Rc;
7use crate::sync::Arc;
60c5eb7d
XL
8use crate::sys_common::wtf8::{Wtf8, Wtf8Buf};
9use crate::sys_common::{AsInner, FromInner, IntoInner};
85aaf69f
SL
10
11#[derive(Clone, Hash)]
12pub struct Buf {
60c5eb7d 13 pub inner: Wtf8Buf,
85aaf69f
SL
14}
15
7453a54e
SL
16impl IntoInner<Wtf8Buf> for Buf {
17 fn into_inner(self) -> Wtf8Buf {
18 self.inner
19 }
20}
21
ff7c6d11
XL
22impl FromInner<Wtf8Buf> for Buf {
23 fn from_inner(inner: Wtf8Buf) -> Self {
24 Buf { inner }
25 }
26}
27
7453a54e
SL
28impl AsInner<Wtf8> for Buf {
29 fn as_inner(&self) -> &Wtf8 {
30 &self.inner
31 }
32}
33
041b39d2 34impl fmt::Debug for Buf {
532ac7d7 35 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
041b39d2
XL
36 fmt::Debug::fmt(self.as_slice(), formatter)
37 }
38}
39
40impl fmt::Display for Buf {
532ac7d7 41 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
041b39d2 42 fmt::Display::fmt(self.as_slice(), formatter)
85aaf69f
SL
43 }
44}
45
46pub struct Slice {
60c5eb7d 47 pub inner: Wtf8,
85aaf69f
SL
48}
49
041b39d2 50impl fmt::Debug for Slice {
532ac7d7 51 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
041b39d2
XL
52 fmt::Debug::fmt(&self.inner, formatter)
53 }
54}
55
56impl fmt::Display for Slice {
532ac7d7 57 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
041b39d2 58 fmt::Display::fmt(&self.inner, formatter)
85aaf69f
SL
59 }
60}
61
62impl Buf {
7453a54e 63 pub fn with_capacity(capacity: usize) -> Buf {
60c5eb7d 64 Buf { inner: Wtf8Buf::with_capacity(capacity) }
7453a54e
SL
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
85aaf69f
SL
75 pub fn from_string(s: String) -> Buf {
76 Buf { inner: Wtf8Buf::from_string(s) }
77 }
78
85aaf69f 79 pub fn as_slice(&self) -> &Slice {
1b1a35ee 80 // SAFETY: Slice is just a wrapper for Wtf8,
ba9703b0
XL
81 // and self.inner.as_slice() returns &Wtf8.
82 // Therefore, transmuting &Wtf8 to &Slice is safe.
85aaf69f
SL
83 unsafe { mem::transmute(self.inner.as_slice()) }
84 }
85
ba9703b0 86 pub fn as_mut_slice(&mut self) -> &mut Slice {
1b1a35ee 87 // SAFETY: Slice is just a wrapper for Wtf8,
ba9703b0
XL
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
85aaf69f
SL
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 }
7453a54e
SL
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 }
8bb4bdeb
XL
110
111 pub fn shrink_to_fit(&mut self) {
112 self.inner.shrink_to_fit()
113 }
114
0531ce1d
XL
115 #[inline]
116 pub fn shrink_to(&mut self, min_capacity: usize) {
117 self.inner.shrink_to(min_capacity)
118 }
119
8bb4bdeb
XL
120 #[inline]
121 pub fn into_box(self) -> Box<Slice> {
122 unsafe { mem::transmute(self.inner.into_box()) }
123 }
cc61c64b
XL
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 }
ff7c6d11
XL
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 }
85aaf69f
SL
140}
141
142impl Slice {
60c5eb7d 143 #[inline]
85aaf69f
SL
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
532ac7d7 152 pub fn to_string_lossy(&self) -> Cow<'_, str> {
85aaf69f
SL
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 }
8bb4bdeb 161
ba9703b0
XL
162 pub fn clone_into(&self, buf: &mut Buf) {
163 self.inner.clone_into(&mut buf.inner)
164 }
165
8bb4bdeb
XL
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 }
ff7c6d11
XL
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 }
ba9703b0
XL
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 }
85aaf69f 216}