Format-bytes
This crate exposes procedural macros that allow you to format bytestrings. For more background on why you would want to do that, read this article.
Usage
Add this to your Cargo.toml:
[]
= "0.4"
Requires Rust 1.85 or later. See the changelog.
Then use the macro like so:
use format_bytes;
See more examples of how it works on the documentation of
format_bytes! itself.
Arguments can be of any type that implements DisplayBytes: bytestrings,
integers and floats, Utf8 for std::fmt::Display types, join() for
sequences, and references and smart pointers to those.
Writing to a stream: write_bytes!
write_bytes! takes a &mut to the stream to write to, followed by the same
arguments as format_bytes!:
use write_bytes;
use io;
The stream can be any std::io::Write, or any Sink.
Formatting later: message!
message! takes the same arguments as format_bytes!, but returns a
Message that keeps the format string and the arguments, to be written
later, for example by an error handler. Arguments are moved into the message,
so they must be 'static, Send and Sync.
use ;
A Message is itself a DisplayBytes argument. Message::write_to
writes it to a Sink, and Message::template gives the format string,
parsed at compile time.
Custom output: Sink
A Sink receives the format string separately from each argument and its
name (ArgName). Every std::io::Write is a Sink that writes both
as-is. Other sinks can, for example, tell user data apart from the message
around it:
use ;
use io;
/// Quotes arguments
;
A sink that cares about types for structured output can call
DisplayBytes::visit with a Visit, which implements methods for
integers, UTF-8 text, bytes, a join() sequence or a nested Message.
Missing features
format!-like explicit indices ({0}) and format specs ({:?}, padding): if the need manifests itself, they might appear#[no_std]support
Why not 1.0?
Not until the macro gets a bit of mileage (it will be used in Mercurial).