Removed need for Downcast on Function

This commit is contained in:
Gino Valente 2024-09-14 15:58:34 -07:00
parent 65edd1e15d
commit 87fa86c10a
2 changed files with 17 additions and 15 deletions

View file

@ -154,6 +154,10 @@ impl Function for DynamicFunction<'static> {
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a> {
self.call(args)
}
fn clone_dynamic(&self) -> DynamicFunction<'static> {
self.clone()
}
}
impl PartialReflect for DynamicFunction<'static> {
@ -186,16 +190,16 @@ impl PartialReflect for DynamicFunction<'static> {
}
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
if let ReflectRef::Function(func) = value.reflect_ref() {
if let Some(func) = func.downcast_ref::<Self>() {
*self = func.clone();
return Ok(());
match value.reflect_ref() {
ReflectRef::Function(func) => {
*self = func.clone_dynamic();
Ok(())
}
_ => Err(ApplyError::MismatchedTypes {
from_type: value.reflect_type_path().into(),
to_type: Self::type_path().into(),
}),
}
Err(ApplyError::MismatchedTypes {
from_type: value.reflect_type_path().into(),
to_type: Self::type_path().into(),
})
}
fn reflect_kind(&self) -> ReflectKind {

View file

@ -1,8 +1,7 @@
use crate::func::{ArgList, FunctionInfo, FunctionResult};
use crate::func::{ArgList, DynamicFunction, FunctionInfo, FunctionResult};
use crate::PartialReflect;
use alloc::borrow::Cow;
use core::fmt::Debug;
use downcast_rs::{impl_downcast, Downcast};
/// A trait used to power [function-like] operations via [reflection].
///
@ -31,7 +30,7 @@ use downcast_rs::{impl_downcast, Downcast};
/// [`Reflect`]: crate::Reflect
/// [arguments]: crate::func::args
/// [`DynamicFunction`]: crate::func::DynamicFunction
pub trait Function: PartialReflect + Downcast + Debug {
pub trait Function: PartialReflect + Debug {
/// The name of the function, if any.
///
/// For [`DynamicFunctions`] created using [`IntoFunction`],
@ -55,11 +54,10 @@ pub trait Function: PartialReflect + Downcast + Debug {
/// Call this function with the given arguments.
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>;
}
// We need to be able to downcast from `dyn Function` so that
// we can implement `PartialReflect::try_apply`.
impl_downcast!(Function);
/// Clone this function into a [`DynamicFunction`].
fn clone_dynamic(&self) -> DynamicFunction<'static>;
}
#[cfg(test)]
mod tests {