]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/ffi.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / libstd / sys / unix / ext / ffi.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 //! Unix-specific extension to the primitives in the `std::ffi` module
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use ffi::{OsStr, OsString};
16 use mem;
17 use sys::os_str::Buf;
18 use sys_common::{FromInner, IntoInner, AsInner};
19
20 /// Unix-specific extensions to `OsString`.
21 #[stable(feature = "rust1", since = "1.0.0")]
22 pub trait OsStringExt {
23     /// Creates an `OsString` from a byte vector.
24     #[stable(feature = "rust1", since = "1.0.0")]
25     fn from_vec(vec: Vec<u8>) -> Self;
26
27     /// Yields the underlying byte vector of this `OsString`.
28     #[stable(feature = "rust1", since = "1.0.0")]
29     fn into_vec(self) -> Vec<u8>;
30 }
31
32 #[stable(feature = "rust1", since = "1.0.0")]
33 impl OsStringExt for OsString {
34     fn from_vec(vec: Vec<u8>) -> OsString {
35         FromInner::from_inner(Buf { inner: vec })
36     }
37     fn into_vec(self) -> Vec<u8> {
38         self.into_inner().inner
39     }
40 }
41
42 /// Unix-specific extensions to `OsStr`.
43 #[stable(feature = "rust1", since = "1.0.0")]
44 pub trait OsStrExt {
45     #[stable(feature = "rust1", since = "1.0.0")]
46     fn from_bytes(slice: &[u8]) -> &Self;
47
48     /// Gets the underlying byte view of the `OsStr` slice.
49     #[stable(feature = "rust1", since = "1.0.0")]
50     fn as_bytes(&self) -> &[u8];
51 }
52
53 #[stable(feature = "rust1", since = "1.0.0")]
54 impl OsStrExt for OsStr {
55     fn from_bytes(slice: &[u8]) -> &OsStr {
56         unsafe { mem::transmute(slice) }
57     }
58     fn as_bytes(&self) -> &[u8] {
59         &self.as_inner().inner
60     }
61 }