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};
20 use sys_common::{AsInner, IntoInner};
22 #[derive(Clone, Hash)]
31 impl Debug for Slice {
32 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
33 self.to_string_lossy().fmt(formatter)
38 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
39 self.as_slice().fmt(formatter)
43 impl IntoInner<Vec<u8>> for Buf {
44 fn into_inner(self) -> Vec<u8> {
49 impl AsInner<[u8]> for Buf {
50 fn as_inner(&self) -> &[u8] {
57 pub fn from_string(s: String) -> Buf {
58 Buf { inner: s.into_bytes() }
62 pub fn with_capacity(capacity: usize) -> Buf {
64 inner: Vec::with_capacity(capacity)
69 pub fn clear(&mut self) {
74 pub fn capacity(&self) -> usize {
79 pub fn reserve(&mut self, additional: usize) {
80 self.inner.reserve(additional)
84 pub fn reserve_exact(&mut self, additional: usize) {
85 self.inner.reserve_exact(additional)
88 pub fn as_slice(&self) -> &Slice {
89 unsafe { mem::transmute(&*self.inner) }
92 pub fn into_string(self) -> Result<String, Buf> {
93 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
96 pub fn push_slice(&mut self, s: &Slice) {
97 self.inner.extend_from_slice(&s.inner)
102 fn from_u8_slice(s: &[u8]) -> &Slice {
103 unsafe { mem::transmute(s) }
106 pub fn from_str(s: &str) -> &Slice {
107 Slice::from_u8_slice(s.as_bytes())
110 pub fn to_str(&self) -> Option<&str> {
111 str::from_utf8(&self.inner).ok()
114 pub fn to_string_lossy(&self) -> Cow<str> {
115 String::from_utf8_lossy(&self.inner)
118 pub fn to_owned(&self) -> Buf {
119 Buf { inner: self.inner.to_vec() }