| 1 |
//! This crate allows printing broken UTF-8 bytes to an output stream as |
| 2 |
//! losslessly as possible. |
| 3 |
//! |
| 4 |
//! Usually, paths are printed by calling [`Path::display`] or |
| 5 |
//! [`Path::to_string_lossy`] beforehand. However, both of these methods are |
| 6 |
//! always lossy; they misrepresent some valid paths in output. The same is |
| 7 |
//! true when using [`String::from_utf8_lossy`] to print any other |
| 8 |
//! UTF-8–like byte sequence. |
| 9 |
//! |
| 10 |
//! Instead, this crate only performs a lossy conversion when the output device |
| 11 |
//! is known to require Unicode, to make output as accurate as possible. When |
| 12 |
//! necessary, any character sequence that cannot be represented will be |
| 13 |
//! replaced with [`REPLACEMENT_CHARACTER`]. That convention is shared with the |
| 14 |
//! standard library, which uses the same character for its lossy conversion |
| 15 |
//! functions. |
| 16 |
//! |
| 17 |
//! ### Note: Windows Compatibility |
| 18 |
//! |
| 19 |
//! [`OsStr`] and related structs may be printed lossily on Windows. Paths are |
| 20 |
//! not represented using bytes on that platform, so it may be confusing to |
| 21 |
//! display them in that manner. Plus, the encoding most often used to account |
| 22 |
//! for the difference is [not permitted to be written to files][wtf-8 |
| 23 |
//! audience], so it would not make sense for this crate to use it. |
| 24 |
//! |
| 25 |
//! Windows Console can display these paths, so this crate will output them |
| 26 |
//! losslessly when writing to that terminal. |
| 27 |
//! |
| 28 |
//! # Features |
| 29 |
//! |
| 30 |
//! These features are optional and can be enabled or disabled in a |
| 31 |
//! "Cargo.toml" file. Nightly features are unstable, since they rely on |
| 32 |
//! unstable Rust features. |
| 33 |
//! |
| 34 |
//! ### Nightly Features |
| 35 |
//! |
| 36 |
//! - **specialization** - |
| 37 |
//! Provides an implementation of [`WriteLossy`] for all types. |
| 38 |
//! |
| 39 |
//! # Examples |
| 40 |
//! |
| 41 |
//! ``` |
| 42 |
//! use std::env; |
| 43 |
//! # use std::io; |
| 44 |
//! |
| 45 |
//! use print_bytes::println_lossy; |
| 46 |
//! |
| 47 |
//! print!("exe: "); |
| 48 |
//! println_lossy(&env::current_exe()?); |
| 49 |
//! println!(); |
| 50 |
//! |
| 51 |
//! println!("args:"); |
| 52 |
//! for arg in env::args_os().skip(1) { |
| 53 |
//! println_lossy(&arg); |
| 54 |
//! } |
| 55 |
//! # |
| 56 |
//! # Ok::<_, io::Error>(()) |
| 57 |
//! ``` |
| 58 |
//! |
| 59 |
//! [`OsStr`]: ::std::ffi::OsStr |
| 60 |
//! [`Path::display`]: ::std::path::Path::display |
| 61 |
//! [`Path::to_string_lossy`]: ::std::path::Path::to_string_lossy |
| 62 |
//! [`REPLACEMENT_CHARACTER`]: ::std::char::REPLACEMENT_CHARACTER |
| 63 |
//! [wtf-8 audience]: https://simonsapin.github.io/wtf-8/#intended-audience |
| 64 |
|
| 65 |
#![cfg_attr(feature = "specialization", allow(incomplete_features))] |
| 66 |
// Only require a nightly compiler when building documentation for docs.rs. |
| 67 |
// This is a private option that should not be used. |
| 68 |
// https://github.com/rust-lang/docs.rs/issues/147#issuecomment-389544407 |
| 69 |
#![cfg_attr(print_bytes_docs_rs, feature(doc_cfg))] |
| 70 |
// Nightly is also currently required for the SGX platform. |
| 71 |
#![cfg_attr( |
| 72 |
all(target_vendor = "fortanix", target_env = "sgx"), |
| 73 |
feature(sgx_platform) |
| 74 |
)] |
| 75 |
#![cfg_attr(feature = "specialization", feature(specialization))] |
| 76 |
#![warn(unused_results)] |
| 77 |
|
| 78 |
use std::io; |
| 79 |
use std::io::Write; |
| 80 |
|
| 81 |
mod bytes; |
| 82 |
pub use bytes::ByteStr; |
| 83 |
use bytes::ByteStrInner; |
| 84 |
pub use bytes::ToBytes; |
| 85 |
#[cfg(any(doc, windows))] |
| 86 |
pub use bytes::WideStr; |
| 87 |
|
| 88 |
#[cfg(windows)] |
| 89 |
mod console; |
| 90 |
|
| 91 |
#[cfg_attr(test, macro_use)] |
| 92 |
mod writer; |
| 93 |
pub use writer::WriteLossy; |
| 94 |
|
| 95 |
#[cfg(test)] |
| 96 |
mod tests; |
| 97 |
|
| 98 |
/// Writes a value to a "writer". |
| 99 |
/// |
| 100 |
/// This function is similar to [`write!`] but does not take a format |
| 101 |
/// parameter. |
| 102 |
/// |
| 103 |
/// For more information, see [the module-level documentation][module]. |
| 104 |
/// |
| 105 |
/// # Errors |
| 106 |
/// |
| 107 |
/// Returns an error if writing to the writer fails. |
| 108 |
/// |
| 109 |
/// # Examples |
| 110 |
/// |
| 111 |
/// ``` |
| 112 |
/// use std::env; |
| 113 |
/// use std::ffi::OsStr; |
| 114 |
/// |
| 115 |
/// use print_bytes::write_lossy; |
| 116 |
/// |
| 117 |
/// let string = "foobar"; |
| 118 |
/// let os_string = OsStr::new(string); |
| 119 |
/// |
| 120 |
/// let mut lossy_string = Vec::new(); |
| 121 |
/// write_lossy(&mut lossy_string, os_string) |
| 122 |
/// .expect("failed writing to vector"); |
| 123 |
/// assert_eq!(string.as_bytes(), lossy_string); |
| 124 |
/// ``` |
| 125 |
/// |
| 126 |
/// [module]: self |
| 127 |
#[inline] |
| 128 |
pub fn write_lossy<T, W>(mut writer: W, value: &T) -> io::Result<()> |
| 129 |
where |
| 130 |
T: ?Sized + ToBytes, |
| 131 |
W: Write + WriteLossy, |
| 132 |
{ |
| 133 |
#[cfg_attr(not(windows), allow(unused_mut))] |
| 134 |
let mut lossy = false; |
| 135 |
#[cfg(windows)] |
| 136 |
if let Some(mut console) = writer.__to_console() { |
| 137 |
if let Some(string) = value.to_wide() { |
| 138 |
return console.write_wide_all(&string.0); |
| 139 |
} |
| 140 |
lossy = true; |
| 141 |
} |
| 142 |
|
| 143 |
let buffer; |
| 144 |
let string = value.to_bytes(); |
| 145 |
let string = match &string.0 { |
| 146 |
ByteStrInner::Bytes(string) => { |
| 147 |
if lossy { |
| 148 |
buffer = String::from_utf8_lossy(string); |
| 149 |
buffer.as_bytes() |
| 150 |
} else { |
| 151 |
string |
| 152 |
} |
| 153 |
} |
| 154 |
#[cfg(windows)] |
| 155 |
ByteStrInner::Str(string) => string.as_bytes(), |
| 156 |
}; |
| 157 |
writer.write_all(string) |
| 158 |
} |
| 159 |
|
| 160 |
macro_rules! expect_print { |
| 161 |
( $label:literal , $result:expr ) => { |
| 162 |
$result |
| 163 |
.unwrap_or_else(|x| panic!("failed writing to {}: {}", $label, x)) |
| 164 |
}; |
| 165 |
} |
| 166 |
|
| 167 |
macro_rules! r#impl { |
| 168 |
( |
| 169 |
$writer:expr , |
| 170 |
$(#[ $print_fn_attr:meta ])* $print_fn:ident , |
| 171 |
$(#[ $println_fn_attr:meta ])* $println_fn:ident , |
| 172 |
$label:literal $(,)? |
| 173 |
) => { |
| 174 |
#[inline] |
| 175 |
$(#[$print_fn_attr])* |
| 176 |
pub fn $print_fn<T>(value: &T) |
| 177 |
where |
| 178 |
T: ?Sized + ToBytes, |
| 179 |
{ |
| 180 |
expect_print!($label, write_lossy($writer, value)); |
| 181 |
} |
| 182 |
|
| 183 |
#[inline] |
| 184 |
$(#[$println_fn_attr])* |
| 185 |
pub fn $println_fn<T>(value: &T) |
| 186 |
where |
| 187 |
T: ?Sized + ToBytes, |
| 188 |
{ |
| 189 |
let mut writer = $writer.lock(); |
| 190 |
expect_print!($label, write_lossy(&mut writer, value)); |
| 191 |
expect_print!($label, writer.write_all(b"\n")); |
| 192 |
} |
| 193 |
}; |
| 194 |
} |
| 195 |
r#impl!( |
| 196 |
io::stderr(), |
| 197 |
/// Prints a value to the standard error stream. |
| 198 |
/// |
| 199 |
/// This function is similar to [`eprint!`] but does not take a format |
| 200 |
/// parameter. |
| 201 |
/// |
| 202 |
/// For more information, see [the module-level documentation][module]. |
| 203 |
/// |
| 204 |
/// # Panics |
| 205 |
/// |
| 206 |
/// Panics if writing to the stream fails. |
| 207 |
/// |
| 208 |
/// # Examples |
| 209 |
/// |
| 210 |
/// ``` |
| 211 |
/// use std::env; |
| 212 |
/// # use std::io; |
| 213 |
/// |
| 214 |
/// use print_bytes::eprint_lossy; |
| 215 |
/// |
| 216 |
/// eprint_lossy(&env::current_exe()?); |
| 217 |
/// # |
| 218 |
/// # Ok::<_, io::Error>(()) |
| 219 |
/// ``` |
| 220 |
/// |
| 221 |
/// [module]: self |
| 222 |
eprint_lossy, |
| 223 |
/// Prints a value to the standard error stream, followed by a newline. |
| 224 |
/// |
| 225 |
/// This function is similar to [`eprintln!`] but does not take a format |
| 226 |
/// parameter. |
| 227 |
/// |
| 228 |
/// For more information, see [the module-level documentation][module]. |
| 229 |
/// |
| 230 |
/// # Panics |
| 231 |
/// |
| 232 |
/// Panics if writing to the stream fails. |
| 233 |
/// |
| 234 |
/// # Examples |
| 235 |
/// |
| 236 |
/// ``` |
| 237 |
/// use std::env; |
| 238 |
/// # use std::io; |
| 239 |
/// |
| 240 |
/// use print_bytes::eprintln_lossy; |
| 241 |
/// |
| 242 |
/// eprintln_lossy(&env::current_exe()?); |
| 243 |
/// # |
| 244 |
/// # Ok::<_, io::Error>(()) |
| 245 |
/// ``` |
| 246 |
/// |
| 247 |
/// [module]: self |
| 248 |
eprintln_lossy, |
| 249 |
"stderr", |
| 250 |
); |
| 251 |
r#impl!( |
| 252 |
io::stdout(), |
| 253 |
/// Prints a value to the standard output stream. |
| 254 |
/// |
| 255 |
/// This function is similar to [`print!`] but does not take a format |
| 256 |
/// parameter. |
| 257 |
/// |
| 258 |
/// For more information, see [the module-level documentation][module]. |
| 259 |
/// |
| 260 |
/// # Panics |
| 261 |
/// |
| 262 |
/// Panics if writing to the stream fails. |
| 263 |
/// |
| 264 |
/// # Examples |
| 265 |
/// |
| 266 |
/// ``` |
| 267 |
/// use std::env; |
| 268 |
/// # use std::io; |
| 269 |
/// |
| 270 |
/// use print_bytes::print_lossy; |
| 271 |
/// |
| 272 |
/// print_lossy(&env::current_exe()?); |
| 273 |
/// # |
| 274 |
/// # Ok::<_, io::Error>(()) |
| 275 |
/// ``` |
| 276 |
/// |
| 277 |
/// [module]: self |
| 278 |
print_lossy, |
| 279 |
/// Prints a value to the standard output stream, followed by a newline. |
| 280 |
/// |
| 281 |
/// This function is similar to [`println!`] but does not take a format |
| 282 |
/// parameter. |
| 283 |
/// |
| 284 |
/// For more information, see [the module-level documentation][module]. |
| 285 |
/// |
| 286 |
/// # Panics |
| 287 |
/// |
| 288 |
/// Panics if writing to the stream fails. |
| 289 |
/// |
| 290 |
/// # Examples |
| 291 |
/// |
| 292 |
/// ``` |
| 293 |
/// use std::env; |
| 294 |
/// # use std::io; |
| 295 |
/// |
| 296 |
/// use print_bytes::println_lossy; |
| 297 |
/// |
| 298 |
/// println_lossy(&env::current_exe()?); |
| 299 |
/// # |
| 300 |
/// # Ok::<_, io::Error>(()) |
| 301 |
/// ``` |
| 302 |
/// |
| 303 |
/// [module]: self |
| 304 |
println_lossy, |
| 305 |
"stdout", |
| 306 |
); |