std/backtrace/src/
types.rs1cfg_if::cfg_if! {
4    if #[cfg(feature = "std")] {
5        use std::borrow::Cow;
6        use std::fmt;
7        use std::path::PathBuf;
8        use std::prelude::v1::*;
9        use std::str;
10    }
11}
12
13#[derive(Debug)]
17pub enum BytesOrWideString<'a> {
18    Bytes(&'a [u8]),
20    Wide(&'a [u16]),
22}
23
24#[cfg(feature = "std")]
25impl<'a> BytesOrWideString<'a> {
26    pub fn to_str_lossy(&self) -> Cow<'a, str> {
34        use self::BytesOrWideString::*;
35
36        match *self {
37            Bytes(slice) => String::from_utf8_lossy(slice),
38            Wide(wide) => Cow::Owned(String::from_utf16_lossy(wide)),
39        }
40    }
41
42    pub fn into_path_buf(self) -> PathBuf {
49        #[cfg(unix)]
50        {
51            use std::ffi::OsStr;
52            use std::os::unix::ffi::OsStrExt;
53
54            if let BytesOrWideString::Bytes(slice) = self {
55                return PathBuf::from(OsStr::from_bytes(slice));
56            }
57        }
58
59        #[cfg(windows)]
60        {
61            use std::ffi::OsString;
62            use std::os::windows::ffi::OsStringExt;
63
64            if let BytesOrWideString::Wide(slice) = self {
65                return PathBuf::from(OsString::from_wide(slice));
66            }
67        }
68
69        if let BytesOrWideString::Bytes(b) = self {
70            if let Ok(s) = str::from_utf8(b) {
71                return PathBuf::from(s);
72            }
73        }
74        unreachable!()
75    }
76}
77
78#[cfg(feature = "std")]
79impl<'a> fmt::Display for BytesOrWideString<'a> {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        self.to_str_lossy().fmt(f)
82    }
83}