Added is_overloaded method

This commit is contained in:
Gino Valente 2024-09-05 11:45:56 -07:00
parent dbd3dc1e9b
commit f79672def3
3 changed files with 45 additions and 6 deletions

View file

@ -299,9 +299,7 @@ impl<'env> DynamicFunction<'env> {
let expected_arg_count = self.function_map.info().arg_count(); let expected_arg_count = self.function_map.info().arg_count();
let received_arg_count = args.len(); let received_arg_count = args.len();
if matches!(self.function_map.info(), FunctionInfoType::Standard(_)) if !self.is_overloaded() && expected_arg_count != received_arg_count {
&& expected_arg_count != received_arg_count
{
Err(FunctionError::ArgCountMismatch { Err(FunctionError::ArgCountMismatch {
expected: expected_arg_count, expected: expected_arg_count,
received: received_arg_count, received: received_arg_count,
@ -334,6 +332,24 @@ impl<'env> DynamicFunction<'env> {
pub fn name(&self) -> Option<&Cow<'static, str>> { pub fn name(&self) -> Option<&Cow<'static, str>> {
self.name.as_ref() 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> { impl Function for DynamicFunction<'static> {

View file

@ -269,9 +269,7 @@ impl<'env> DynamicFunctionMut<'env> {
let expected_arg_count = self.function_map.info().arg_count(); let expected_arg_count = self.function_map.info().arg_count();
let received_arg_count = args.len(); let received_arg_count = args.len();
if matches!(self.function_map.info(), FunctionInfoType::Standard(_)) if !self.is_overloaded() && expected_arg_count != received_arg_count {
&& expected_arg_count != received_arg_count
{
Err(FunctionError::ArgCountMismatch { Err(FunctionError::ArgCountMismatch {
expected: expected_arg_count, expected: expected_arg_count,
received: received_arg_count, received: received_arg_count,
@ -333,6 +331,26 @@ impl<'env> DynamicFunctionMut<'env> {
pub fn name(&self) -> Option<&Cow<'static, str>> { pub fn name(&self) -> Option<&Cow<'static, str>> {
self.name.as_ref() 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. /// Outputs the function's signature.

View file

@ -19,6 +19,11 @@ pub(super) enum FunctionMap<F> {
} }
impl<F> FunctionMap<F> { impl<F> FunctionMap<F> {
/// 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. /// Get a reference to a function in the map.
/// ///
/// If there is only one function in the map, it will be returned. /// If there is only one function in the map, it will be returned.