| 1 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 | |
| 3 | macro_rules! derive_fmt { |
| 4 | ($trait:ident, $Trait:ident, [$($name:expr),*]) => { |
| 5 | pub(crate) mod $trait { |
| 6 | use crate::derive::prelude::*; |
| 7 | |
| 8 | pub(crate) const NAME: &[&str] = &[$($name),*]; |
| 9 | |
| 10 | pub(crate) fn derive(_cx: &Context, data: &Data) -> Result<TokenStream> { |
| 11 | Ok(derive_trait(data, &parse_quote!(::core::fmt::$Trait), None, parse_quote! { |
| 12 | trait $Trait { |
| 13 | #[inline] |
| 14 | fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result; |
| 15 | } |
| 16 | })) |
| 17 | } |
| 18 | } |
| 19 | }; |
| 20 | } |
| 21 | |
| 22 | derive_fmt!(debug, Debug, ["Debug" , "fmt::Debug" ]); |
| 23 | derive_fmt!(display, Display, ["Display" , "fmt::Display" ]); |
| 24 | |
| 25 | #[cfg (feature = "fmt" )] |
| 26 | derive_fmt!(binary, Binary, ["fmt::Binary" ]); |
| 27 | #[cfg (feature = "fmt" )] |
| 28 | derive_fmt!(lower_exp, LowerExp, ["fmt::LowerExp" ]); |
| 29 | #[cfg (feature = "fmt" )] |
| 30 | derive_fmt!(lower_hex, LowerHex, ["fmt::LowerHex" ]); |
| 31 | #[cfg (feature = "fmt" )] |
| 32 | derive_fmt!(octal, Octal, ["fmt::Octal" ]); |
| 33 | #[cfg (feature = "fmt" )] |
| 34 | derive_fmt!(pointer, Pointer, ["fmt::Pointer" ]); |
| 35 | #[cfg (feature = "fmt" )] |
| 36 | derive_fmt!(upper_exp, UpperExp, ["fmt::UpperExp" ]); |
| 37 | #[cfg (feature = "fmt" )] |
| 38 | derive_fmt!(upper_hex, UpperHex, ["fmt::UpperHex" ]); |
| 39 | |
| 40 | pub(crate) mod write { |
| 41 | use crate::derive::prelude::*; |
| 42 | |
| 43 | pub(crate) const NAME: &[&str] = &["fmt::Write" ]; |
| 44 | |
| 45 | pub(crate) fn derive(_cx: &Context, data: &Data) -> Result<TokenStream> { |
| 46 | Ok(derive_trait(data, &parse_quote!(::core::fmt::Write), supertraits_types:None, trait_def:parse_quote! { |
| 47 | trait Write { |
| 48 | #[inline] |
| 49 | fn write_str(&mut self, s: &str) -> ::core::fmt::Result; |
| 50 | #[inline] |
| 51 | fn write_char(&mut self, c: char) -> ::core::fmt::Result; |
| 52 | #[inline] |
| 53 | fn write_fmt(&mut self, args: ::core::fmt::Arguments<'_>) -> ::core::fmt::Result; |
| 54 | } |
| 55 | })) |
| 56 | } |
| 57 | } |
| 58 | |