Crate try_ascii

source ·
Expand description

§Try ASCII

Helper to format byte slices that probably/mostly contain ASCII-encoded text

This can be used to log binary data which is expected to be text but which is not guaranteed to be valid UTF-8. In these cases, it can be unfeasible to use std::str::from_utf8 because whenever that function fails, an Utf8Error would be logged instead of the actual data. Using try_ascii, the text parts are formatted as text while the binary parts fall back to the default Debug implementation for &[u8]:

use try_ascii::try_ascii;
println!("{:x?}", try_ascii(b"Hello, \xa0\xa1\xa2\xa3"));

Prints as:

Hello, [a0, a1, a2, a3]

§License

Open Logistics Foundation License
Version 1.3, January 2023

See the LICENSE file in the top-level directory.

§Contact

Fraunhofer IML Embedded Rust Group - embedded-rust@iml.fraunhofer.de

Structs§

  • Wrapper type that contains the byte slice and implements Debug

Functions§

  • Helper function to format the text parts of a byte slice as text and the non-text parts with the default Debug implementation for byte slices