1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
use std::borrow::Cow; use std::ffi::OsStr; use std::ffi::OsString; use crate::EncodingError; use crate::OsStrBytes; use crate::OsStringBytes; #[inline] fn from_bytes(string: &[u8]) -> Cow<'_, OsStr> { Cow::Borrowed(::std::os::unix::ffi::OsStrExt::from_bytes(string)) } #[inline] fn from_vec(string: Vec<u8>) -> OsString { ::std::os::unix::ffi::OsStringExt::from_vec(string) } impl OsStrBytes for OsStr { #[inline] fn from_bytes(string: &[u8]) -> Result<Cow<'_, Self>, EncodingError> { Ok(from_bytes(string)) } #[inline] unsafe fn from_bytes_unchecked(string: &[u8]) -> Cow<'_, Self> { from_bytes(string) } #[inline] fn to_bytes(&self) -> Cow<'_, [u8]> { Cow::Borrowed(::std::os::unix::ffi::OsStrExt::as_bytes(self)) } } impl OsStringBytes for OsString { #[inline] fn from_bytes<TString>(string: TString) -> Result<Self, EncodingError> where TString: AsRef<[u8]>, { Ok(from_bytes(string.as_ref()).into_owned()) } #[inline] unsafe fn from_bytes_unchecked<TString>(string: TString) -> Self where TString: AsRef<[u8]>, { from_bytes(string.as_ref()).into_owned() } #[inline] fn from_vec(string: Vec<u8>) -> Result<Self, EncodingError> { Ok(from_vec(string)) } #[inline] unsafe fn from_vec_unchecked(string: Vec<u8>) -> Self { from_vec(string) } #[inline] fn into_vec(self) -> Vec<u8> { ::std::os::unix::ffi::OsStringExt::into_vec(self) } }