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]`.
15 use fmt
::{self, Debug}
;
21 #[derive(Clone, Hash)]
30 impl Debug
for Slice
{
31 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
32 self.to_string_lossy().fmt(formatter
)
37 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> Result
<(), fmt
::Error
> {
38 self.as_slice().fmt(formatter
)
43 pub fn from_string(s
: String
) -> Buf
{
44 Buf { inner: s.into_bytes() }
47 pub fn as_slice(&self) -> &Slice
{
48 unsafe { mem::transmute(&*self.inner) }
51 pub fn into_string(self) -> Result
<String
, Buf
> {
52 String
::from_utf8(self.inner
).map_err(|p
| Buf { inner: p.into_bytes() }
)
55 pub fn push_slice(&mut self, s
: &Slice
) {
56 self.inner
.extend_from_slice(&s
.inner
)
61 fn from_u8_slice(s
: &[u8]) -> &Slice
{
62 unsafe { mem::transmute(s) }
65 pub fn from_str(s
: &str) -> &Slice
{
66 Slice
::from_u8_slice(s
.as_bytes())
69 pub fn to_str(&self) -> Option
<&str> {
70 str::from_utf8(&self.inner
).ok()
73 pub fn to_string_lossy(&self) -> Cow
<str> {
74 String
::from_utf8_lossy(&self.inner
)
77 pub fn to_owned(&self) -> Buf
{
78 Buf { inner: self.inner.to_vec() }