1 //! The underlying OsString/OsStr implementation on Unix and many other
2 //! systems: just a `Vec<u8>`/`[u8]`.
4 use crate::borrow
::Cow
;
11 use crate::sys_common
::{AsInner, IntoInner}
;
13 use core
::str::lossy
::{Utf8Lossy, Utf8LossyChunk}
;
29 impl fmt
::Debug
for Slice
{
30 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
31 // Writes out a valid unicode string with the correct escape sequences
33 formatter
.write_str("\"")?
;
34 for Utf8LossyChunk { valid, broken }
in Utf8Lossy
::from_bytes(&self.inner
).chunks() {
35 for c
in valid
.chars().flat_map(|c
| c
.escape_debug()) {
36 formatter
.write_char(c
)?
40 write
!(formatter
, "\\x{:02X}", b
)?
;
43 formatter
.write_str("\"")
47 impl fmt
::Display
for Slice
{
48 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
49 fmt
::Display
::fmt(&Utf8Lossy
::from_bytes(&self.inner
), formatter
)
53 impl fmt
::Debug
for Buf
{
54 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
55 fmt
::Debug
::fmt(self.as_slice(), formatter
)
59 impl fmt
::Display
for Buf
{
60 fn fmt(&self, formatter
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
61 fmt
::Display
::fmt(self.as_slice(), formatter
)
67 fn clone(&self) -> Self {
68 Buf { inner: self.inner.clone() }
72 fn clone_from(&mut self, source
: &Self) {
73 self.inner
.clone_from(&source
.inner
)
77 impl IntoInner
<Vec
<u8>> for Buf
{
78 fn into_inner(self) -> Vec
<u8> {
83 impl AsInner
<[u8]> for Buf
{
84 fn as_inner(&self) -> &[u8] {
90 pub fn from_string(s
: String
) -> Buf
{
91 Buf { inner: s.into_bytes() }
95 pub fn with_capacity(capacity
: usize) -> Buf
{
96 Buf { inner: Vec::with_capacity(capacity) }
100 pub fn clear(&mut self) {
105 pub fn capacity(&self) -> usize {
106 self.inner
.capacity()
110 pub fn reserve(&mut self, additional
: usize) {
111 self.inner
.reserve(additional
)
115 pub fn reserve_exact(&mut self, additional
: usize) {
116 self.inner
.reserve_exact(additional
)
120 pub fn shrink_to_fit(&mut self) {
121 self.inner
.shrink_to_fit()
125 pub fn shrink_to(&mut self, min_capacity
: usize) {
126 self.inner
.shrink_to(min_capacity
)
130 pub fn as_slice(&self) -> &Slice
{
131 // SAFETY: Slice just wraps [u8],
132 // and &*self.inner is &[u8], therefore
133 // transmuting &[u8] to &Slice is safe.
134 unsafe { mem::transmute(&*self.inner) }
138 pub fn as_mut_slice(&mut self) -> &mut Slice
{
139 // SAFETY: Slice just wraps [u8],
140 // and &mut *self.inner is &mut [u8], therefore
141 // transmuting &mut [u8] to &mut Slice is safe.
142 unsafe { mem::transmute(&mut *self.inner) }
145 pub fn into_string(self) -> Result
<String
, Buf
> {
146 String
::from_utf8(self.inner
).map_err(|p
| Buf { inner: p.into_bytes() }
)
149 pub fn push_slice(&mut self, s
: &Slice
) {
150 self.inner
.extend_from_slice(&s
.inner
)
154 pub fn into_box(self) -> Box
<Slice
> {
155 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
159 pub fn from_box(boxed
: Box
<Slice
>) -> Buf
{
160 let inner
: Box
<[u8]> = unsafe { mem::transmute(boxed) }
;
161 Buf { inner: inner.into_vec() }
165 pub fn into_arc(&self) -> Arc
<Slice
> {
166 self.as_slice().into_arc()
170 pub fn into_rc(&self) -> Rc
<Slice
> {
171 self.as_slice().into_rc()
177 fn from_u8_slice(s
: &[u8]) -> &Slice
{
178 unsafe { mem::transmute(s) }
182 pub fn from_str(s
: &str) -> &Slice
{
183 Slice
::from_u8_slice(s
.as_bytes())
186 pub fn to_str(&self) -> Option
<&str> {
187 str::from_utf8(&self.inner
).ok()
190 pub fn to_string_lossy(&self) -> Cow
<'_
, str> {
191 String
::from_utf8_lossy(&self.inner
)
194 pub fn to_owned(&self) -> Buf
{
195 Buf { inner: self.inner.to_vec() }
198 pub fn clone_into(&self, buf
: &mut Buf
) {
199 self.inner
.clone_into(&mut buf
.inner
)
203 pub fn into_box(&self) -> Box
<Slice
> {
204 let boxed
: Box
<[u8]> = self.inner
.into();
205 unsafe { mem::transmute(boxed) }
208 pub fn empty_box() -> Box
<Slice
> {
209 let boxed
: Box
<[u8]> = Default
::default();
210 unsafe { mem::transmute(boxed) }
214 pub fn into_arc(&self) -> Arc
<Slice
> {
215 let arc
: Arc
<[u8]> = Arc
::from(&self.inner
);
216 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
220 pub fn into_rc(&self) -> Rc
<Slice
> {
221 let rc
: Rc
<[u8]> = Rc
::from(&self.inner
);
222 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }
226 pub fn make_ascii_lowercase(&mut self) {
227 self.inner
.make_ascii_lowercase()
231 pub fn make_ascii_uppercase(&mut self) {
232 self.inner
.make_ascii_uppercase()
236 pub fn to_ascii_lowercase(&self) -> Buf
{
237 Buf { inner: self.inner.to_ascii_lowercase() }
241 pub fn to_ascii_uppercase(&self) -> Buf
{
242 Buf { inner: self.inner.to_ascii_uppercase() }
246 pub fn is_ascii(&self) -> bool
{
247 self.inner
.is_ascii()
251 pub fn eq_ignore_ascii_case(&self, other
: &Self) -> bool
{
252 self.inner
.eq_ignore_ascii_case(&other
.inner
)