Crate fmt_adapter

Source
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 the fmt documentation.

Structs§

DebugFromDisplay
Provides Debug formatting by using the result of invoking Display formatting.
DebugFromLowerExp
Provides Debug formatting by using the result of invoking LowerExp formatting.
DebugFromLowerHex
Provides Debug formatting by using the result of invoking LowerHex formatting.
DebugFromOctal
Provides Debug formatting by using the result of invoking Octal formatting.
DebugFromPointer
Provides Debug formatting by using the result of invoking Pointer formatting.
DebugFromUpperExp
Provides Debug formatting by using the result of invoking UpperExp formatting.
DebugFromUpperHex
Provides Debug formatting by using the result of invoking UpperHex formatting.
DisplayFromDebug
Provides Display formatting by using the result of invoking Debug formatting.
DisplayFromLowerExp
Provides Display formatting by using the result of invoking LowerExp formatting.
DisplayFromLowerHex
Provides Display formatting by using the result of invoking LowerHex formatting.
DisplayFromOctal
Provides Display formatting by using the result of invoking Octal formatting.
DisplayFromPointer
Provides Display formatting by using the result of invoking Pointer formatting.
DisplayFromUpperExp
Provides Display formatting by using the result of invoking UpperExp formatting.
DisplayFromUpperHex
Provides Display formatting by using the result of invoking UpperHex formatting.
LowerExpFromDebug
Provides LowerExp formatting by using the result of invoking Debug formatting.
LowerExpFromDisplay
Provides LowerExp formatting by using the result of invoking Display formatting.
LowerExpFromLowerHex
Provides LowerExp formatting by using the result of invoking LowerHex formatting.
LowerExpFromOctal
Provides LowerExp formatting by using the result of invoking Octal formatting.
LowerExpFromPointer
Provides LowerExp formatting by using the result of invoking Pointer formatting.
LowerExpFromUpperExp
Provides LowerExp formatting by using the result of invoking UpperExp formatting.
LowerExpFromUpperHex
Provides LowerExp formatting by using the result of invoking UpperHex formatting.
LowerHexFromDebug
Provides LowerHex formatting by using the result of invoking Debug formatting.
LowerHexFromDisplay
Provides LowerHex formatting by using the result of invoking Display formatting.
LowerHexFromLowerExp
Provides LowerHex formatting by using the result of invoking LowerExp formatting.
LowerHexFromOctal
Provides LowerHex formatting by using the result of invoking Octal formatting.
LowerHexFromPointer
Provides LowerHex formatting by using the result of invoking Pointer formatting.
LowerHexFromUpperExp
Provides LowerHex formatting by using the result of invoking UpperExp formatting.
LowerHexFromUpperHex
Provides LowerHex formatting by using the result of invoking UpperHex formatting.
OctalFromDebug
Provides Octal formatting by using the result of invoking Debug formatting.
OctalFromDisplay
Provides Octal formatting by using the result of invoking Display formatting.
OctalFromLowerExp
Provides Octal formatting by using the result of invoking LowerExp formatting.
OctalFromLowerHex
Provides Octal formatting by using the result of invoking LowerHex formatting.
OctalFromPointer
Provides Octal formatting by using the result of invoking Pointer formatting.
OctalFromUpperExp
Provides Octal formatting by using the result of invoking UpperExp formatting.
OctalFromUpperHex
Provides Octal formatting by using the result of invoking UpperHex formatting.
OnlyDebug
Only implements the Debug formatting trait despite any other trait being implemented on T.
OnlyDisplay
Only implements the Display formatting trait despite any other trait being implemented on T.
OnlyLowerExp
Only implements the LowerExp formatting trait despite any other trait being implemented on T.
OnlyLowerHex
Only implements the LowerHex formatting trait despite any other trait being implemented on T.
OnlyOctal
Only implements the Octal formatting trait despite any other trait being implemented on T.
OnlyPointer
Only implements the Pointer formatting trait despite any other trait being implemented on T.
OnlyUpperExp
Only implements the UpperExp formatting trait despite any other trait being implemented on T.
OnlyUpperHex
Only implements the UpperHex formatting trait despite any other trait being implemented on T.
PointerFromDebug
Provides Pointer formatting by using the result of invoking Debug formatting.
PointerFromDisplay
Provides Pointer formatting by using the result of invoking Display formatting.
PointerFromLowerExp
Provides Pointer formatting by using the result of invoking LowerExp formatting.
PointerFromLowerHex
Provides Pointer formatting by using the result of invoking LowerHex formatting.
PointerFromOctal
Provides Pointer formatting by using the result of invoking Octal formatting.
PointerFromUpperExp
Provides Pointer formatting by using the result of invoking UpperExp formatting.
PointerFromUpperHex
Provides Pointer formatting by using the result of invoking UpperHex formatting.
UpperExpFromDebug
Provides UpperExp formatting by using the result of invoking Debug formatting.
UpperExpFromDisplay
Provides UpperExp formatting by using the result of invoking Display formatting.
UpperExpFromLowerExp
Provides UpperExp formatting by using the result of invoking LowerExp formatting.
UpperExpFromLowerHex
Provides UpperExp formatting by using the result of invoking LowerHex formatting.
UpperExpFromOctal
Provides UpperExp formatting by using the result of invoking Octal formatting.
UpperExpFromPointer
Provides UpperExp formatting by using the result of invoking Pointer formatting.
UpperExpFromUpperHex
Provides UpperExp formatting by using the result of invoking UpperHex formatting.
UpperHexFromDebug
Provides UpperHex formatting by using the result of invoking Debug formatting.
UpperHexFromDisplay
Provides UpperHex formatting by using the result of invoking Display formatting.
UpperHexFromLowerExp
Provides UpperHex formatting by using the result of invoking LowerExp formatting.
UpperHexFromLowerHex
Provides UpperHex formatting by using the result of invoking LowerHex formatting.
UpperHexFromOctal
Provides UpperHex formatting by using the result of invoking Octal formatting.
UpperHexFromPointer
Provides UpperHex formatting by using the result of invoking Pointer formatting.
UpperHexFromUpperExp
Provides UpperHex formatting by using the result of invoking UpperExp formatting.