]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/os_str.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / libstd / sys / unix / os_str.rs
CommitLineData
85aaf69f
SL
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.
4//
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.
10
11/// The underlying OsString/OsStr implementation on Unix systems: just
12/// a `Vec<u8>`/`[u8]`.
13
c34b1796 14use borrow::Cow;
85aaf69f 15use fmt::{self, Debug};
85aaf69f 16use str;
85aaf69f 17use mem;
7453a54e 18use sys_common::{AsInner, IntoInner};
85aaf69f
SL
19
20#[derive(Clone, Hash)]
21pub struct Buf {
22 pub inner: Vec<u8>
23}
24
25pub struct Slice {
26 pub inner: [u8]
27}
28
29impl Debug for Slice {
30 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
31 self.to_string_lossy().fmt(formatter)
32 }
33}
34
35impl Debug for Buf {
36 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
37 self.as_slice().fmt(formatter)
38 }
39}
40
7453a54e
SL
41impl IntoInner<Vec<u8>> for Buf {
42 fn into_inner(self) -> Vec<u8> {
43 self.inner
44 }
45}
46
47impl AsInner<[u8]> for Buf {
48 fn as_inner(&self) -> &[u8] {
49 &self.inner
50 }
51}
52
53
85aaf69f
SL
54impl Buf {
55 pub fn from_string(s: String) -> Buf {
56 Buf { inner: s.into_bytes() }
57 }
58
7453a54e
SL
59 #[inline]
60 pub fn with_capacity(capacity: usize) -> Buf {
61 Buf {
62 inner: Vec::with_capacity(capacity)
63 }
64 }
65
66 #[inline]
67 pub fn clear(&mut self) {
68 self.inner.clear()
69 }
70
71 #[inline]
72 pub fn capacity(&self) -> usize {
73 self.inner.capacity()
74 }
75
76 #[inline]
77 pub fn reserve(&mut self, additional: usize) {
78 self.inner.reserve(additional)
79 }
80
81 #[inline]
82 pub fn reserve_exact(&mut self, additional: usize) {
83 self.inner.reserve_exact(additional)
84 }
85
85aaf69f
SL
86 pub fn as_slice(&self) -> &Slice {
87 unsafe { mem::transmute(&*self.inner) }
88 }
89
90 pub fn into_string(self) -> Result<String, Buf> {
91 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
92 }
93
94 pub fn push_slice(&mut self, s: &Slice) {
92a42be0 95 self.inner.extend_from_slice(&s.inner)
85aaf69f
SL
96 }
97}
98
99impl Slice {
100 fn from_u8_slice(s: &[u8]) -> &Slice {
101 unsafe { mem::transmute(s) }
102 }
103
104 pub fn from_str(s: &str) -> &Slice {
c34b1796 105 Slice::from_u8_slice(s.as_bytes())
85aaf69f
SL
106 }
107
108 pub fn to_str(&self) -> Option<&str> {
109 str::from_utf8(&self.inner).ok()
110 }
111
c34b1796 112 pub fn to_string_lossy(&self) -> Cow<str> {
85aaf69f
SL
113 String::from_utf8_lossy(&self.inner)
114 }
115
116 pub fn to_owned(&self) -> Buf {
117 Buf { inner: self.inner.to_vec() }
118 }
119}