Skip to content

Commit 91f249f

Browse files
committed
Implement IntoFunction for DynamicFunction
1 parent c95210c commit 91f249f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

crates/bevy_reflect/src/func/function.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::func::args::{ArgInfo, ArgList};
22
use crate::func::error::FunctionError;
33
use crate::func::info::FunctionInfo;
44
use crate::func::return_type::Return;
5-
use crate::func::ReturnInfo;
5+
use crate::func::{IntoFunction, ReturnInfo};
66
use alloc::borrow::Cow;
77
use core::fmt::{Debug, Formatter};
88
use std::ops::DerefMut;
@@ -90,8 +90,6 @@ pub type FunctionResult<'a> = Result<Return<'a>, FunctionError>;
9090
/// // Check the result:
9191
/// assert_eq!(list, vec!["Hello, World!!!"]);
9292
/// ```
93-
///
94-
/// [`IntoFunction`]: crate::func::IntoFunction
9593
pub struct DynamicFunction<'env> {
9694
info: FunctionInfo,
9795
func: Box<dyn for<'a> FnMut(ArgList<'a>, &FunctionInfo) -> FunctionResult<'a> + 'env>,
@@ -120,7 +118,6 @@ impl<'env> DynamicFunction<'env> {
120118
/// the default name will always be the full path to the function as returned by [`std::any::type_name`].
121119
///
122120
/// [`DynamicFunctions`]: DynamicFunction
123-
/// [`IntoFunction`]: crate::func::IntoFunction
124121
pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
125122
self.info = self.info.with_name(name);
126123
self
@@ -214,3 +211,10 @@ impl<'env> Debug for DynamicFunction<'env> {
214211
write!(f, ") -> {ret})")
215212
}
216213
}
214+
215+
impl<'env> IntoFunction<'env, ()> for DynamicFunction<'env> {
216+
#[inline]
217+
fn into_function(self) -> DynamicFunction<'env> {
218+
self
219+
}
220+
}

crates/bevy_reflect/src/func/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,14 @@ mod tests {
252252
})
253253
);
254254
}
255+
256+
#[test]
257+
fn should_convert_dynamic_function_with_into_function() {
258+
fn make_function<'a, F: IntoFunction<'a, M>, M>(f: F) -> DynamicFunction<'a> {
259+
f.into_function()
260+
}
261+
262+
let function: DynamicFunction = make_function(|| {});
263+
let _: DynamicFunction = make_function(function);
264+
}
255265
}

0 commit comments

Comments
 (0)