]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/os_str.rs
Auto merge of #23809 - cmr:issue-21310, r=Manishearth
[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
22 #[derive(Clone, Hash)]
23 pub struct Buf {
24     pub inner: Wtf8Buf
25 }
26
27 impl Debug for Buf {
28     fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
29         self.as_slice().fmt(formatter)
30     }
31 }
32
33 pub struct Slice {
34     pub inner: Wtf8
35 }
36
37 impl Debug for Slice {
38     fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
39         self.inner.fmt(formatter)
40     }
41 }
42
43 impl Buf {
44     pub fn from_string(s: String) -> Buf {
45         Buf { inner: Wtf8Buf::from_string(s) }
46     }
47
48     pub fn as_slice(&self) -> &Slice {
49         unsafe { mem::transmute(self.inner.as_slice()) }
50     }
51
52     pub fn into_string(self) -> Result<String, Buf> {
53         self.inner.into_string().map_err(|buf| Buf { inner: buf })
54     }
55
56     pub fn push_slice(&mut self, s: &Slice) {
57         self.inner.push_wtf8(&s.inner)
58     }
59 }
60
61 impl Slice {
62     pub fn from_str(s: &str) -> &Slice {
63         unsafe { mem::transmute(Wtf8::from_str(s)) }
64     }
65
66     pub fn to_str(&self) -> Option<&str> {
67         self.inner.as_str()
68     }
69
70     pub fn to_string_lossy(&self) -> Cow<str> {
71         self.inner.to_string_lossy()
72     }
73
74     pub fn to_owned(&self) -> Buf {
75         let mut buf = Wtf8Buf::with_capacity(self.inner.len());
76         buf.push_wtf8(&self.inner);
77         Buf { inner: buf }
78     }
79 }