]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/os_str.rs
New upstream version 1.26.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;
041b39d2 15use fmt;
85aaf69f 16use str;
85aaf69f 17use mem;
ff7c6d11
XL
18use rc::Rc;
19use sync::Arc;
7453a54e 20use sys_common::{AsInner, IntoInner};
ff7c6d11 21use sys_common::bytestring::debug_fmt_bytestring;
041b39d2 22use std_unicode::lossy::Utf8Lossy;
85aaf69f
SL
23
24#[derive(Clone, Hash)]
25pub struct Buf {
26 pub inner: Vec<u8>
27}
28
29pub struct Slice {
30 pub inner: [u8]
31}
32
041b39d2
XL
33impl fmt::Debug for Slice {
34 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
ff7c6d11 35 debug_fmt_bytestring(&self.inner, formatter)
85aaf69f
SL
36 }
37}
38
041b39d2
XL
39impl fmt::Display for Slice {
40 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
41 fmt::Display::fmt(&Utf8Lossy::from_bytes(&self.inner), formatter)
42 }
43}
44
45impl fmt::Debug for Buf {
46 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
47 fmt::Debug::fmt(self.as_slice(), formatter)
48 }
49}
50
51impl fmt::Display for Buf {
52 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
53 fmt::Display::fmt(self.as_slice(), formatter)
85aaf69f
SL
54 }
55}
56
7453a54e
SL
57impl IntoInner<Vec<u8>> for Buf {
58 fn into_inner(self) -> Vec<u8> {
59 self.inner
60 }
61}
62
63impl AsInner<[u8]> for Buf {
64 fn as_inner(&self) -> &[u8] {
65 &self.inner
66 }
67}
68
69
85aaf69f
SL
70impl Buf {
71 pub fn from_string(s: String) -> Buf {
72 Buf { inner: s.into_bytes() }
73 }
74
7453a54e
SL
75 #[inline]
76 pub fn with_capacity(capacity: usize) -> Buf {
77 Buf {
78 inner: Vec::with_capacity(capacity)
79 }
80 }
81
82 #[inline]
83 pub fn clear(&mut self) {
84 self.inner.clear()
85 }
86
87 #[inline]
88 pub fn capacity(&self) -> usize {
89 self.inner.capacity()
90 }
91
92 #[inline]
93 pub fn reserve(&mut self, additional: usize) {
94 self.inner.reserve(additional)
95 }
96
97 #[inline]
98 pub fn reserve_exact(&mut self, additional: usize) {
99 self.inner.reserve_exact(additional)
100 }
101
8bb4bdeb
XL
102 #[inline]
103 pub fn shrink_to_fit(&mut self) {
104 self.inner.shrink_to_fit()
105 }
106
0531ce1d
XL
107 #[inline]
108 pub fn shrink_to(&mut self, min_capacity: usize) {
109 self.inner.shrink_to(min_capacity)
110 }
111
85aaf69f
SL
112 pub fn as_slice(&self) -> &Slice {
113 unsafe { mem::transmute(&*self.inner) }
114 }
115
116 pub fn into_string(self) -> Result<String, Buf> {
117 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() } )
118 }
119
120 pub fn push_slice(&mut self, s: &Slice) {
92a42be0 121 self.inner.extend_from_slice(&s.inner)
85aaf69f 122 }
8bb4bdeb
XL
123
124 #[inline]
125 pub fn into_box(self) -> Box<Slice> {
126 unsafe { mem::transmute(self.inner.into_boxed_slice()) }
127 }
cc61c64b
XL
128
129 #[inline]
130 pub fn from_box(boxed: Box<Slice>) -> Buf {
131 let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
132 Buf { inner: inner.into_vec() }
133 }
ff7c6d11
XL
134
135 #[inline]
136 pub fn into_arc(&self) -> Arc<Slice> {
137 self.as_slice().into_arc()
138 }
139
140 #[inline]
141 pub fn into_rc(&self) -> Rc<Slice> {
142 self.as_slice().into_rc()
143 }
85aaf69f
SL
144}
145
146impl Slice {
147 fn from_u8_slice(s: &[u8]) -> &Slice {
148 unsafe { mem::transmute(s) }
149 }
150
151 pub fn from_str(s: &str) -> &Slice {
c34b1796 152 Slice::from_u8_slice(s.as_bytes())
85aaf69f
SL
153 }
154
155 pub fn to_str(&self) -> Option<&str> {
156 str::from_utf8(&self.inner).ok()
157 }
158
c34b1796 159 pub fn to_string_lossy(&self) -> Cow<str> {
85aaf69f
SL
160 String::from_utf8_lossy(&self.inner)
161 }
162
163 pub fn to_owned(&self) -> Buf {
164 Buf { inner: self.inner.to_vec() }
165 }
8bb4bdeb
XL
166
167 #[inline]
168 pub fn into_box(&self) -> Box<Slice> {
169 let boxed: Box<[u8]> = self.inner.into();
170 unsafe { mem::transmute(boxed) }
171 }
172
173 pub fn empty_box() -> Box<Slice> {
174 let boxed: Box<[u8]> = Default::default();
175 unsafe { mem::transmute(boxed) }
176 }
ff7c6d11
XL
177
178 #[inline]
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) }
182 }
183
184 #[inline]
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) }
188 }
85aaf69f 189}