-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
243 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::borrow::Cow; | ||
use std::convert::Infallible; | ||
use std::ffi::OsStr; | ||
use std::ffi::OsString; | ||
use std::result; | ||
|
||
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] | ||
use std::os::fortanix_sgx as os; | ||
#[cfg(target_os = "hermit")] | ||
use std::os::hermit as os; | ||
#[cfg(target_os = "solid_asp3")] | ||
use std::os::solid as os; | ||
#[cfg(unix)] | ||
use std::os::unix as os; | ||
#[cfg(target_os = "wasi")] | ||
use std::os::wasi as os; | ||
#[cfg(target_os = "xous")] | ||
use std::os::xous as os; | ||
|
||
use os::ffi::OsStrExt; | ||
use os::ffi::OsStringExt; | ||
|
||
pub(crate) type EncodingError = Infallible; | ||
|
||
pub(crate) type Result<T> = result::Result<T, EncodingError>; | ||
|
||
pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> { | ||
Ok(Cow::Borrowed(OsStrExt::from_bytes(string))) | ||
} | ||
|
||
pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> { | ||
Cow::Borrowed(OsStrExt::as_bytes(os_string)) | ||
} | ||
|
||
pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> { | ||
Ok(OsStringExt::from_vec(string)) | ||
} | ||
|
||
pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> { | ||
OsStringExt::into_vec(os_string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,7 @@ | ||
use std::borrow::Cow; | ||
use std::convert::Infallible; | ||
use std::ffi::OsStr; | ||
use std::ffi::OsString; | ||
use std::result; | ||
|
||
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] | ||
use std::os::fortanix_sgx as os; | ||
#[cfg(target_os = "hermit")] | ||
use std::os::hermit as os; | ||
#[cfg(target_os = "solid_asp3")] | ||
use std::os::solid as os; | ||
#[cfg(unix)] | ||
use std::os::unix as os; | ||
#[cfg(target_os = "wasi")] | ||
use std::os::wasi as os; | ||
#[cfg(target_os = "xous")] | ||
use std::os::xous as os; | ||
|
||
use os::ffi::OsStrExt; | ||
use os::ffi::OsStringExt; | ||
if_conversions! { | ||
pub(super) mod convert; | ||
} | ||
|
||
if_raw_str! { | ||
pub(super) mod raw; | ||
} | ||
|
||
pub(super) type EncodingError = Infallible; | ||
|
||
pub(super) type Result<T> = result::Result<T, EncodingError>; | ||
|
||
pub(super) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> { | ||
Ok(Cow::Borrowed(OsStrExt::from_bytes(string))) | ||
} | ||
|
||
pub(super) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> { | ||
Cow::Borrowed(OsStrExt::as_bytes(os_string)) | ||
} | ||
|
||
pub(super) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> { | ||
Ok(OsStringExt::from_vec(string)) | ||
} | ||
|
||
pub(super) fn os_string_into_vec(os_string: OsString) -> Vec<u8> { | ||
OsStringExt::into_vec(os_string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::borrow::Cow; | ||
use std::error::Error; | ||
use std::ffi::OsStr; | ||
use std::ffi::OsString; | ||
use std::fmt; | ||
use std::fmt::Display; | ||
use std::fmt::Formatter; | ||
use std::result; | ||
use std::str; | ||
use std::str::Utf8Error; | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub(crate) struct EncodingError(Utf8Error); | ||
|
||
impl Display for EncodingError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "os_str_bytes: {}", self.0) | ||
} | ||
} | ||
|
||
impl Error for EncodingError {} | ||
|
||
pub(crate) type Result<T> = result::Result<T, EncodingError>; | ||
|
||
macro_rules! expect_utf8 { | ||
( $result:expr ) => { | ||
$result.expect( | ||
"platform string contains invalid UTF-8, which should not be \ | ||
possible", | ||
) | ||
}; | ||
} | ||
|
||
fn from_bytes(string: &[u8]) -> Result<&str> { | ||
str::from_utf8(string).map_err(EncodingError) | ||
} | ||
|
||
pub(crate) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> { | ||
from_bytes(string).map(|x| Cow::Borrowed(OsStr::new(x))) | ||
} | ||
|
||
pub(crate) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> { | ||
Cow::Borrowed(expect_utf8!(os_string.to_str()).as_bytes()) | ||
} | ||
|
||
pub(crate) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> { | ||
String::from_utf8(string) | ||
.map(Into::into) | ||
.map_err(|x| EncodingError(x.utf8_error())) | ||
} | ||
|
||
pub(crate) fn os_string_into_vec(os_string: OsString) -> Vec<u8> { | ||
expect_utf8!(os_string.into_string()).into_bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,7 @@ | ||
use std::borrow::Cow; | ||
use std::error::Error; | ||
use std::ffi::OsStr; | ||
use std::ffi::OsString; | ||
use std::fmt; | ||
use std::fmt::Display; | ||
use std::fmt::Formatter; | ||
use std::result; | ||
use std::str; | ||
use std::str::Utf8Error; | ||
if_conversions! { | ||
pub(super) mod convert; | ||
} | ||
|
||
if_raw_str! { | ||
pub(super) mod raw; | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub(super) struct EncodingError(Utf8Error); | ||
|
||
impl Display for EncodingError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "os_str_bytes: {}", self.0) | ||
} | ||
} | ||
|
||
impl Error for EncodingError {} | ||
|
||
pub(super) type Result<T> = result::Result<T, EncodingError>; | ||
|
||
macro_rules! expect_utf8 { | ||
( $result:expr ) => { | ||
$result.expect( | ||
"platform string contains invalid UTF-8, which should not be \ | ||
possible", | ||
) | ||
}; | ||
} | ||
|
||
fn from_bytes(string: &[u8]) -> Result<&str> { | ||
str::from_utf8(string).map_err(EncodingError) | ||
} | ||
|
||
pub(super) fn os_str_from_bytes(string: &[u8]) -> Result<Cow<'_, OsStr>> { | ||
from_bytes(string).map(|x| Cow::Borrowed(OsStr::new(x))) | ||
} | ||
|
||
pub(super) fn os_str_to_bytes(os_string: &OsStr) -> Cow<'_, [u8]> { | ||
Cow::Borrowed(expect_utf8!(os_string.to_str()).as_bytes()) | ||
} | ||
|
||
pub(super) fn os_string_from_vec(string: Vec<u8>) -> Result<OsString> { | ||
String::from_utf8(string) | ||
.map(Into::into) | ||
.map_err(|x| EncodingError(x.utf8_error())) | ||
} | ||
|
||
pub(super) fn os_string_into_vec(os_string: OsString) -> Vec<u8> { | ||
expect_utf8!(os_string.into_string()).into_bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.