]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/os_str.rs
Rollup merge of #40521 - TimNN:panic-free-shift, r=alexcrichton
[rust.git] / src / libstd / sys / windows / 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 Windows is a
12 /// wrapper around the "WTF-8" encoding; see the `wtf8` module for more.
13
14 use borrow::Cow;
15 use fmt::{self, Debug};
16 use sys_common::wtf8::{Wtf8, Wtf8Buf};
17 use mem;
18 use sys_common::{AsInner, IntoInner};
19
20 #[derive(Clone, Hash)]
21 pub struct Buf {
22     pub inner: Wtf8Buf
23 }
24
25 impl IntoInner<Wtf8Buf> for Buf {
26     fn into_inner(self) -> Wtf8Buf {
27         self.inner
28     }
29 }
30
31 impl AsInner<Wtf8> for Buf {
32     fn as_inner(&self) -> &Wtf8 {
33         &self.inner
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 pub struct Slice {
44     pub inner: Wtf8
45 }
46
47 impl Debug for Slice {
48     fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
49         self.inner.fmt(formatter)
50     }
51 }
52
53 impl Buf {
54     pub fn with_capacity(capacity: usize) -> Buf {
55         Buf {
56             inner: Wtf8Buf::with_capacity(capacity)
57         }
58     }
59
60     pub fn clear(&mut self) {
61         self.inner.clear()
62     }
63
64     pub fn capacity(&self) -> usize {
65         self.inner.capacity()
66     }
67
68     pub fn from_string(s: String) -> Buf {
69         Buf { inner: Wtf8Buf::from_string(s) }
70     }
71
72     pub fn as_slice(&self) -> &Slice {
73         unsafe { mem::transmute(self.inner.as_slice()) }
74     }
75
76     pub fn into_string(self) -> Result<String, Buf> {
77         self.inner.into_string().map_err(|buf| Buf { inner: buf })
78     }
79
80     pub fn push_slice(&mut self, s: &Slice) {
81         self.inner.push_wtf8(&s.inner)
82     }
83
84     pub fn reserve(&mut self, additional: usize) {
85         self.inner.reserve(additional)
86     }
87
88     pub fn reserve_exact(&mut self, additional: usize) {
89         self.inner.reserve_exact(additional)
90     }
91
92     pub fn shrink_to_fit(&mut self) {
93         self.inner.shrink_to_fit()
94     }
95
96     #[inline]
97     pub fn into_box(self) -> Box<Slice> {
98         unsafe { mem::transmute(self.inner.into_box()) }
99     }
100
101     #[inline]
102     pub fn from_box(boxed: Box<Slice>) -> Buf {
103         let inner: Box<Wtf8> = unsafe { mem::transmute(boxed) };
104         Buf { inner: Wtf8Buf::from_box(inner) }
105     }
106 }
107
108 impl Slice {
109     pub fn from_str(s: &str) -> &Slice {
110         unsafe { mem::transmute(Wtf8::from_str(s)) }
111     }
112
113     pub fn to_str(&self) -> Option<&str> {
114         self.inner.as_str()
115     }
116
117     pub fn to_string_lossy(&self) -> Cow<str> {
118         self.inner.to_string_lossy()
119     }
120
121     pub fn to_owned(&self) -> Buf {
122         let mut buf = Wtf8Buf::with_capacity(self.inner.len());
123         buf.push_wtf8(&self.inner);
124         Buf { inner: buf }
125     }
126
127     #[inline]
128     pub fn into_box(&self) -> Box<Slice> {
129         unsafe { mem::transmute(self.inner.into_box()) }
130     }
131
132     pub fn empty_box() -> Box<Slice> {
133         unsafe { mem::transmute(Wtf8::empty_box()) }
134     }
135 }