[−][src]Crate print_bytes
This crate allows printing broken UTF-8 bytes to an output stream as losslessly as possible.
Usually, paths are printed by calling Path::display
or
Path::to_string_lossy
beforehand. However, both of these methods are
always lossy; they misrepresent some valid paths in output. The same is
true when using String::from_utf8_lossy
to print any other UTF-8–like
byte sequence.
Instead, this crate only performs a lossy conversion when the output device
is known to require unicode, to make output as accurate as possible. When
necessary, any character sequence that cannot be represented will be
replaced with REPLACEMENT_CHARACTER
. That convention is shared with the
standard library, which uses the same character for its lossy conversion
functions.
Note: Windows Compatibility
OsStr
and related structs may be printed lossily on Windows. Paths are
not represented using bytes on that platform, so it may be confusing to
display them in that manner. Plus, the encoding most often used to account
for the difference is not permitted to be written to files, so it would not make sense for this crate to use it.
Windows Console can display these paths, so this crate will output them losslessly when writing to that terminal.
Features
These features are optional and can be enabled or disabled in a "Cargo.toml" file. Nightly features are unstable, since they rely on unstable Rust features.
Nightly Features
-
const_generics - Provides an implementation of
ToBytes
for[u8; N]
. As a result, it can be output using functions in this crate. -
specialization - Provides
write_bytes
.
Examples
use std::env; use print_bytes::println_bytes; print!("exe: "); println_bytes(&env::current_exe()?); println!(); println!("args:"); for arg in env::args_os().skip(1) { println_bytes(&arg); }
Structs
Bytes | A value that can be printed by any of the functions in this crate. |
Traits
ToBytes | Represents a type similarly to |
Functions
eprint_bytes | Prints a value to the standard error stream. |
eprintln_bytes | Prints a value to the standard error stream, followed by a newline. |
print_bytes | Prints a value to the standard output stream. |
println_bytes | Prints a value to the standard output stream, followed by a newline. |
write_bytes | feature="specialization" Writes a value to a "writer". |