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
macro_rules! derive_fmt {
    ($trait:ident, $Trait:ident, [$($name:expr),*]) => {
        pub(crate) mod $trait {
            use crate::derive::*;

            pub(crate) const NAME: &[&str] = &[$($name),*];

            pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
                Ok(derive_trait(data, parse_quote!(::core::fmt::$Trait), None, parse_quote! {
                    trait $Trait {
                        #[inline]
                        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result;
                    }
                }))
            }
        }
    };
}

derive_fmt!(debug, Debug, ["Debug", "fmt::Debug"]);
derive_fmt!(display, Display, ["Display", "fmt::Display"]);

#[cfg(feature = "fmt")]
derive_fmt!(binary, Binary, ["fmt::Binary"]);
#[cfg(feature = "fmt")]
derive_fmt!(lower_exp, LowerExp, ["fmt::LowerExp"]);
#[cfg(feature = "fmt")]
derive_fmt!(lower_hex, LowerHex, ["fmt::LowerHex"]);
#[cfg(feature = "fmt")]
derive_fmt!(octal, Octal, ["fmt::Octal"]);
#[cfg(feature = "fmt")]
derive_fmt!(pointer, Pointer, ["fmt::Pointer"]);
#[cfg(feature = "fmt")]
derive_fmt!(upper_exp, UpperExp, ["fmt::UpperExp"]);
#[cfg(feature = "fmt")]
derive_fmt!(upper_hex, UpperHex, ["fmt::UpperHex"]);

pub(crate) mod write {
    use crate::derive::*;

    pub(crate) const NAME: &[&str] = &["fmt::Write"];

    pub(crate) fn derive(data: &Data) -> Result<TokenStream> {
        Ok(derive_trait(data, parse_quote!(::core::fmt::Write), None, parse_quote! {
            trait Write {
                #[inline]
                fn write_str(&mut self, s: &str) -> ::core::fmt::Result;
                #[inline]
                fn write_char(&mut self, c: char) -> ::core::fmt::Result;
                #[inline]
                fn write_fmt(&mut self, args: ::core::fmt::Arguments<'_>) -> ::core::fmt::Result;
            }
        }))
    }
}