From f79672def3bbfa11292292bf71355115091f7402 Mon Sep 17 00:00:00 2001 From: Gino Valente Date: Thu, 5 Sep 2024 11:45:56 -0700 Subject: [PATCH] Added `is_overloaded` method --- .../bevy_reflect/src/func/dynamic_function.rs | 22 ++++++++++++++--- .../src/func/dynamic_function_mut.rs | 24 ++++++++++++++++--- crates/bevy_reflect/src/func/function_map.rs | 5 ++++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/crates/bevy_reflect/src/func/dynamic_function.rs b/crates/bevy_reflect/src/func/dynamic_function.rs index f227038ce2..e5e270b5ff 100644 --- a/crates/bevy_reflect/src/func/dynamic_function.rs +++ b/crates/bevy_reflect/src/func/dynamic_function.rs @@ -299,9 +299,7 @@ impl<'env> DynamicFunction<'env> { let expected_arg_count = self.function_map.info().arg_count(); let received_arg_count = args.len(); - if matches!(self.function_map.info(), FunctionInfoType::Standard(_)) - && expected_arg_count != received_arg_count - { + if !self.is_overloaded() && expected_arg_count != received_arg_count { Err(FunctionError::ArgCountMismatch { expected: expected_arg_count, received: received_arg_count, @@ -334,6 +332,24 @@ impl<'env> DynamicFunction<'env> { pub fn name(&self) -> Option<&Cow<'static, str>> { self.name.as_ref() } + + /// Returns `true` if the function is [overloaded]. + /// + /// # Example + /// + /// ``` + /// # use bevy_reflect::func::IntoFunction; + /// let add = (|a: i32, b: i32| a + b).into_function(); + /// assert!(!add.is_overloaded()); + /// + /// let add = add.with_overload(|a: f32, b: f32| a + b); + /// assert!(add.is_overloaded()); + /// ``` + /// + /// [overloaded]: Self::with_overload + pub fn is_overloaded(&self) -> bool { + self.function_map.is_overloaded() + } } impl Function for DynamicFunction<'static> { diff --git a/crates/bevy_reflect/src/func/dynamic_function_mut.rs b/crates/bevy_reflect/src/func/dynamic_function_mut.rs index 5f84fe58ae..bdd6823a92 100644 --- a/crates/bevy_reflect/src/func/dynamic_function_mut.rs +++ b/crates/bevy_reflect/src/func/dynamic_function_mut.rs @@ -269,9 +269,7 @@ impl<'env> DynamicFunctionMut<'env> { let expected_arg_count = self.function_map.info().arg_count(); let received_arg_count = args.len(); - if matches!(self.function_map.info(), FunctionInfoType::Standard(_)) - && expected_arg_count != received_arg_count - { + if !self.is_overloaded() && expected_arg_count != received_arg_count { Err(FunctionError::ArgCountMismatch { expected: expected_arg_count, received: received_arg_count, @@ -333,6 +331,26 @@ impl<'env> DynamicFunctionMut<'env> { pub fn name(&self) -> Option<&Cow<'static, str>> { self.name.as_ref() } + + /// Returns `true` if the function is [overloaded]. + /// + /// # Example + /// + /// ``` + /// # use bevy_reflect::func::IntoFunctionMut; + /// let mut total_i32 = 0; + /// let increment = (|value: i32| total_i32 += value).into_function_mut(); + /// assert!(!increment.is_overloaded()); + /// + /// let mut total_f32 = 0.0; + /// let increment = increment.with_overload(|value: f32| total_f32 += value); + /// assert!(increment.is_overloaded()); + /// ``` + /// + /// [overloaded]: Self::with_overload + pub fn is_overloaded(&self) -> bool { + self.function_map.is_overloaded() + } } /// Outputs the function's signature. diff --git a/crates/bevy_reflect/src/func/function_map.rs b/crates/bevy_reflect/src/func/function_map.rs index 2fcf708d1f..b7e672fc1a 100644 --- a/crates/bevy_reflect/src/func/function_map.rs +++ b/crates/bevy_reflect/src/func/function_map.rs @@ -19,6 +19,11 @@ pub(super) enum FunctionMap { } impl FunctionMap { + /// Returns `true` if the map contains an overloaded function. + pub fn is_overloaded(&self) -> bool { + matches!(self, Self::Overloaded(..)) + } + /// Get a reference to a function in the map. /// /// If there is only one function in the map, it will be returned.