File tree 4 files changed +43
-6
lines changed
crates/bevy_reflect/src/func
4 files changed +43
-6
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,12 @@ impl<'env> DynamicFunction<'env> {
115
115
}
116
116
117
117
/// Set the name of the function.
118
+ ///
119
+ /// For [`DynamicFunctions`] created using [`IntoFunction`],
120
+ /// the default name will always be the full path to the function as returned by [`std::any::type_name`].
121
+ ///
122
+ /// [`DynamicFunctions`]: DynamicFunction
123
+ /// [`IntoFunction`]: crate::func::IntoFunction
118
124
pub fn with_name ( mut self , name : impl Into < Cow < ' static , str > > ) -> Self {
119
125
self . info = self . info . with_name ( name) ;
120
126
self
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ impl FunctionInfo {
26
26
27
27
/// Set the name of the function.
28
28
///
29
- /// Reflected functions are not required to have a name and by default are not given one ,
29
+ /// Reflected functions are not required to have a name,
30
30
/// so this method must be called manually to set the name.
31
31
pub fn with_name ( mut self , name : impl Into < Cow < ' static , str > > ) -> Self {
32
32
self . name = Some ( name. into ( ) ) ;
@@ -52,11 +52,8 @@ impl FunctionInfo {
52
52
53
53
/// The name of the function, if it was given one.
54
54
///
55
- /// Note that this may return `None` even if the function has a name.
56
- /// This is because the name needs to be manually set using [`Self::with_name`]
57
- /// since the name can't be inferred from the function type alone.
58
- ///
59
- /// For [`DynamicFunctions`] created using [`IntoFunction`], the name will always be `None`.
55
+ /// For [`DynamicFunctions`] created using [`IntoFunction`],
56
+ /// the name will always be the full path to the function as returned by [`std::any::type_name`].
60
57
///
61
58
/// [`DynamicFunctions`]: crate::func::DynamicFunction
62
59
pub fn name ( & self ) -> Option < & str > {
Original file line number Diff line number Diff line change @@ -123,6 +123,7 @@ macro_rules! impl_into_function {
123
123
const COUNT : usize = count_tts!( $( $Arg) * ) ;
124
124
125
125
let info = $crate:: func:: FunctionInfo :: new( )
126
+ . with_name( std:: any:: type_name:: <F >( ) )
126
127
. with_args( {
127
128
#[ allow( unused_mut) ]
128
129
let mut _index = 0 ;
@@ -171,6 +172,7 @@ macro_rules! impl_into_function {
171
172
const COUNT : usize = count_tts!( Receiver $( $Arg) * ) ;
172
173
173
174
let info = $crate:: func:: FunctionInfo :: new( )
175
+ . with_name( std:: any:: type_name:: <F >( ) )
174
176
. with_args( {
175
177
#[ allow( unused_mut) ]
176
178
let mut _index = 1 ;
@@ -222,6 +224,7 @@ macro_rules! impl_into_function {
222
224
const COUNT : usize = count_tts!( Receiver $( $Arg) * ) ;
223
225
224
226
let info = $crate:: func:: FunctionInfo :: new( )
227
+ . with_name( std:: any:: type_name:: <F >( ) )
225
228
. with_args( {
226
229
#[ allow( unused_mut) ]
227
230
let mut _index = 1 ;
@@ -273,6 +276,7 @@ macro_rules! impl_into_function {
273
276
const COUNT : usize = count_tts!( Receiver $( $Arg) * ) ;
274
277
275
278
let info = $crate:: func:: FunctionInfo :: new( )
279
+ . with_name( std:: any:: type_name:: <F >( ) )
276
280
. with_args( {
277
281
#[ allow( unused_mut) ]
278
282
let mut _index = 1 ;
Original file line number Diff line number Diff line change @@ -157,6 +157,36 @@ mod tests {
157
157
assert_eq ! ( result. downcast_mut:: <i32 >( ) , Some ( & mut 123 ) ) ;
158
158
}
159
159
160
+ #[ test]
161
+ fn should_default_with_function_type_name ( ) {
162
+ fn foo ( ) { }
163
+
164
+ let func = foo. into_function ( ) ;
165
+ assert_eq ! (
166
+ func. info( ) . name( ) ,
167
+ Some ( "bevy_reflect::func::tests::should_default_with_function_type_name::foo" )
168
+ ) ;
169
+ }
170
+
171
+ #[ test]
172
+ fn should_default_with_closure_type_name ( ) {
173
+ let bar = |_: i32 | { } ;
174
+
175
+ let func = bar. into_function ( ) ;
176
+ assert_eq ! (
177
+ func. info( ) . name( ) ,
178
+ Some ( "bevy_reflect::func::tests::should_default_with_closure_type_name::{{closure}}" )
179
+ ) ;
180
+ }
181
+
182
+ #[ test]
183
+ fn should_overwrite_function_name ( ) {
184
+ fn foo ( ) { }
185
+
186
+ let func = foo. into_function ( ) . with_name ( "my_function" ) ;
187
+ assert_eq ! ( func. info( ) . name( ) , Some ( "my_function" ) ) ;
188
+ }
189
+
160
190
#[ test]
161
191
fn should_error_on_missing_args ( ) {
162
192
fn foo ( _: i32 ) { }
You can’t perform that action at this time.
0 commit comments