Skip to content

Commit c95210c

Browse files
committed
Infer function name using std::any::type_name
1 parent 1b32c3e commit c95210c

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

crates/bevy_reflect/src/func/function.rs

+6
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ impl<'env> DynamicFunction<'env> {
115115
}
116116

117117
/// 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
118124
pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
119125
self.info = self.info.with_name(name);
120126
self

crates/bevy_reflect/src/func/info.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl FunctionInfo {
2626

2727
/// Set the name of the function.
2828
///
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,
3030
/// so this method must be called manually to set the name.
3131
pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
3232
self.name = Some(name.into());
@@ -52,11 +52,8 @@ impl FunctionInfo {
5252

5353
/// The name of the function, if it was given one.
5454
///
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`].
6057
///
6158
/// [`DynamicFunctions`]: crate::func::DynamicFunction
6259
pub fn name(&self) -> Option<&str> {

crates/bevy_reflect/src/func/into_function.rs

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ macro_rules! impl_into_function {
123123
const COUNT: usize = count_tts!($($Arg)*);
124124

125125
let info = $crate::func::FunctionInfo::new()
126+
.with_name(std::any::type_name::<F>())
126127
.with_args({
127128
#[allow(unused_mut)]
128129
let mut _index = 0;
@@ -171,6 +172,7 @@ macro_rules! impl_into_function {
171172
const COUNT: usize = count_tts!(Receiver $($Arg)*);
172173

173174
let info = $crate::func::FunctionInfo::new()
175+
.with_name(std::any::type_name::<F>())
174176
.with_args({
175177
#[allow(unused_mut)]
176178
let mut _index = 1;
@@ -222,6 +224,7 @@ macro_rules! impl_into_function {
222224
const COUNT: usize = count_tts!(Receiver $($Arg)*);
223225

224226
let info = $crate::func::FunctionInfo::new()
227+
.with_name(std::any::type_name::<F>())
225228
.with_args({
226229
#[allow(unused_mut)]
227230
let mut _index = 1;
@@ -273,6 +276,7 @@ macro_rules! impl_into_function {
273276
const COUNT: usize = count_tts!(Receiver $($Arg)*);
274277

275278
let info = $crate::func::FunctionInfo::new()
279+
.with_name(std::any::type_name::<F>())
276280
.with_args({
277281
#[allow(unused_mut)]
278282
let mut _index = 1;

crates/bevy_reflect/src/func/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,36 @@ mod tests {
157157
assert_eq!(result.downcast_mut::<i32>(), Some(&mut 123));
158158
}
159159

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+
160190
#[test]
161191
fn should_error_on_missing_args() {
162192
fn foo(_: i32) {}

0 commit comments

Comments
 (0)