]>
git.proxmox.com Git - rustc.git/blob - vendor/os_str_bytes/src/wasm32/mod.rs
4 use std
::ffi
::OsString
;
7 use std
::fmt
::Formatter
;
10 use std
::str::Utf8Error
;
16 #[derive(Debug, Eq, PartialEq)]
17 pub(super) struct EncodingError(Utf8Error
);
19 impl Display
for EncodingError
{
20 fn fmt(&self, formatter
: &mut Formatter
<'_
>) -> fmt
::Result
{
21 write
!(formatter
, "os_str_bytes: {}", self.0)
25 impl Error
for EncodingError {}
27 type Result
<T
> = result
::Result
<T
, EncodingError
>;
29 macro_rules
! expect_utf8
{
32 "platform string contains invalid UTF-8, which should not be \
38 pub(super) fn os_str_from_bytes(string
: &[u8]) -> Result
<Cow
<'_
, OsStr
>> {
39 str::from_utf8(string
)
40 .map(|x
| Cow
::Borrowed(OsStr
::new(x
)))
41 .map_err(EncodingError
)
44 pub(super) fn os_str_to_bytes(os_string
: &OsStr
) -> Cow
<'_
, [u8]> {
45 Cow
::Borrowed(expect_utf8
!(os_string
.to_str()).as_bytes())
48 pub(super) fn os_string_from_vec(string
: Vec
<u8>) -> Result
<OsString
> {
49 String
::from_utf8(string
)
51 .map_err(|x
| EncodingError(x
.utf8_error()))
54 pub(super) fn os_string_into_vec(os_string
: OsString
) -> Vec
<u8> {
55 expect_utf8
!(os_string
.into_string()).into_bytes()