Skip to content

Commit 87fa86c

Browse files
committed
Removed need for Downcast on Function
1 parent 65edd1e commit 87fa86c

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

crates/bevy_reflect/src/func/dynamic_function.rs

Lines changed: 12 additions & 8 deletions
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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::func::{ArgList, FunctionInfo, FunctionResult};
1+
use crate::func::{ArgList, DynamicFunction, FunctionInfo, FunctionResult};
22
use crate::PartialReflect;
33
use alloc::borrow::Cow;
44
use core::fmt::Debug;
5-
use downcast_rs::{impl_downcast, Downcast};
65

76
/// A trait used to power [function-like] operations via [reflection].
87
///
@@ -31,7 +30,7 @@ use downcast_rs::{impl_downcast, Downcast};
3130
/// [`Reflect`]: crate::Reflect
3231
/// [arguments]: crate::func::args
3332
/// [`DynamicFunction`]: crate::func::DynamicFunction
34-
pub trait Function: PartialReflect + Downcast + Debug {
33+
pub trait Function: PartialReflect + Debug {
3534
/// The name of the function, if any.
3635
///
3736
/// For [`DynamicFunctions`] created using [`IntoFunction`],
@@ -55,11 +54,10 @@ pub trait Function: PartialReflect + Downcast + Debug {
5554

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

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

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

0 commit comments

Comments
 (0)