Expand description
This crate provides newtype adaptors to and from any formatting trait. More specifically, it allows one to wrap a value into a filter wrapper which filters out the implementations of all formatting traits except for one and then wrap it into a adaptor wrapper which implements one specific formatting trait, which can be different from the original formatting trait. This is better demonstrated with an example:
// Let's create an example type...
#[derive(Copy, Clone, Debug)]
enum Sign {
Positive,
Negative
}
//...and implement a formatting trait by hand.
impl fmt::Display for Sign {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}",
match self {
Positive => '+',
Negative => '-'
}
)
}
}
let sign = Sign::Positive;
println!("The sign character is {}", sign); // Outputs the Display formatting
println!("The sign debug form is {:?}", sign); // Outputs the derived Debug formatting
let sign = DebugFromDisplay(sign); // Wrap it into a formatting adapter
println!("The sign debug form is {:?}", sign); // Outputs the Display formatting, even
// though we have {:?} as the formatting mode
let sign = UpperHexFromDebug(sign.0); // Get the value from the previous adapter
// and do something very random
println!("The sign in uppercase hexadecimal is `0x{:X}`", sign);All adapters in the crate are generated from a list of traits using a build script. As such, no manual editing of the adaptor delarations and their impl blocks is possible. Use cargo download and build the crate if you want to explore the generated results, or simply take a look at the build.rs file.
The crate is #![no_std], meaning that it works in a freestanding context and only depends on core::fmt, which requires a functional global allocator.
§Types of adapters
Here’s a list of adapter types (values between {} can be replaced by any formatting trait):
Only{trait}— filters out all formatting traits of a type except for{trait}. This can be used to restrict access to formatting traits when transferring objects between subsystems.{out_tr}From{in_tr}— implements{out_tr}by using the results of formatting with{in_tr}. This can be used to provide arbitrary formatting methods to interfaces which only accept one specific formatting method while still correctly implementing other formatting traits exactly as specified by thefmtdocumentation.
Structs§
- Debug
From Display - Provides
Debugformatting by using the result of invokingDisplayformatting. - Debug
From Lower Exp - Provides
Debugformatting by using the result of invokingLowerExpformatting. - Debug
From Lower Hex - Provides
Debugformatting by using the result of invokingLowerHexformatting. - Debug
From Octal - Provides
Debugformatting by using the result of invokingOctalformatting. - Debug
From Pointer - Provides
Debugformatting by using the result of invokingPointerformatting. - Debug
From Upper Exp - Provides
Debugformatting by using the result of invokingUpperExpformatting. - Debug
From Upper Hex - Provides
Debugformatting by using the result of invokingUpperHexformatting. - Display
From Debug - Provides
Displayformatting by using the result of invokingDebugformatting. - Display
From Lower Exp - Provides
Displayformatting by using the result of invokingLowerExpformatting. - Display
From Lower Hex - Provides
Displayformatting by using the result of invokingLowerHexformatting. - Display
From Octal - Provides
Displayformatting by using the result of invokingOctalformatting. - Display
From Pointer - Provides
Displayformatting by using the result of invokingPointerformatting. - Display
From Upper Exp - Provides
Displayformatting by using the result of invokingUpperExpformatting. - Display
From Upper Hex - Provides
Displayformatting by using the result of invokingUpperHexformatting. - Lower
ExpFrom Debug - Provides
LowerExpformatting by using the result of invokingDebugformatting. - Lower
ExpFrom Display - Provides
LowerExpformatting by using the result of invokingDisplayformatting. - Lower
ExpFrom Lower Hex - Provides
LowerExpformatting by using the result of invokingLowerHexformatting. - Lower
ExpFrom Octal - Provides
LowerExpformatting by using the result of invokingOctalformatting. - Lower
ExpFrom Pointer - Provides
LowerExpformatting by using the result of invokingPointerformatting. - Lower
ExpFrom Upper Exp - Provides
LowerExpformatting by using the result of invokingUpperExpformatting. - Lower
ExpFrom Upper Hex - Provides
LowerExpformatting by using the result of invokingUpperHexformatting. - Lower
HexFrom Debug - Provides
LowerHexformatting by using the result of invokingDebugformatting. - Lower
HexFrom Display - Provides
LowerHexformatting by using the result of invokingDisplayformatting. - Lower
HexFrom Lower Exp - Provides
LowerHexformatting by using the result of invokingLowerExpformatting. - Lower
HexFrom Octal - Provides
LowerHexformatting by using the result of invokingOctalformatting. - Lower
HexFrom Pointer - Provides
LowerHexformatting by using the result of invokingPointerformatting. - Lower
HexFrom Upper Exp - Provides
LowerHexformatting by using the result of invokingUpperExpformatting. - Lower
HexFrom Upper Hex - Provides
LowerHexformatting by using the result of invokingUpperHexformatting. - Octal
From Debug - Provides
Octalformatting by using the result of invokingDebugformatting. - Octal
From Display - Provides
Octalformatting by using the result of invokingDisplayformatting. - Octal
From Lower Exp - Provides
Octalformatting by using the result of invokingLowerExpformatting. - Octal
From Lower Hex - Provides
Octalformatting by using the result of invokingLowerHexformatting. - Octal
From Pointer - Provides
Octalformatting by using the result of invokingPointerformatting. - Octal
From Upper Exp - Provides
Octalformatting by using the result of invokingUpperExpformatting. - Octal
From Upper Hex - Provides
Octalformatting by using the result of invokingUpperHexformatting. - Only
Debug - Only implements the
Debugformatting trait despite any other trait being implemented onT. - Only
Display - Only implements the
Displayformatting trait despite any other trait being implemented onT. - Only
Lower Exp - Only implements the
LowerExpformatting trait despite any other trait being implemented onT. - Only
Lower Hex - Only implements the
LowerHexformatting trait despite any other trait being implemented onT. - Only
Octal - Only implements the
Octalformatting trait despite any other trait being implemented onT. - Only
Pointer - Only implements the
Pointerformatting trait despite any other trait being implemented onT. - Only
Upper Exp - Only implements the
UpperExpformatting trait despite any other trait being implemented onT. - Only
Upper Hex - Only implements the
UpperHexformatting trait despite any other trait being implemented onT. - Pointer
From Debug - Provides
Pointerformatting by using the result of invokingDebugformatting. - Pointer
From Display - Provides
Pointerformatting by using the result of invokingDisplayformatting. - Pointer
From Lower Exp - Provides
Pointerformatting by using the result of invokingLowerExpformatting. - Pointer
From Lower Hex - Provides
Pointerformatting by using the result of invokingLowerHexformatting. - Pointer
From Octal - Provides
Pointerformatting by using the result of invokingOctalformatting. - Pointer
From Upper Exp - Provides
Pointerformatting by using the result of invokingUpperExpformatting. - Pointer
From Upper Hex - Provides
Pointerformatting by using the result of invokingUpperHexformatting. - Upper
ExpFrom Debug - Provides
UpperExpformatting by using the result of invokingDebugformatting. - Upper
ExpFrom Display - Provides
UpperExpformatting by using the result of invokingDisplayformatting. - Upper
ExpFrom Lower Exp - Provides
UpperExpformatting by using the result of invokingLowerExpformatting. - Upper
ExpFrom Lower Hex - Provides
UpperExpformatting by using the result of invokingLowerHexformatting. - Upper
ExpFrom Octal - Provides
UpperExpformatting by using the result of invokingOctalformatting. - Upper
ExpFrom Pointer - Provides
UpperExpformatting by using the result of invokingPointerformatting. - Upper
ExpFrom Upper Hex - Provides
UpperExpformatting by using the result of invokingUpperHexformatting. - Upper
HexFrom Debug - Provides
UpperHexformatting by using the result of invokingDebugformatting. - Upper
HexFrom Display - Provides
UpperHexformatting by using the result of invokingDisplayformatting. - Upper
HexFrom Lower Exp - Provides
UpperHexformatting by using the result of invokingLowerExpformatting. - Upper
HexFrom Lower Hex - Provides
UpperHexformatting by using the result of invokingLowerHexformatting. - Upper
HexFrom Octal - Provides
UpperHexformatting by using the result of invokingOctalformatting. - Upper
HexFrom Pointer - Provides
UpperHexformatting by using the result of invokingPointerformatting. - Upper
HexFrom Upper Exp - Provides
UpperHexformatting by using the result of invokingUpperExpformatting.