1 use std::borrow::Cow;
2 use std::ffi::OsStr;
3 use std::ffi::OsString;
4 
5 #[cfg(any(target_os = "hermit", target_os = "redox", unix))]
6 use std::os::unix as os;
7 #[cfg(any(target_env = "wasi", target_os = "wasi"))]
8 use std::os::wasi as os;
9 
10 use os::ffi::OsStrExt;
11 use os::ffi::OsStringExt;
12 
13 use super::EncodingError;
14 use super::OsStrBytes;
15 use super::OsStringBytes;
16 
17 if_raw! {
18     pub(super) mod raw;
19 }
20 
21 impl OsStrBytes for OsStr {
22     #[inline]
from_bytes<TString>( string: &TString, ) -> Result<Cow<'_, Self>, EncodingError> where TString: AsRef<[u8]> + ?Sized,23     fn from_bytes<TString>(
24         string: &TString,
25     ) -> Result<Cow<'_, Self>, EncodingError>
26     where
27         TString: AsRef<[u8]> + ?Sized,
28     {
29         Ok(Cow::Borrowed(OsStrExt::from_bytes(string.as_ref())))
30     }
31 
32     #[inline]
to_bytes(&self) -> Cow<'_, [u8]>33     fn to_bytes(&self) -> Cow<'_, [u8]> {
34         Cow::Borrowed(OsStrExt::as_bytes(self))
35     }
36 }
37 
38 impl OsStringBytes for OsString {
39     #[inline]
from_bytes<TString>(string: TString) -> Result<Self, EncodingError> where TString: AsRef<[u8]>,40     fn from_bytes<TString>(string: TString) -> Result<Self, EncodingError>
41     where
42         TString: AsRef<[u8]>,
43     {
44         <OsStr as OsStrBytes>::from_bytes(&string).map(Cow::into_owned)
45     }
46 
47     #[inline]
from_vec(string: Vec<u8>) -> Result<Self, EncodingError>48     fn from_vec(string: Vec<u8>) -> Result<Self, EncodingError> {
49         Ok(OsStringExt::from_vec(string))
50     }
51 
52     #[inline]
into_vec(self) -> Vec<u8>53     fn into_vec(self) -> Vec<u8> {
54         OsStringExt::into_vec(self)
55     }
56 }
57