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.
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.
11 //! Unix-specific extension to the primitives in the `std::ffi` module
13 #![stable(feature = "rust1", since = "1.0.0")]
15 use ffi::{OsStr, OsString};
19 use sys_common::{FromInner, IntoInner, AsInner};
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;
28 /// Yields the underlying byte vector of this `OsString`.
29 #[stable(feature = "rust1", since = "1.0.0")]
30 fn into_vec(self) -> Vec<u8>;
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 })
38 fn into_vec(self) -> Vec<u8> {
39 self.into_inner().inner
43 /// Unix-specific extensions to `OsStr`.
44 #[stable(feature = "rust1", since = "1.0.0")]
46 #[stable(feature = "rust1", since = "1.0.0")]
47 fn from_bytes(slice: &[u8]) -> &Self;
49 /// Gets the underlying byte view of the `OsStr` slice.
50 #[stable(feature = "rust1", since = "1.0.0")]
51 fn as_bytes(&self) -> &[u8];
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) }
59 fn as_bytes(&self) -> &[u8] {
60 &self.as_inner().inner