Skip to content

Commit 8186212

Browse files
committed
Removed need for Downcast on Function
1 parent c9f96bc commit 8186212

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

crates/bevy_reflect/src/func/dynamic_function.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ impl Function for DynamicFunction<'static> {
154154
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a> {
155155
self.call(args)
156156
}
157+
158+
fn clone_dynamic(&self) -> DynamicFunction<'static> {
159+
self.clone()
160+
}
157161
}
158162

159163
impl PartialReflect for DynamicFunction<'static> {
@@ -186,16 +190,16 @@ impl PartialReflect for DynamicFunction<'static> {
186190
}
187191

188192
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
189-
if let ReflectRef::Function(func) = value.reflect_ref() {
190-
if let Some(func) = func.downcast_ref::<Self>() {
191-
*self = func.clone();
192-
return Ok(());
193+
match value.reflect_ref() {
194+
ReflectRef::Function(func) => {
195+
*self = func.clone_dynamic();
196+
Ok(())
193197
}
198+
_ => Err(ApplyError::MismatchedTypes {
199+
from_type: value.reflect_type_path().into(),
200+
to_type: Self::type_path().into(),
201+
}),
194202
}
195-
Err(ApplyError::MismatchedTypes {
196-
from_type: value.reflect_type_path().into(),
197-
to_type: Self::type_path().into(),
198-
})
199203
}
200204

201205
fn reflect_kind(&self) -> ReflectKind {

crates/bevy_reflect/src/func/function.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::func::{ArgList, FunctionInfo, FunctionResult};
1+
use crate::func::{ArgList, DynamicFunction, FunctionInfo, FunctionResult};
22
use crate::PartialReflect;
33
use alloc::borrow::Cow;
4-
use downcast_rs::{impl_downcast, Downcast};
54

65
/// A trait used to power [function-like] operations via [reflection].
76
///
@@ -30,7 +29,7 @@ use downcast_rs::{impl_downcast, Downcast};
3029
/// [`Reflect`]: crate::Reflect
3130
/// [arguments]: crate::func::args
3231
/// [`DynamicFunction`]: crate::func::DynamicFunction
33-
pub trait Function: PartialReflect + Downcast {
32+
pub trait Function: PartialReflect {
3433
/// The name of the function, if any.
3534
///
3635
/// For [`DynamicFunctions`] created using [`IntoFunction`],
@@ -54,11 +53,10 @@ pub trait Function: PartialReflect + Downcast {
5453

5554
/// Call this function with the given arguments.
5655
fn reflect_call<'a>(&self, args: ArgList<'a>) -> FunctionResult<'a>;
57-
}
5856

59-
// We need to be able to downcast from `dyn Function` so that
60-
// we can implement `PartialReflect::try_apply`.
61-
impl_downcast!(Function);
57+
/// Clone this function into a [`DynamicFunction`].
58+
fn clone_dynamic(&self) -> DynamicFunction<'static>;
59+
}
6260

6361
#[cfg(test)]
6462
mod tests {

0 commit comments

Comments
 (0)