Skip to content

Commit

Permalink
Implement IntoFunction for DynamicFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Jun 30, 2024
1 parent c95210c commit 91f249f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crates/bevy_reflect/src/func/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::func::args::{ArgInfo, ArgList};
use crate::func::error::FunctionError;
use crate::func::info::FunctionInfo;
use crate::func::return_type::Return;
use crate::func::ReturnInfo;
use crate::func::{IntoFunction, ReturnInfo};
use alloc::borrow::Cow;
use core::fmt::{Debug, Formatter};
use std::ops::DerefMut;
Expand Down Expand Up @@ -90,8 +90,6 @@ pub type FunctionResult<'a> = Result<Return<'a>, FunctionError>;
/// // Check the result:
/// assert_eq!(list, vec!["Hello, World!!!"]);
/// ```
///
/// [`IntoFunction`]: crate::func::IntoFunction
pub struct DynamicFunction<'env> {
info: FunctionInfo,
func: Box<dyn for<'a> FnMut(ArgList<'a>, &FunctionInfo) -> FunctionResult<'a> + 'env>,
Expand Down Expand Up @@ -120,7 +118,6 @@ impl<'env> DynamicFunction<'env> {
/// the default name will always be the full path to the function as returned by [`std::any::type_name`].
///
/// [`DynamicFunctions`]: DynamicFunction
/// [`IntoFunction`]: crate::func::IntoFunction
pub fn with_name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
self.info = self.info.with_name(name);
self
Expand Down Expand Up @@ -214,3 +211,10 @@ impl<'env> Debug for DynamicFunction<'env> {
write!(f, ") -> {ret})")
}
}

impl<'env> IntoFunction<'env, ()> for DynamicFunction<'env> {
#[inline]
fn into_function(self) -> DynamicFunction<'env> {
self
}
}
10 changes: 10 additions & 0 deletions crates/bevy_reflect/src/func/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,14 @@ mod tests {
})
);
}

#[test]
fn should_convert_dynamic_function_with_into_function() {
fn make_function<'a, F: IntoFunction<'a, M>, M>(f: F) -> DynamicFunction<'a> {
f.into_function()
}

let function: DynamicFunction = make_function(|| {});
let _: DynamicFunction = make_function(function);
}
}

0 comments on commit 91f249f

Please sign in to comment.