| 1 | use std::borrow::Cow; |
| 2 | use std::ffi::CStr; |
| 3 | use std::ffi::CString; |
| 4 | use std::ops::Deref; |
| 5 | |
| 6 | #[derive(Debug)] |
| 7 | pub(super) enum ByteStrInner<'a> { |
| 8 | Bytes(&'a [u8]), |
| 9 | #[cfg(windows)] |
| 10 | Str(Cow<'a, str>), |
| 11 | } |
| 12 | |
| 13 | /// A value returned by [`ToBytes::to_bytes`]. |
| 14 | /// |
| 15 | /// This struct is usually initialized by calling the above method for |
| 16 | /// [`[u8]`][slice]. |
| 17 | #[derive(Debug)] |
| 18 | pub struct ByteStr<'a>(pub(super) ByteStrInner<'a>); |
| 19 | |
| 20 | #[cfg(any(doc, windows))] |
| 21 | #[cfg_attr(print_bytes_docs_rs, doc(cfg(windows)))] |
| 22 | impl<'a> ByteStr<'a> { |
| 23 | /// Wraps a byte string lossily. |
| 24 | /// |
| 25 | /// This method can be used to implement [`ToBytes::to_bytes`] when |
| 26 | /// [`ToBytes::to_wide`] is the better way to represent the string. |
| 27 | #[inline] |
| 28 | #[must_use] |
| 29 | pub fn from_utf8_lossy(string: &'a [u8]) -> Self { |
| 30 | Self(ByteStrInner::Str(String::from_utf8_lossy(string))) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// A value returned by [`ToBytes::to_wide`]. |
| 35 | #[cfg(any(doc, windows))] |
| 36 | #[cfg_attr(print_bytes_docs_rs, doc(cfg(windows)))] |
| 37 | #[derive(Debug)] |
| 38 | pub struct WideStr(pub(super) Vec<u16>); |
| 39 | |
| 40 | #[cfg(any(doc, windows))] |
| 41 | impl WideStr { |
| 42 | /// Wraps a wide character string. |
| 43 | /// |
| 44 | /// This method can be used to implement [`ToBytes::to_wide`]. |
| 45 | #[inline] |
| 46 | #[must_use] |
| 47 | pub fn new(string: Vec<u16>) -> Self { |
| 48 | Self(string) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// Represents a type similarly to [`Display`]. |
| 53 | /// |
| 54 | /// Implement this trait to allow printing a type that cannot guarantee UTF-8 |
| 55 | /// output. It is used to bound values accepted by functions in this crate. |
| 56 | /// |
| 57 | /// # Examples |
| 58 | /// |
| 59 | /// ``` |
| 60 | /// use print_bytes::println_lossy; |
| 61 | /// use print_bytes::ByteStr; |
| 62 | /// use print_bytes::ToBytes; |
| 63 | /// #[cfg(windows)] |
| 64 | /// use print_bytes::WideStr; |
| 65 | /// |
| 66 | /// struct ByteSlice<'a>(&'a [u8]); |
| 67 | /// |
| 68 | /// impl ToBytes for ByteSlice<'_> { |
| 69 | /// fn to_bytes(&self) -> ByteStr<'_> { |
| 70 | /// self.0.to_bytes() |
| 71 | /// } |
| 72 | /// |
| 73 | /// #[cfg(windows)] |
| 74 | /// fn to_wide(&self) -> Option<WideStr> { |
| 75 | /// self.0.to_wide() |
| 76 | /// } |
| 77 | /// } |
| 78 | /// |
| 79 | /// println_lossy(&ByteSlice(b"Hello, world!")); |
| 80 | /// ``` |
| 81 | /// |
| 82 | /// [`Display`]: ::std::fmt::Display |
| 83 | /// [`to_bytes`]: Self::to_bytes |
| 84 | /// [`ToString`]: ::std::string::ToString |
| 85 | pub trait ToBytes { |
| 86 | /// Returns a byte string that will be used to represent the instance. |
| 87 | #[must_use] |
| 88 | fn to_bytes(&self) -> ByteStr<'_>; |
| 89 | |
| 90 | /// Returns a wide character string that will be used to represent the |
| 91 | /// instance. |
| 92 | /// |
| 93 | /// The Windows API frequently uses wide character strings. This method |
| 94 | /// allows them to be printed losslessly in some cases, even when they |
| 95 | /// cannot be converted to UTF-8. |
| 96 | /// |
| 97 | /// Returning [`None`] causes [`to_bytes`] to be used instead. |
| 98 | /// |
| 99 | /// [`to_bytes`]: Self::to_bytes |
| 100 | #[cfg(any(doc, windows))] |
| 101 | #[cfg_attr(print_bytes_docs_rs, doc(cfg(windows)))] |
| 102 | #[must_use] |
| 103 | fn to_wide(&self) -> Option<WideStr>; |
| 104 | } |
| 105 | |
| 106 | impl ToBytes for [u8] { |
| 107 | #[inline] |
| 108 | fn to_bytes(&self) -> ByteStr<'_> { |
| 109 | ByteStr(ByteStrInner::Bytes(self)) |
| 110 | } |
| 111 | |
| 112 | #[cfg(any(doc, windows))] |
| 113 | #[inline] |
| 114 | fn to_wide(&self) -> Option<WideStr> { |
| 115 | None |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | macro_rules! defer_methods { |
| 120 | ( $convert_method:ident ) => { |
| 121 | #[inline] |
| 122 | fn to_bytes(&self) -> ByteStr<'_> { |
| 123 | ToBytes::to_bytes(self.$convert_method()) |
| 124 | } |
| 125 | |
| 126 | #[cfg(any(doc, windows))] |
| 127 | #[inline] |
| 128 | fn to_wide(&self) -> Option<WideStr> { |
| 129 | self.$convert_method().to_wide() |
| 130 | } |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | impl<const N: usize> ToBytes for [u8; N] { |
| 135 | defer_methods!(as_slice); |
| 136 | } |
| 137 | |
| 138 | impl<T> ToBytes for Cow<'_, T> |
| 139 | where |
| 140 | T: ?Sized + ToBytes + ToOwned, |
| 141 | T::Owned: ToBytes, |
| 142 | { |
| 143 | defer_methods!(deref); |
| 144 | } |
| 145 | |
| 146 | macro_rules! defer_impl { |
| 147 | ( $type:ty , $convert_method:ident ) => { |
| 148 | impl ToBytes for $type { |
| 149 | defer_methods!($convert_method); |
| 150 | } |
| 151 | }; |
| 152 | } |
| 153 | defer_impl!(CStr, to_bytes); |
| 154 | defer_impl!(CString, as_c_str); |
| 155 | defer_impl!(Vec<u8>, as_slice); |
| 156 | |
| 157 | #[cfg(any( |
| 158 | all(target_vendor = "fortanix", target_env = "sgx"), |
| 159 | target_os = "hermit", |
| 160 | target_os = "solid_asp3", |
| 161 | target_os = "wasi", |
| 162 | target_os = "xous", |
| 163 | unix, |
| 164 | windows, |
| 165 | ))] |
| 166 | mod os_str { |
| 167 | use std::ffi::OsStr; |
| 168 | use std::ffi::OsString; |
| 169 | use std::path::Path; |
| 170 | use std::path::PathBuf; |
| 171 | |
| 172 | use super::ByteStr; |
| 173 | use super::ToBytes; |
| 174 | #[cfg(any(doc, windows))] |
| 175 | use super::WideStr; |
| 176 | |
| 177 | impl ToBytes for OsStr { |
| 178 | #[inline] |
| 179 | fn to_bytes(&self) -> ByteStr<'_> { |
| 180 | #[cfg(windows)] |
| 181 | { |
| 182 | use super::ByteStrInner; |
| 183 | |
| 184 | ByteStr(ByteStrInner::Str(self.to_string_lossy())) |
| 185 | } |
| 186 | #[cfg(not(windows))] |
| 187 | { |
| 188 | #[cfg(all( |
| 189 | target_vendor = "fortanix", |
| 190 | target_env = "sgx", |
| 191 | ))] |
| 192 | use std::os::fortanix_sgx as os; |
| 193 | #[cfg(target_os = "hermit")] |
| 194 | use std::os::hermit as os; |
| 195 | #[cfg(target_os = "solid_asp3")] |
| 196 | use std::os::solid as os; |
| 197 | #[cfg(unix)] |
| 198 | use std::os::unix as os; |
| 199 | #[cfg(target_os = "wasi")] |
| 200 | use std::os::wasi as os; |
| 201 | #[cfg(target_os = "xous")] |
| 202 | use std::os::xous as os; |
| 203 | |
| 204 | use os::ffi::OsStrExt; |
| 205 | |
| 206 | self.as_bytes().to_bytes() |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #[cfg(any(doc, windows))] |
| 211 | #[inline] |
| 212 | fn to_wide(&self) -> Option<WideStr> { |
| 213 | #[cfg(windows)] |
| 214 | use std::os::windows::ffi::OsStrExt; |
| 215 | |
| 216 | Some(WideStr(self.encode_wide().collect())) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | defer_impl!(OsString, as_os_str); |
| 221 | defer_impl!(Path, as_os_str); |
| 222 | defer_impl!(PathBuf, as_path); |
| 223 | } |