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