1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /// The underlying OsString/OsStr implementation on Unix systems: just
12 /// a `Vec<u8>`/`[u8]`.
20 use sys_common
::{AsInner, IntoInner}
;
21 use sys_common
::bytestring
::debug_fmt_bytestring
;
22 use std_unicode
::lossy
::Utf8Lossy
;
24 #[derive(Clone, Hash)]
33 impl fmt
::Debug
for Slice
{
34 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
35 debug_fmt_bytestring(&self.inner
, formatter
)
39 impl fmt
::Display
for Slice
{
40 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
41 fmt
::Display
::fmt(&Utf8Lossy
::from_bytes(&self.inner
), formatter
)
45 impl fmt
::Debug
for Buf
{
46 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
47 fmt
::Debug
::fmt(self.as_slice(), formatter
)
51 impl fmt
::Display
for Buf
{
52 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
53 fmt
::Display
::fmt(self.as_slice(), formatter
)
57 impl IntoInner
<Vec
<u8>> for Buf
{
58 fn into_inner(self) -> Vec
<u8> {
63 impl AsInner
<[u8]> for Buf
{
64 fn as_inner(&self) -> &[u8] {
71 pub fn from_string(s
: String
) -> Buf
{
72 Buf { inner: s.into_bytes() }
76 pub fn with_capacity(capacity
: usize) -> Buf
{
78 inner
: Vec
::with_capacity(capacity
)
83 pub fn clear(&mut self) {
88 pub fn capacity(&self) -> usize {
93 pub fn reserve(&mut self, additional
: usize) {
94 self.inner
.reserve(additional
)
98 pub fn reserve_exact(&mut self, additional
: usize) {
99 self.inner
.reserve_exact(additional
)
103 pub fn shrink_to_fit(&mut self) {
104 self.inner
.shrink_to_fit()
108 pub fn shrink_to(&mut self, min_capacity
: usize) {
109 self.inner
.shrink_to(min_capacity
)
112 pub fn as_slice(&self) -> &Slice
{
113 unsafe { mem::transmute(&*self.inner) }
116 pub fn into_string(self) -> Result
<String
, Buf
> {
117 String
::from_utf8(self.inner
).map_err(|p
| Buf { inner: p.into_bytes() }
)
120 pub fn push_slice(&mut self, s
: &Slice
) {
121 self.inner
.extend_from_slice(&s
.inner
)
125 pub fn into_box(self) -> Box
<Slice
> {
126 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
130 pub fn from_box(boxed
: Box
<Slice
>) -> Buf
{
131 let inner
: Box
<[u8]> = unsafe { mem::transmute(boxed) }
;
132 Buf { inner: inner.into_vec() }
136 pub fn into_arc(&self) -> Arc
<Slice
> {
137 self.as_slice().into_arc()
141 pub fn into_rc(&self) -> Rc
<Slice
> {
142 self.as_slice().into_rc()
147 fn from_u8_slice(s
: &[u8]) -> &Slice
{
148 unsafe { mem::transmute(s) }
151 pub fn from_str(s
: &str) -> &Slice
{
152 Slice
::from_u8_slice(s
.as_bytes())
155 pub fn to_str(&self) -> Option
<&str> {
156 str::from_utf8(&self.inner
).ok()
159 pub fn to_string_lossy(&self) -> Cow
<str> {
160 String
::from_utf8_lossy(&self.inner
)
163 pub fn to_owned(&self) -> Buf
{
164 Buf { inner: self.inner.to_vec() }
168 pub fn into_box(&self) -> Box
<Slice
> {
169 let boxed
: Box
<[u8]> = self.inner
.into();
170 unsafe { mem::transmute(boxed) }
173 pub fn empty_box() -> Box
<Slice
> {
174 let boxed
: Box
<[u8]> = Default
::default();
175 unsafe { mem::transmute(boxed) }
179 pub fn into_arc(&self) -> Arc
<Slice
> {
180 let arc
: Arc
<[u8]> = Arc
::from(&self.inner
);
181 unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Slice) }
185 pub fn into_rc(&self) -> Rc
<Slice
> {
186 let rc
: Rc
<[u8]> = Rc
::from(&self.inner
);
187 unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Slice) }