| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#; |
| 5 | |
| 6 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 7 | pub(superrust-analyzer cargo print_bytes 1.2.0 crate/) enum ByteStrInnerrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#<'a> { |
| 8 | Bytesrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#Bytes#(&'a [u8]), |
| 9 | #[cfg(windows)] |
| 10 | Str(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/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 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 18 | pub struct ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStr#<'a>(pub(superrust-analyzer cargo print_bytes 1.2.0 crate/) ByteStrInnerrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#<'a>); |
| 19 | |
| 20 | #[cfg(any(doc, windows))] |
| 21 | #[cfg_attr(print_bytes_docs_rs, doc(cfg(windows)))] |
| 22 | impl<'a> ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/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(ByteStrInnerrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#::Str(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/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 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debug)] |
| 38 | pub struct WideStr(pub(superrust-analyzer cargo print_bytes 1.2.0 crate/) Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/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: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/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 ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes# { |
| 86 | /// Returns a byte string that will be used to represent the instance. |
| 87 | #[must_use] |
| 88 | fn to_bytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes#to_bytes().(&self) -> ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/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) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<WideStr>; |
| 104 | } |
| 105 | |
| 106 | impl ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes# for [u8] { |
| 107 | #[inline] |
| 108 | fn to_bytesrust-analyzer cargo print_bytes 1.2.0 bytes/impl#[`[u8]`][ToBytes]to_bytes().(&self) -> ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStr#<'_> { |
| 109 | ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStr#(ByteStrInnerrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#::Bytesrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#Bytes#(self)) |
| 110 | } |
| 111 | |
| 112 | #[cfg(any(doc, windows))] |
| 113 | #[inline] |
| 114 | fn to_wide(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<WideStr> { |
| 115 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | macro_rules! defer_methodsrust-analyzer cargo print_bytes 1.2.0 bytes/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> ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes# for [u8; N] { |
| 135 | defer_methodsrust-analyzer cargo print_bytes 1.2.0 bytes/defer_methods!!(as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().); |
| 136 | } |
| 137 | |
| 138 | impl<T> ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes# for Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, T> |
| 139 | where |
| 140 | T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes# + ToOwnedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#, |
| 141 | T::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#Owned#: ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes#, |
| 142 | { |
| 143 | defer_methodsrust-analyzer cargo print_bytes 1.2.0 bytes/defer_methods!!(derefrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().); |
| 144 | } |
| 145 | |
| 146 | macro_rules! defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl! { |
| 147 | ( $type:ty , $convert_method:ident ) => { |
| 148 | impl ToBytes for $type { |
| 149 | defer_methods!($convert_method); |
| 150 | } |
| 151 | }; |
| 152 | } |
| 153 | defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl!!(CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#, to_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/impl#[CStr]to_bytes().); |
| 154 | defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl!!(CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#, as_c_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]as_c_str().); |
| 155 | defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl!!(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]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_strrust-analyzer cargo print_bytes 1.2.0 bytes/os_str/ { |
| 167 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#; |
| 168 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#; |
| 169 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#; |
| 170 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#; |
| 171 | |
| 172 | use superrust-analyzer cargo print_bytes 1.2.0 bytes/::ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStr#; |
| 173 | use superrust-analyzer cargo print_bytes 1.2.0 bytes/::ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes#; |
| 174 | #[cfg(any(doc, windows))] |
| 175 | use superrust-analyzer cargo print_bytes 1.2.0 bytes/::WideStr; |
| 176 | |
| 177 | impl ToBytesrust-analyzer cargo print_bytes 1.2.0 bytes/ToBytes# for OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr# { |
| 178 | #[inline] |
| 179 | fn to_bytesrust-analyzer cargo print_bytes 1.2.0 bytes/os_str/impl#[OsStr][ToBytes]to_bytes().(&self) -> ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStr#<'_> { |
| 180 | #[cfg(windows)] |
| 181 | { |
| 182 | use superrust-analyzer cargo print_bytes 1.2.0 bytes/::ByteStrInnerrust-analyzer cargo print_bytes 1.2.0 bytes/ByteStrInner#; |
| 183 | |
| 184 | ByteStrrust-analyzer cargo print_bytes 1.2.0 bytes/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 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::fortanix_sgx as os; |
| 193 | #[cfg(target_os = "hermit")] |
| 194 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::hermit as os; |
| 195 | #[cfg(target_os = "solid_asp3")] |
| 196 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::solid as os; |
| 197 | #[cfg(unix)] |
| 198 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ as osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/; |
| 199 | #[cfg(target_os = "wasi")] |
| 200 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::wasi as os; |
| 201 | #[cfg(target_os = "xous")] |
| 202 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::xous as os; |
| 203 | |
| 204 | use osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/::OsStrExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/OsStrExt#; |
| 205 | |
| 206 | self.as_bytesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/ffi/os_str/impl#[OsStr][OsStrExt]as_bytes().().to_bytesrust-analyzer cargo print_bytes 1.2.0 bytes/impl#[`[u8]`][ToBytes]to_bytes().() |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #[cfg(any(doc, windows))] |
| 211 | #[inline] |
| 212 | fn to_wide(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<WideStr> { |
| 213 | #[cfg(windows)] |
| 214 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::windows::ffi::OsStrExt; |
| 215 | |
| 216 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(WideStr(selfrust-analyzer cargo print_bytes 1.2.0 bytes/os_str/.encode_wide().collect())) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl!!(OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#, as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString]as_os_str().); |
| 221 | defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl!!(Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, as_os_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]as_os_str().); |
| 222 | defer_implrust-analyzer cargo print_bytes 1.2.0 bytes/defer_impl!!(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, as_pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf]as_path().); |
| 223 | } |