]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/os_str.rs
Auto merge of #33814 - lambda:rtabort-use-platform-abort, 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 string::String;
18 use result::Result;
19 use option::Option;
20 use mem;
21 use sys_common::{AsInner, IntoInner};
22
23 #[derive(Clone, Hash)]
24 pub struct Buf {
25     pub inner: Wtf8Buf
26 }
27
28 impl IntoInner<Wtf8Buf> for Buf {
29     fn into_inner(self) -> Wtf8Buf {
30         self.inner
31     }
32 }
33
34 impl AsInner<Wtf8> for Buf {
35     fn as_inner(&self) -> &Wtf8 {
36         &self.inner
37     }
38 }
39
40 impl Debug for Buf {
41     fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
42         self.as_slice().fmt(formatter)
43     }
44 }
45
46 pub struct Slice {
47     pub inner: Wtf8
48 }
49
50 impl Debug for Slice {
51     fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
52         self.inner.fmt(formatter)
53     }
54 }
55
56 impl Buf {
57     pub fn with_capacity(capacity: usize) -> Buf {
58         Buf {
59             inner: Wtf8Buf::with_capacity(capacity)
60         }
61     }
62
63     pub fn clear(&mut self) {
64         self.inner.clear()
65     }
66
67     pub fn capacity(&self) -> usize {
68         self.inner.capacity()
69     }
70
71     pub fn from_string(s: String) -> Buf {
72         Buf { inner: Wtf8Buf::from_string(s) }
73     }
74
75     pub fn as_slice(&self) -> &Slice {
76         unsafe { mem::transmute(self.inner.as_slice()) }
77     }
78
79     pub fn into_string(self) -> Result<String, Buf> {
80         self.inner.into_string().map_err(|buf| Buf { inner: buf })
81     }
82
83     pub fn push_slice(&mut self, s: &Slice) {
84         self.inner.push_wtf8(&s.inner)
85     }
86
87     pub fn reserve(&mut self, additional: usize) {
88         self.inner.reserve(additional)
89     }
90
91     pub fn reserve_exact(&mut self, additional: usize) {
92         self.inner.reserve_exact(additional)
93     }
94 }
95
96 impl Slice {
97     pub fn from_str(s: &str) -> &Slice {
98         unsafe { mem::transmute(Wtf8::from_str(s)) }
99     }
100
101     pub fn to_str(&self) -> Option<&str> {
102         self.inner.as_str()
103     }
104
105     pub fn to_string_lossy(&self) -> Cow<str> {
106         self.inner.to_string_lossy()
107     }
108
109     pub fn to_owned(&self) -> Buf {
110         let mut buf = Wtf8Buf::with_capacity(self.inner.len());
111         buf.push_wtf8(&self.inner);
112         Buf { inner: buf }
113     }
114 }