@@ -62,34 +62,31 @@ pub struct Argument<'a> {
62
62
ty : ArgumentType < ' a > ,
63
63
}
64
64
65
- macro_rules! define_argument_constructor {
66
- ( $method: ident, $trait: ident, $fmt: item) => {
67
- #[ inline]
68
- pub fn $method<T : $trait>( x: & T ) -> Argument <' _> {
69
- #[ repr( transparent) ]
70
- struct Wrapper <T >( T ) ;
71
-
72
- // SAFETY: `Wrapper<T>` has the same memory layout as `T` due to #[repr(transparent)].
73
- let thunk = unsafe { mem:: transmute:: <& T , & Wrapper <T >>( x) } ;
74
-
75
- impl <T : $trait> FormatThunk for Wrapper <T > {
76
- #[ inline]
77
- $fmt
78
- }
65
+ macro_rules! implement_argument_constructor {
66
+ ( $trait: ident, $operand: expr, $fmt: item) => { {
67
+ #[ repr( transparent) ]
68
+ struct Wrapper <T >( T ) ;
69
+
70
+ // SAFETY: `Wrapper<T>` has the same memory layout as `T` due to #[repr(transparent)].
71
+ let thunk = unsafe { mem:: transmute:: <& T , & Wrapper <T >>( $operand) } ;
79
72
80
- Self :: new( thunk)
73
+ impl <T : $trait> FormatThunk for Wrapper <T > {
74
+ #[ inline]
75
+ $fmt
81
76
}
82
- } ;
83
77
84
- ( $method: ident, $trait: ident) => {
85
- define_argument_constructor!(
86
- $method,
78
+ Self :: new( thunk)
79
+ } } ;
80
+
81
+ ( $trait: ident, $operand: expr) => {
82
+ implement_argument_constructor!(
87
83
$trait,
84
+ $operand,
88
85
fn fmt( & self , f: & mut Formatter <' _>) -> Result {
89
86
let Self ( inner) = self ;
90
87
inner. fmt( f)
91
88
}
92
- ) ;
89
+ )
93
90
} ;
94
91
}
95
92
@@ -100,22 +97,52 @@ impl Argument<'_> {
100
97
Argument { ty : ArgumentType :: Placeholder ( x) }
101
98
}
102
99
103
- define_argument_constructor ! ( new_display, Display ) ;
104
- define_argument_constructor ! ( new_debug, Debug ) ;
105
- define_argument_constructor ! (
106
- new_debug_noop,
107
- Debug ,
108
- fn fmt( & self , _: & mut Formatter <' _>) -> Result {
109
- Ok ( ( ) )
110
- }
111
- ) ;
112
- define_argument_constructor ! ( new_octal, Octal ) ;
113
- define_argument_constructor ! ( new_lower_hex, LowerHex ) ;
114
- define_argument_constructor ! ( new_upper_hex, UpperHex ) ;
115
- define_argument_constructor ! ( new_pointer, Pointer ) ;
116
- define_argument_constructor ! ( new_binary, Binary ) ;
117
- define_argument_constructor ! ( new_lower_exp, LowerExp ) ;
118
- define_argument_constructor ! ( new_upper_exp, UpperExp ) ;
100
+ #[ inline]
101
+ pub fn new_display < T : Display > ( x : & T ) -> Argument < ' _ > {
102
+ implement_argument_constructor ! ( Display , x)
103
+ }
104
+ #[ inline]
105
+ pub fn new_debug < T : Debug > ( x : & T ) -> Argument < ' _ > {
106
+ implement_argument_constructor ! ( Debug , x)
107
+ }
108
+ #[ inline]
109
+ pub fn new_debug_noop < T : Debug > ( x : & T ) -> Argument < ' _ > {
110
+ implement_argument_constructor ! (
111
+ Debug ,
112
+ x,
113
+ fn fmt( & self , _: & mut Formatter <' _>) -> Result {
114
+ Ok ( ( ) )
115
+ }
116
+ )
117
+ }
118
+ #[ inline]
119
+ pub fn new_octal < T : Octal > ( x : & T ) -> Argument < ' _ > {
120
+ implement_argument_constructor ! ( Octal , x)
121
+ }
122
+ #[ inline]
123
+ pub fn new_lower_hex < T : LowerHex > ( x : & T ) -> Argument < ' _ > {
124
+ implement_argument_constructor ! ( LowerHex , x)
125
+ }
126
+ #[ inline]
127
+ pub fn new_upper_hex < T : UpperHex > ( x : & T ) -> Argument < ' _ > {
128
+ implement_argument_constructor ! ( UpperHex , x)
129
+ }
130
+ #[ inline]
131
+ pub fn new_pointer < T : Pointer > ( x : & T ) -> Argument < ' _ > {
132
+ implement_argument_constructor ! ( Pointer , x)
133
+ }
134
+ #[ inline]
135
+ pub fn new_binary < T : Binary > ( x : & T ) -> Argument < ' _ > {
136
+ implement_argument_constructor ! ( Binary , x)
137
+ }
138
+ #[ inline]
139
+ pub fn new_lower_exp < T : LowerExp > ( x : & T ) -> Argument < ' _ > {
140
+ implement_argument_constructor ! ( LowerExp , x)
141
+ }
142
+ #[ inline]
143
+ pub fn new_upper_exp < T : UpperExp > ( x : & T ) -> Argument < ' _ > {
144
+ implement_argument_constructor ! ( UpperExp , x)
145
+ }
119
146
120
147
#[ inline]
121
148
#[ track_caller]
0 commit comments