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