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;
8 use crate::sys_common::wtf8::{Wtf8, Wtf8Buf};
9 use crate::sys_common::{AsInner, FromInner, IntoInner};
11 #[derive(Clone, Hash)]
16 impl IntoInner<Wtf8Buf> for Buf {
17 fn into_inner(self) -> Wtf8Buf {
22 impl FromInner<Wtf8Buf> for Buf {
23 fn from_inner(inner: Wtf8Buf) -> Self {
28 impl AsInner<Wtf8> for Buf {
29 fn as_inner(&self) -> &Wtf8 {
34 impl fmt::Debug for Buf {
35 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
36 fmt::Debug::fmt(self.as_slice(), formatter)
40 impl fmt::Display for Buf {
41 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
42 fmt::Display::fmt(self.as_slice(), formatter)
50 impl fmt::Debug for Slice {
51 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52 fmt::Debug::fmt(&self.inner, formatter)
56 impl fmt::Display for Slice {
57 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
58 fmt::Display::fmt(&self.inner, formatter)
63 pub fn with_capacity(capacity: usize) -> Buf {
64 Buf { inner: Wtf8Buf::with_capacity(capacity) }
67 pub fn clear(&mut self) {
71 pub fn capacity(&self) -> usize {
75 pub fn from_string(s: String) -> Buf {
76 Buf { inner: Wtf8Buf::from_string(s) }
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()) }
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()) }
95 pub fn into_string(self) -> Result<String, Buf> {
96 self.inner.into_string().map_err(|buf| Buf { inner: buf })
99 pub fn push_slice(&mut self, s: &Slice) {
100 self.inner.push_wtf8(&s.inner)
103 pub fn reserve(&mut self, additional: usize) {
104 self.inner.reserve(additional)
107 pub fn reserve_exact(&mut self, additional: usize) {
108 self.inner.reserve_exact(additional)
111 pub fn shrink_to_fit(&mut self) {
112 self.inner.shrink_to_fit()
116 pub fn shrink_to(&mut self, min_capacity: usize) {
117 self.inner.shrink_to(min_capacity)
121 pub fn into_box(self) -> Box<Slice> {
122 unsafe { mem::transmute(self.inner.into_box()) }
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) }
132 pub fn into_arc(&self) -> Arc<Slice> {
133 self.as_slice().into_arc()
137 pub fn into_rc(&self) -> Rc<Slice> {
138 self.as_slice().into_rc()
144 pub fn from_str(s: &str) -> &Slice {
145 unsafe { mem::transmute(Wtf8::from_str(s)) }
148 pub fn to_str(&self) -> Option<&str> {
152 pub fn to_string_lossy(&self) -> Cow<'_, str> {
153 self.inner.to_string_lossy()
156 pub fn to_owned(&self) -> Buf {
157 let mut buf = Wtf8Buf::with_capacity(self.inner.len());
158 buf.push_wtf8(&self.inner);
162 pub fn clone_into(&self, buf: &mut Buf) {
163 self.inner.clone_into(&mut buf.inner)
167 pub fn into_box(&self) -> Box<Slice> {
168 unsafe { mem::transmute(self.inner.into_box()) }
171 pub fn empty_box() -> Box<Slice> {
172 unsafe { mem::transmute(Wtf8::empty_box()) }
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) }
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) }
188 pub fn make_ascii_lowercase(&mut self) {
189 self.inner.make_ascii_lowercase()
193 pub fn make_ascii_uppercase(&mut self) {
194 self.inner.make_ascii_uppercase()
198 pub fn to_ascii_lowercase(&self) -> Buf {
199 Buf { inner: self.inner.to_ascii_lowercase() }
203 pub fn to_ascii_uppercase(&self) -> Buf {
204 Buf { inner: self.inner.to_ascii_uppercase() }
208 pub fn is_ascii(&self) -> bool {
209 self.inner.is_ascii()
213 pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
214 self.inner.eq_ignore_ascii_case(&other.inner)