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 thefmt
documentation.
Structs§
- Debug
From Display - Provides
Debug
formatting by using the result of invokingDisplay
formatting. - Debug
From Lower Exp - Provides
Debug
formatting by using the result of invokingLowerExp
formatting. - Debug
From Lower Hex - Provides
Debug
formatting by using the result of invokingLowerHex
formatting. - Debug
From Octal - Provides
Debug
formatting by using the result of invokingOctal
formatting. - Debug
From Pointer - Provides
Debug
formatting by using the result of invokingPointer
formatting. - Debug
From Upper Exp - Provides
Debug
formatting by using the result of invokingUpperExp
formatting. - Debug
From Upper Hex - Provides
Debug
formatting by using the result of invokingUpperHex
formatting. - Display
From Debug - Provides
Display
formatting by using the result of invokingDebug
formatting. - Display
From Lower Exp - Provides
Display
formatting by using the result of invokingLowerExp
formatting. - Display
From Lower Hex - Provides
Display
formatting by using the result of invokingLowerHex
formatting. - Display
From Octal - Provides
Display
formatting by using the result of invokingOctal
formatting. - Display
From Pointer - Provides
Display
formatting by using the result of invokingPointer
formatting. - Display
From Upper Exp - Provides
Display
formatting by using the result of invokingUpperExp
formatting. - Display
From Upper Hex - Provides
Display
formatting by using the result of invokingUpperHex
formatting. - Lower
ExpFrom Debug - Provides
LowerExp
formatting by using the result of invokingDebug
formatting. - Lower
ExpFrom Display - Provides
LowerExp
formatting by using the result of invokingDisplay
formatting. - Lower
ExpFrom Lower Hex - Provides
LowerExp
formatting by using the result of invokingLowerHex
formatting. - Lower
ExpFrom Octal - Provides
LowerExp
formatting by using the result of invokingOctal
formatting. - Lower
ExpFrom Pointer - Provides
LowerExp
formatting by using the result of invokingPointer
formatting. - Lower
ExpFrom Upper Exp - Provides
LowerExp
formatting by using the result of invokingUpperExp
formatting. - Lower
ExpFrom Upper Hex - Provides
LowerExp
formatting by using the result of invokingUpperHex
formatting. - Lower
HexFrom Debug - Provides
LowerHex
formatting by using the result of invokingDebug
formatting. - Lower
HexFrom Display - Provides
LowerHex
formatting by using the result of invokingDisplay
formatting. - Lower
HexFrom Lower Exp - Provides
LowerHex
formatting by using the result of invokingLowerExp
formatting. - Lower
HexFrom Octal - Provides
LowerHex
formatting by using the result of invokingOctal
formatting. - Lower
HexFrom Pointer - Provides
LowerHex
formatting by using the result of invokingPointer
formatting. - Lower
HexFrom Upper Exp - Provides
LowerHex
formatting by using the result of invokingUpperExp
formatting. - Lower
HexFrom Upper Hex - Provides
LowerHex
formatting by using the result of invokingUpperHex
formatting. - Octal
From Debug - Provides
Octal
formatting by using the result of invokingDebug
formatting. - Octal
From Display - Provides
Octal
formatting by using the result of invokingDisplay
formatting. - Octal
From Lower Exp - Provides
Octal
formatting by using the result of invokingLowerExp
formatting. - Octal
From Lower Hex - Provides
Octal
formatting by using the result of invokingLowerHex
formatting. - Octal
From Pointer - Provides
Octal
formatting by using the result of invokingPointer
formatting. - Octal
From Upper Exp - Provides
Octal
formatting by using the result of invokingUpperExp
formatting. - Octal
From Upper Hex - Provides
Octal
formatting by using the result of invokingUpperHex
formatting. - Only
Debug - Only implements the
Debug
formatting trait despite any other trait being implemented onT
. - Only
Display - Only implements the
Display
formatting trait despite any other trait being implemented onT
. - Only
Lower Exp - Only implements the
LowerExp
formatting trait despite any other trait being implemented onT
. - Only
Lower Hex - Only implements the
LowerHex
formatting trait despite any other trait being implemented onT
. - Only
Octal - Only implements the
Octal
formatting trait despite any other trait being implemented onT
. - Only
Pointer - Only implements the
Pointer
formatting trait despite any other trait being implemented onT
. - Only
Upper Exp - Only implements the
UpperExp
formatting trait despite any other trait being implemented onT
. - Only
Upper Hex - Only implements the
UpperHex
formatting trait despite any other trait being implemented onT
. - Pointer
From Debug - Provides
Pointer
formatting by using the result of invokingDebug
formatting. - Pointer
From Display - Provides
Pointer
formatting by using the result of invokingDisplay
formatting. - Pointer
From Lower Exp - Provides
Pointer
formatting by using the result of invokingLowerExp
formatting. - Pointer
From Lower Hex - Provides
Pointer
formatting by using the result of invokingLowerHex
formatting. - Pointer
From Octal - Provides
Pointer
formatting by using the result of invokingOctal
formatting. - Pointer
From Upper Exp - Provides
Pointer
formatting by using the result of invokingUpperExp
formatting. - Pointer
From Upper Hex - Provides
Pointer
formatting by using the result of invokingUpperHex
formatting. - Upper
ExpFrom Debug - Provides
UpperExp
formatting by using the result of invokingDebug
formatting. - Upper
ExpFrom Display - Provides
UpperExp
formatting by using the result of invokingDisplay
formatting. - Upper
ExpFrom Lower Exp - Provides
UpperExp
formatting by using the result of invokingLowerExp
formatting. - Upper
ExpFrom Lower Hex - Provides
UpperExp
formatting by using the result of invokingLowerHex
formatting. - Upper
ExpFrom Octal - Provides
UpperExp
formatting by using the result of invokingOctal
formatting. - Upper
ExpFrom Pointer - Provides
UpperExp
formatting by using the result of invokingPointer
formatting. - Upper
ExpFrom Upper Hex - Provides
UpperExp
formatting by using the result of invokingUpperHex
formatting. - Upper
HexFrom Debug - Provides
UpperHex
formatting by using the result of invokingDebug
formatting. - Upper
HexFrom Display - Provides
UpperHex
formatting by using the result of invokingDisplay
formatting. - Upper
HexFrom Lower Exp - Provides
UpperHex
formatting by using the result of invokingLowerExp
formatting. - Upper
HexFrom Lower Hex - Provides
UpperHex
formatting by using the result of invokingLowerHex
formatting. - Upper
HexFrom Octal - Provides
UpperHex
formatting by using the result of invokingOctal
formatting. - Upper
HexFrom Pointer - Provides
UpperHex
formatting by using the result of invokingPointer
formatting. - Upper
HexFrom Upper Exp - Provides
UpperHex
formatting by using the result of invokingUpperExp
formatting.