1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![no_std]

use core::fmt::{self, Formatter, Result};

// Generate lazy format macro
macro_rules! gen_lazy_format {
    (Debug) => {
        gen_lazy_format! { @base
            #[doc = "A lazy format type that implements [Debug](core::fmt::Debug) as base format trait"]
            Debug
        }
    };
    (Display) => {
        gen_lazy_format! { @base
            #[doc = "A lazy format type that implements [Display](core::fmt::Display) as base format trait, [Debug](core::fmt::Debug) as derivative from [Display](core::fmt::Display)"]
            Display
        }
        gen_lazy_format! { @debug Display }
    };
    ($self:ident) => {
        gen_lazy_format! { @base
            #[doc = concat!(
                "A lazy format type that implements [",
                stringify!($self),
                "](core::fmt::",
                stringify!($self),
                ") as base format trait, [Display](core::fmt::Display) and [Debug](core::fmt::Debug) as derivative from [",
                stringify!($self),
                "](core::fmt::",
                stringify!($self),
                ")"
            )]
            $self
        }
        gen_lazy_format! { @display $self }
        gen_lazy_format! { @debug $self }
    };
    (@base $(#[doc = $doc:expr])* $self:ident) => {
        $(#[doc = $doc])*
        #[derive(Clone, Copy)]
        pub struct $self<F: Fn(&mut Formatter) -> Result>(pub F);

        impl<F: Fn(&mut Formatter) -> Result> fmt::$self for $self<F> {
            fn fmt(&self, f: &mut Formatter) -> Result {
                (self.0)(f)
            }
        }
    };
    (@display $self:ident) => {
        impl<F: Fn(&mut Formatter) -> Result> fmt::Display for $self<F> {
            fn fmt(&self, f: &mut Formatter) -> Result {
                fmt::$self::fmt(self, f)
            }
        }
    };
    (@debug $self:ident) => {
        impl<F: Fn(&mut Formatter) -> Result> fmt::Debug for $self<F> {
            fn fmt(&self, f: &mut Formatter) -> Result {
                f.debug_tuple(stringify!($self))
                    .field(&Debug(&self.0))
                    .finish()
            }
        }
    };
}

gen_lazy_format! { Debug }
gen_lazy_format! { Display }
gen_lazy_format! { Binary }
gen_lazy_format! { LowerExp }
gen_lazy_format! { LowerHex }
gen_lazy_format! { Octal }
gen_lazy_format! { Pointer }
gen_lazy_format! { UpperExp }
gen_lazy_format! { UpperHex }