Rename ArgList::push methods to with and add new push methods which take &mut self (#16567)

# Objective

The `ArgList::push` family of methods consume `self` and return a new
`ArgList` which means they can't be used with `&mut ArgList` references.

```rust
fn foo(args: &mut ArgList) {
    args.push_owned(47_i32); // doesn't work :(
}
```

It's typical for `push` methods on other existing types to take `&mut
self`.

## Solution

Renamed the existing push methods to `with_arg`, `with_ref` etc and
added new `push` methods which take `&mut self`.

## Migration Guide

Uses of the `ArgList::push` methods should be replaced with the `with`
counterpart.

<details>

| old | new |
| --- | --- |
| push_arg | with_arg |
| push_ref | with_ref |
| push_mut | with_mut |
| push_owned | with_owned | 
| push_boxed | with_boxed |

</details>
This commit is contained in:
poopy 2025-01-28 06:06:50 +01:00 committed by GitHub
parent 514a35c656
commit 15f00278e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 193 additions and 153 deletions

View File

@ -65,7 +65,7 @@ fn call(c: &mut Criterion) {
.bench_function("function", |b| {
let add = add.into_function();
b.iter_batched(
|| ArgList::new().push_owned(75_i32).push_owned(25_i32),
|| ArgList::new().with_owned(75_i32).with_owned(25_i32),
|args| add.call(args),
BatchSize::SmallInput,
);
@ -74,7 +74,7 @@ fn call(c: &mut Criterion) {
let capture = 25;
let add = (|a: i32| a + capture).into_function();
b.iter_batched(
|| ArgList::new().push_owned(75_i32),
|| ArgList::new().with_owned(75_i32),
|args| add.call(args),
BatchSize::SmallInput,
);
@ -83,7 +83,7 @@ fn call(c: &mut Criterion) {
let mut capture = 25;
let mut add = (|a: i32| capture += a).into_function_mut();
b.iter_batched(
|| ArgList::new().push_owned(75_i32),
|| ArgList::new().with_owned(75_i32),
|args| add.call(args),
BatchSize::SmallInput,
);
@ -246,7 +246,7 @@ fn call_overload(c: &mut Criterion) {
|| {
(
simple::<i8>.into_function().with_overload(simple::<i16>),
ArgList::new().push_owned(75_i8).push_owned(25_i8),
ArgList::new().with_owned(75_i8).with_owned(25_i8),
)
},
|(func, args)| func.call(args),
@ -263,16 +263,16 @@ fn call_overload(c: &mut Criterion) {
complex::<i16, i32, i64, i128, u8, u16, u32, u64, u128, i8>,
),
ArgList::new()
.push_owned(1_i8)
.push_owned(2_i16)
.push_owned(3_i32)
.push_owned(4_i64)
.push_owned(5_i128)
.push_owned(6_u8)
.push_owned(7_u16)
.push_owned(8_u32)
.push_owned(9_u64)
.push_owned(10_u128),
.with_owned(1_i8)
.with_owned(2_i16)
.with_owned(3_i32)
.with_owned(4_i64)
.with_owned(5_i128)
.with_owned(6_u8)
.with_owned(7_u16)
.with_owned(8_u32)
.with_owned(9_u64)
.with_owned(10_u128),
)
},
|(func, args)| func.call(args),
@ -288,7 +288,7 @@ fn call_overload(c: &mut Criterion) {
.with_overload(simple::<i16>)
.with_overload(simple::<i32>)
.with_overload(simple::<i64>),
ArgList::new().push_owned(75_i32).push_owned(25_i32),
ArgList::new().with_owned(75_i32).with_owned(25_i32),
)
},
|(func, args)| func.call(args),
@ -311,16 +311,16 @@ fn call_overload(c: &mut Criterion) {
complex::<i64, i128, u8, u16, u32, u64, u128, i8, i16, i32>,
),
ArgList::new()
.push_owned(1_i32)
.push_owned(2_i64)
.push_owned(3_i128)
.push_owned(4_u8)
.push_owned(5_u16)
.push_owned(6_u32)
.push_owned(7_u64)
.push_owned(8_u128)
.push_owned(9_i8)
.push_owned(10_i16),
.with_owned(1_i32)
.with_owned(2_i64)
.with_owned(3_i128)
.with_owned(4_u8)
.with_owned(5_u16)
.with_owned(6_u32)
.with_owned(7_u64)
.with_owned(8_u128)
.with_owned(9_i8)
.with_owned(10_i16),
)
},
|(func, args)| func.call(args),
@ -342,7 +342,7 @@ fn call_overload(c: &mut Criterion) {
.with_overload(simple::<u32>)
.with_overload(simple::<u64>)
.with_overload(simple::<u128>),
ArgList::new().push_owned(75_u8).push_owned(25_u8),
ArgList::new().with_owned(75_u8).with_owned(25_u8),
)
},
|(func, args)| func.call(args),
@ -383,16 +383,16 @@ fn call_overload(c: &mut Criterion) {
complex::<u128, i8, i16, i32, i64, i128, u8, u16, u32, u64>,
),
ArgList::new()
.push_owned(1_u8)
.push_owned(2_u16)
.push_owned(3_u32)
.push_owned(4_u64)
.push_owned(5_u128)
.push_owned(6_i8)
.push_owned(7_i16)
.push_owned(8_i32)
.push_owned(9_i64)
.push_owned(10_i128),
.with_owned(1_u8)
.with_owned(2_u16)
.with_owned(3_u32)
.with_owned(4_u64)
.with_owned(5_u128)
.with_owned(6_i8)
.with_owned(7_i16)
.with_owned(8_i32)
.with_owned(9_i64)
.with_owned(10_i128),
)
},
|(func, args)| func.call(args),

View File

@ -52,7 +52,7 @@ impl<'a> Arg<'a> {
/// let a = 1u32;
/// let b = 2u32;
/// let mut c = 3u32;
/// let mut args = ArgList::new().push_owned(a).push_ref(&b).push_mut(&mut c);
/// let mut args = ArgList::new().with_owned(a).with_ref(&b).with_mut(&mut c);
///
/// let a = args.take::<u32>().unwrap();
/// assert_eq!(a, 1);
@ -78,7 +78,7 @@ impl<'a> Arg<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let value = 123u32;
/// let mut args = ArgList::new().push_owned(value);
/// let mut args = ArgList::new().with_owned(value);
/// let value = args.take_owned::<u32>().unwrap();
/// assert_eq!(value, 123);
/// ```
@ -113,7 +113,7 @@ impl<'a> Arg<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let value = 123u32;
/// let mut args = ArgList::new().push_ref(&value);
/// let mut args = ArgList::new().with_ref(&value);
/// let value = args.take_ref::<u32>().unwrap();
/// assert_eq!(*value, 123);
/// ```
@ -152,7 +152,7 @@ impl<'a> Arg<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let mut value = 123u32;
/// let mut args = ArgList::new().push_mut(&mut value);
/// let mut args = ArgList::new().with_mut(&mut value);
/// let value = args.take_mut::<u32>().unwrap();
/// assert_eq!(*value, 123);
/// ```

View File

@ -21,15 +21,15 @@ use alloc::{
/// let mut baz = 789;
/// let args = ArgList::new()
/// // Push an owned argument
/// .push_owned(foo)
/// .with_owned(foo)
/// // Push an owned and boxed argument
/// .push_boxed(Box::new(foo))
/// .with_boxed(Box::new(foo))
/// // Push a reference argument
/// .push_ref(&bar)
/// .with_ref(&bar)
/// // Push a mutable reference argument
/// .push_mut(&mut baz)
/// .with_mut(&mut baz)
/// // Push a manually constructed argument
/// .push_arg(ArgValue::Ref(&3.14));
/// .with_arg(ArgValue::Ref(&3.14));
/// ```
///
/// [arguments]: Arg
@ -58,7 +58,7 @@ impl<'a> ArgList<'a> {
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_arg(mut self, arg: ArgValue<'a>) -> Self {
pub fn push_arg(&mut self, arg: ArgValue<'a>) {
if self.needs_reindex {
for (index, arg) in self.list.iter_mut().enumerate() {
arg.set_index(index);
@ -68,6 +68,46 @@ impl<'a> ArgList<'a> {
let index = self.list.len();
self.list.push_back(Arg::new(index, arg));
}
/// Push an [`ArgValue::Ref`] onto the list with the given reference.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_ref(&mut self, arg: &'a dyn PartialReflect) {
self.push_arg(ArgValue::Ref(arg));
}
/// Push an [`ArgValue::Mut`] onto the list with the given mutable reference.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_mut(&mut self, arg: &'a mut dyn PartialReflect) {
self.push_arg(ArgValue::Mut(arg));
}
/// Push an [`ArgValue::Owned`] onto the list with the given owned value.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_owned(&mut self, arg: impl PartialReflect) {
self.push_arg(ArgValue::Owned(Box::new(arg)));
}
/// Push an [`ArgValue::Owned`] onto the list with the given boxed value.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_boxed(&mut self, arg: Box<dyn PartialReflect>) {
self.push_arg(ArgValue::Owned(arg));
}
/// Push an [`ArgValue`] onto the list.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn with_arg(mut self, arg: ArgValue<'a>) -> Self {
self.push_arg(arg);
self
}
@ -75,32 +115,32 @@ impl<'a> ArgList<'a> {
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_ref(self, arg: &'a dyn PartialReflect) -> Self {
self.push_arg(ArgValue::Ref(arg))
pub fn with_ref(self, arg: &'a dyn PartialReflect) -> Self {
self.with_arg(ArgValue::Ref(arg))
}
/// Push an [`ArgValue::Mut`] onto the list with the given mutable reference.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_mut(self, arg: &'a mut dyn PartialReflect) -> Self {
self.push_arg(ArgValue::Mut(arg))
pub fn with_mut(self, arg: &'a mut dyn PartialReflect) -> Self {
self.with_arg(ArgValue::Mut(arg))
}
/// Push an [`ArgValue::Owned`] onto the list with the given owned value.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_owned(self, arg: impl PartialReflect) -> Self {
self.push_arg(ArgValue::Owned(Box::new(arg)))
pub fn with_owned(self, arg: impl PartialReflect) -> Self {
self.with_arg(ArgValue::Owned(Box::new(arg)))
}
/// Push an [`ArgValue::Owned`] onto the list with the given boxed value.
///
/// If an argument was previously removed from the beginning of the list,
/// this method will also re-index the list.
pub fn push_boxed(self, arg: Box<dyn PartialReflect>) -> Self {
self.push_arg(ArgValue::Owned(arg))
pub fn with_boxed(self, arg: Box<dyn PartialReflect>) -> Self {
self.with_arg(ArgValue::Owned(arg))
}
/// Remove the first argument in the list and return it.
@ -123,7 +163,7 @@ impl<'a> ArgList<'a> {
/// let a = 1u32;
/// let b = 2u32;
/// let mut c = 3u32;
/// let mut args = ArgList::new().push_owned(a).push_ref(&b).push_mut(&mut c);
/// let mut args = ArgList::new().with_owned(a).with_ref(&b).with_mut(&mut c);
///
/// let a = args.take::<u32>().unwrap();
/// assert_eq!(a, 1);
@ -149,7 +189,7 @@ impl<'a> ArgList<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let value = 123u32;
/// let mut args = ArgList::new().push_owned(value);
/// let mut args = ArgList::new().with_owned(value);
/// let value = args.take_owned::<u32>().unwrap();
/// assert_eq!(value, 123);
/// ```
@ -168,7 +208,7 @@ impl<'a> ArgList<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let value = 123u32;
/// let mut args = ArgList::new().push_ref(&value);
/// let mut args = ArgList::new().with_ref(&value);
/// let value = args.take_ref::<u32>().unwrap();
/// assert_eq!(*value, 123);
/// ```
@ -187,7 +227,7 @@ impl<'a> ArgList<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let mut value = 123u32;
/// let mut args = ArgList::new().push_mut(&mut value);
/// let mut args = ArgList::new().with_mut(&mut value);
/// let value = args.take_mut::<u32>().unwrap();
/// assert_eq!(*value, 123);
/// ```
@ -214,7 +254,7 @@ impl<'a> ArgList<'a> {
/// let a = 1u32;
/// let b = 2u32;
/// let mut c = 3u32;
/// let mut args = ArgList::new().push_owned(a).push_ref(&b).push_mut(&mut c);
/// let mut args = ArgList::new().with_owned(a).with_ref(&b).with_mut(&mut c);
///
/// let c = args.pop::<&mut u32>().unwrap();
/// assert_eq!(*c, 3);
@ -240,7 +280,7 @@ impl<'a> ArgList<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let value = 123u32;
/// let mut args = ArgList::new().push_owned(value);
/// let mut args = ArgList::new().with_owned(value);
/// let value = args.pop_owned::<u32>().unwrap();
/// assert_eq!(value, 123);
/// ```
@ -259,7 +299,7 @@ impl<'a> ArgList<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let value = 123u32;
/// let mut args = ArgList::new().push_ref(&value);
/// let mut args = ArgList::new().with_ref(&value);
/// let value = args.pop_ref::<u32>().unwrap();
/// assert_eq!(*value, 123);
/// ```
@ -278,7 +318,7 @@ impl<'a> ArgList<'a> {
/// ```
/// # use bevy_reflect::func::ArgList;
/// let mut value = 123u32;
/// let mut args = ArgList::new().push_mut(&mut value);
/// let mut args = ArgList::new().with_mut(&mut value);
/// let value = args.pop_mut::<u32>().unwrap();
/// assert_eq!(*value, 123);
/// ```
@ -310,9 +350,9 @@ mod tests {
#[test]
fn should_push_arguments_in_order() {
let args = ArgList::new()
.push_owned(123)
.push_owned(456)
.push_owned(789);
.with_owned(123)
.with_owned(456)
.with_owned(789);
assert_eq!(args.len(), 3);
assert_eq!(args.list[0].index(), 0);
@ -331,13 +371,13 @@ mod tests {
let mut g = String::from("g");
let args = ArgList::new()
.push_arg(ArgValue::Owned(Box::new(a)))
.push_arg(ArgValue::Ref(&b))
.push_arg(ArgValue::Mut(&mut c))
.push_owned(d)
.push_boxed(Box::new(e))
.push_ref(&f)
.push_mut(&mut g);
.with_arg(ArgValue::Owned(Box::new(a)))
.with_arg(ArgValue::Ref(&b))
.with_arg(ArgValue::Mut(&mut c))
.with_owned(d)
.with_boxed(Box::new(e))
.with_ref(&f)
.with_mut(&mut g);
assert!(matches!(args.list[0].value(), &ArgValue::Owned(_)));
assert!(matches!(args.list[1].value(), &ArgValue::Ref(_)));
@ -356,10 +396,10 @@ mod tests {
let mut d = 5.78_f32;
let mut args = ArgList::new()
.push_owned(a)
.push_ref(&b)
.push_ref(&c)
.push_mut(&mut d);
.with_owned(a)
.with_ref(&b)
.with_ref(&c)
.with_mut(&mut d);
assert_eq!(args.len(), 4);
assert_eq!(args.take_owned::<String>().unwrap(), String::from("a"));
@ -377,10 +417,10 @@ mod tests {
let mut d = 5.78_f32;
let mut args = ArgList::new()
.push_owned(a)
.push_ref(&b)
.push_ref(&c)
.push_mut(&mut d);
.with_owned(a)
.with_ref(&b)
.with_ref(&c)
.with_mut(&mut d);
assert_eq!(args.len(), 4);
assert_eq!(args.pop_mut::<f32>().unwrap(), &mut 5.78);
@ -393,9 +433,9 @@ mod tests {
#[test]
fn should_reindex_on_push_after_take() {
let mut args = ArgList::new()
.push_owned(123)
.push_owned(456)
.push_owned(789);
.with_owned(123)
.with_owned(456)
.with_owned(789);
assert!(!args.needs_reindex);
@ -406,7 +446,7 @@ mod tests {
assert!(args.list[1].value().reflect_partial_eq(&789).unwrap());
assert_eq!(args.list[1].index(), 2);
let args = args.push_owned(123);
let args = args.with_owned(123);
assert!(!args.needs_reindex);
assert!(args.list[0].value().reflect_partial_eq(&456).unwrap());
assert_eq!(args.list[0].index(), 0);

View File

@ -55,7 +55,7 @@ type ArcFn<'env> = Arc<dyn for<'a> Fn(ArgList<'a>) -> FunctionResult<'a> + Send
/// let mut func: DynamicFunction = add.into_function();
///
/// // Dynamically call it:
/// let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
/// let value = func.call(args).unwrap().unwrap_owned();
///
/// // Check the result:
@ -161,12 +161,12 @@ impl<'env> DynamicFunction<'env> {
/// func = func.with_overload(add::<f32>);
///
/// // Test `i32`:
/// let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
/// let result = func.call(args).unwrap().unwrap_owned();
/// assert_eq!(result.try_take::<i32>().unwrap(), 100);
///
/// // Test `f32`:
/// let args = ArgList::default().push_owned(25.0_f32).push_owned(75.0_f32);
/// let args = ArgList::default().with_owned(25.0_f32).with_owned(75.0_f32);
/// let result = func.call(args).unwrap().unwrap_owned();
/// assert_eq!(result.try_take::<f32>().unwrap(), 100.0);
///```
@ -189,15 +189,15 @@ impl<'env> DynamicFunction<'env> {
/// func = func.with_overload(add_3);
///
/// // Test two arguments:
/// let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
/// let result = func.call(args).unwrap().unwrap_owned();
/// assert_eq!(result.try_take::<i32>().unwrap(), 100);
///
/// // Test three arguments:
/// let args = ArgList::default()
/// .push_owned(25_i32)
/// .push_owned(75_i32)
/// .push_owned(100_i32);
/// .with_owned(25_i32)
/// .with_owned(75_i32)
/// .with_owned(100_i32);
/// let result = func.call(args).unwrap().unwrap_owned();
/// assert_eq!(result.try_take::<i32>().unwrap(), 200);
/// ```
@ -266,7 +266,7 @@ impl<'env> DynamicFunction<'env> {
/// };
///
/// let mut func = add.into_function().with_name("add");
/// let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
/// let result = func.call(args).unwrap().unwrap_owned();
/// assert_eq!(result.try_take::<i32>().unwrap(), 123);
/// ```
@ -513,7 +513,7 @@ mod tests {
fn should_return_error_on_arg_count_mismatch() {
let func = (|a: i32, b: i32| a + b).into_function();
let args = ArgList::default().push_owned(25_i32);
let args = ArgList::default().with_owned(25_i32);
let error = func.call(args).unwrap_err();
assert_eq!(
@ -532,10 +532,10 @@ mod tests {
.with_overload(|a: i32, b: i32, c: i32| a + b + c);
let args = ArgList::default()
.push_owned(1_i32)
.push_owned(2_i32)
.push_owned(3_i32)
.push_owned(4_i32);
.with_owned(1_i32)
.with_owned(2_i32)
.with_owned(3_i32)
.with_owned(4_i32);
let error = func.call(args).unwrap_err();
@ -564,7 +564,7 @@ mod tests {
assert_eq!(clone.name().unwrap(), "greet");
let clone_value = clone
.call(ArgList::default().push_ref(&String::from("world")))
.call(ArgList::default().with_ref(&String::from("world")))
.unwrap()
.unwrap_owned()
.try_take::<String>()
@ -578,7 +578,7 @@ mod tests {
let mut func: Box<dyn Function> = Box::new((|a: i32, b: i32| a + b).into_function());
func.apply(&((|a: i32, b: i32| a * b).into_function()));
let args = ArgList::new().push_owned(5_i32).push_owned(5_i32);
let args = ArgList::new().with_owned(5_i32).with_owned(5_i32);
let result = func.reflect_call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 25);
}
@ -599,8 +599,8 @@ mod tests {
ReflectRef::Function(func) => {
let result = func.reflect_call(
ArgList::new()
.push_ref(this.as_partial_reflect())
.push_owned(curr - 1),
.with_ref(this.as_partial_reflect())
.with_owned(curr - 1),
);
let value = result.unwrap().unwrap_owned().try_take::<i32>().unwrap();
Ok((curr * value).into_return())
@ -615,7 +615,7 @@ mod tests {
.with_arg::<()>("this"),
);
let args = ArgList::new().push_ref(&factorial).push_owned(5_i32);
let args = ArgList::new().with_ref(&factorial).with_owned(5_i32);
let value = factorial.call(args).unwrap().unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 120);
}
@ -653,11 +653,11 @@ mod tests {
let func = func.with_name("add");
assert_eq!(func.name().unwrap(), "add");
let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 100);
let args = ArgList::default().push_owned(25.0_f32).push_owned(75.0_f32);
let args = ArgList::default().with_owned(25.0_f32).with_owned(75.0_f32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<f32>().unwrap(), 100.0);
}
@ -676,11 +676,11 @@ mod tests {
let func = add::<i32>.into_function().with_overload(add::<f32>);
let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 100);
let args = ArgList::default().push_owned(25.0_f32).push_owned(75.0_f32);
let args = ArgList::default().with_owned(25.0_f32).with_owned(75.0_f32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<f32>().unwrap(), 100.0);
}
@ -697,14 +697,14 @@ mod tests {
let func = add_2.into_function().with_overload(add_3);
let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 100);
let args = ArgList::default()
.push_owned(25_i32)
.push_owned(75_i32)
.push_owned(100_i32);
.with_owned(25_i32)
.with_owned(75_i32)
.with_owned(100_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 200);
}
@ -740,11 +740,11 @@ mod tests {
let func = manual.with_overload(|a: u32, b: u32| a + b);
let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 100);
let args = ArgList::default().push_owned(25_u32).push_owned(75_u32);
let args = ArgList::default().with_owned(25_u32).with_owned(75_u32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<u32>().unwrap(), 100);
}
@ -757,7 +757,7 @@ mod tests {
let func = add::<i32>.into_function().with_overload(add::<f32>);
let args = ArgList::default().push_owned(25_u32).push_owned(75_u32);
let args = ArgList::default().with_owned(25_u32).with_owned(75_u32);
let result = func.call(args);
assert_eq!(
result.unwrap_err(),

View File

@ -51,7 +51,7 @@ type BoxFnMut<'env> = Box<dyn for<'a> FnMut(ArgList<'a>) -> FunctionResult<'a> +
/// let mut func: DynamicFunctionMut = replace.into_function_mut();
///
/// // Dynamically call it:
/// let args = ArgList::default().push_owned(1_usize).push_owned(-2_i32);
/// let args = ArgList::default().with_owned(1_usize).with_owned(-2_i32);
/// let value = func.call(args).unwrap().unwrap_owned();
///
/// // Check the result:
@ -155,11 +155,11 @@ impl<'env> DynamicFunctionMut<'env> {
/// func = func.with_overload(add_f32);
///
/// // Test `i32`:
/// let args = bevy_reflect::func::ArgList::new().push_owned(123_i32);
/// let args = bevy_reflect::func::ArgList::new().with_owned(123_i32);
/// func.call(args).unwrap();
///
/// // Test `f32`:
/// let args = bevy_reflect::func::ArgList::new().push_owned(1.23_f32);
/// let args = bevy_reflect::func::ArgList::new().with_owned(1.23_f32);
/// func.call(args).unwrap();
///
/// drop(func);
@ -220,7 +220,7 @@ impl<'env> DynamicFunctionMut<'env> {
/// };
///
/// let mut func = add.into_function_mut().with_name("add");
/// let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
/// let result = func.call(args).unwrap().unwrap_owned();
/// assert_eq!(result.try_take::<i32>().unwrap(), 100);
/// ```
@ -252,7 +252,7 @@ impl<'env> DynamicFunctionMut<'env> {
/// let increment = |amount: i32| count += amount;
///
/// let increment_function = increment.into_function_mut();
/// let args = ArgList::new().push_owned(5_i32);
/// let args = ArgList::new().with_owned(5_i32);
///
/// // We need to drop `increment_function` here so that we
/// // can regain access to `count`.
@ -408,7 +408,7 @@ mod tests {
let mut total = 0;
let mut func = (|a: i32, b: i32| total = a + b).into_function_mut();
let args = ArgList::default().push_owned(25_i32);
let args = ArgList::default().with_owned(25_i32);
let error = func.call(args).unwrap_err();
assert_eq!(
error,
@ -418,7 +418,7 @@ mod tests {
}
);
let args = ArgList::default().push_owned(25_i32);
let args = ArgList::default().with_owned(25_i32);
let error = func.call_once(args).unwrap_err();
assert_eq!(
error,
@ -456,9 +456,9 @@ mod tests {
let mut func = func.with_name("add");
assert_eq!(func.name().unwrap(), "add");
let args = ArgList::default().push_owned(25_i32);
let args = ArgList::default().with_owned(25_i32);
func.call(args).unwrap();
let args = ArgList::default().push_owned(75_i16);
let args = ArgList::default().with_owned(75_i16);
func.call(args).unwrap();
drop(func);
@ -477,11 +477,11 @@ mod tests {
let mut func = add::<i32>.into_function_mut().with_overload(add::<f32>);
let args = ArgList::default().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::default().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<i32>().unwrap(), 100);
let args = ArgList::default().push_owned(25.0_f32).push_owned(75.0_f32);
let args = ArgList::default().with_owned(25.0_f32).with_owned(75.0_f32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_take::<f32>().unwrap(), 100.0);
}

View File

@ -25,7 +25,7 @@ use core::fmt::Debug;
/// }
///
/// let func: Box<dyn Function> = Box::new(add.into_function());
/// let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
/// let value = func.reflect_call(args).unwrap().unwrap_owned();
/// assert_eq!(value.try_take::<i32>().unwrap(), 100);
/// ```
@ -80,7 +80,7 @@ mod tests {
}
let func: Box<dyn Function> = Box::new(add.into_function());
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let value = func.reflect_call(args).unwrap().unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 100);
}

View File

@ -42,7 +42,7 @@ mod tests {
fn should_create_dynamic_function_from_closure() {
let c = 23;
let func = (|a: i32, b: i32| a + b + c).into_function();
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_downcast_ref::<i32>(), Some(&123));
}
@ -54,7 +54,7 @@ mod tests {
}
let func = add.into_function();
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_downcast_ref::<i32>(), Some(&100));
}

View File

@ -48,7 +48,7 @@ mod tests {
fn should_create_dynamic_function_mut_from_closure() {
let c = 23;
let func = (|a: i32, b: i32| a + b + c).into_function();
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_downcast_ref::<i32>(), Some(&123));
}
@ -57,7 +57,7 @@ mod tests {
fn should_create_dynamic_function_mut_from_closure_with_mutable_capture() {
let mut total = 0;
let func = (|a: i32, b: i32| total = a + b).into_function_mut();
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
func.call_once(args).unwrap();
assert_eq!(total, 100);
}
@ -69,7 +69,7 @@ mod tests {
}
let mut func = add.into_function_mut();
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let result = func.call(args).unwrap().unwrap_owned();
assert_eq!(result.try_downcast_ref::<i32>(), Some(&100));
}

View File

@ -25,9 +25,9 @@
//! let mut func: DynamicFunction = add.into_function();
//! let args: ArgList = ArgList::default()
//! // Pushing a known type with owned ownership
//! .push_owned(25_i32)
//! .with_owned(25_i32)
//! // Pushing a reflected type with owned ownership
//! .push_boxed(Box::new(75_i32) as Box<dyn PartialReflect>);
//! .with_boxed(Box::new(75_i32) as Box<dyn PartialReflect>);
//! let result: FunctionResult = func.call(args);
//! let value: Return = result.unwrap();
//! assert_eq!(value.unwrap_owned().try_downcast_ref::<i32>(), Some(&100));
@ -141,11 +141,11 @@
//!
//! // You can then retrieve and call these functions by name:
//! let reflect_add = registry.get(core::any::type_name_of_val(&add)).unwrap();
//! let value = reflect_add.call(ArgList::default().push_owned(10_i32).push_owned(5_i32)).unwrap();
//! let value = reflect_add.call(ArgList::default().with_owned(10_i32).with_owned(5_i32)).unwrap();
//! assert_eq!(value.unwrap_owned().try_downcast_ref::<i32>(), Some(&15));
//!
//! let reflect_mul = registry.get("mul").unwrap();
//! let value = reflect_mul.call(ArgList::default().push_owned(10_i32).push_owned(5_i32)).unwrap();
//! let value = reflect_mul.call(ArgList::default().with_owned(10_i32).with_owned(5_i32)).unwrap();
//! assert_eq!(value.unwrap_owned().try_downcast_ref::<i32>(), Some(&50));
//! ```
//!
@ -219,7 +219,7 @@ mod tests {
fn foo() {}
let func = foo.into_function();
let args = ArgList::new().push_owned(123_i32);
let args = ArgList::new().with_owned(123_i32);
let result = func.call(args);
assert_eq!(
result.unwrap_err(),
@ -235,7 +235,7 @@ mod tests {
fn foo(_: i32) {}
let func = foo.into_function();
let args = ArgList::new().push_owned(123_u32);
let args = ArgList::new().with_owned(123_u32);
let result = func.call(args);
assert_eq!(
result.unwrap_err(),
@ -252,7 +252,7 @@ mod tests {
fn foo(_: &i32) {}
let func = foo.into_function();
let args = ArgList::new().push_owned(123_i32);
let args = ArgList::new().with_owned(123_i32);
let result = func.call(args);
assert_eq!(
result.unwrap_err(),

View File

@ -44,7 +44,7 @@ use crate::{
/// a + b
/// }
///
/// let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
/// let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
///
/// let value = add.reflect_call(args).unwrap().unwrap_owned();
/// assert_eq!(value.try_take::<i32>().unwrap(), 100);

View File

@ -50,7 +50,7 @@ use crate::{
/// list.insert(index, value);
/// };
///
/// let args = ArgList::new().push_owned(1_usize).push_owned(2_i32);
/// let args = ArgList::new().with_owned(1_usize).with_owned(2_i32);
///
/// insert.reflect_call_mut(args).unwrap();
/// assert_eq!(list, vec![1, 2, 3]);

View File

@ -487,7 +487,7 @@ mod tests {
let mut registry = FunctionRegistry::default();
registry.register(add).unwrap();
let args = ArgList::new().push_owned(25_i32).push_owned(75_i32);
let args = ArgList::new().with_owned(25_i32).with_owned(75_i32);
let result = registry
.call(core::any::type_name_of_val(&add), args)
.unwrap();

View File

@ -44,7 +44,7 @@ fn main() {
// This time, you'll notice that `DynamicFunction` doesn't take any information about the function's arguments or return value.
// This is because `DynamicFunction` checks the types of the arguments and return value at runtime.
// Now we can generate a list of arguments:
let args: ArgList = dbg!(ArgList::new().push_owned(2_i32).push_owned(2_i32));
let args: ArgList = dbg!(ArgList::new().with_owned(2_i32).with_owned(2_i32));
// And finally, we can call the function.
// This returns a `Result` indicating whether the function was called successfully.
@ -64,7 +64,7 @@ fn main() {
let clamp = |value: i32| value.max(minimum);
let function: DynamicFunction = dbg!(clamp.into_function());
let args = dbg!(ArgList::new().push_owned(2_i32));
let args = dbg!(ArgList::new().with_owned(2_i32));
let return_value = dbg!(function.call(args).unwrap());
let value: Box<dyn PartialReflect> = return_value.unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 5);
@ -75,7 +75,7 @@ fn main() {
let increment = |amount: i32| count += amount;
let closure: DynamicFunctionMut = dbg!(increment.into_function_mut());
let args = dbg!(ArgList::new().push_owned(5_i32));
let args = dbg!(ArgList::new().with_owned(5_i32));
// Because `DynamicFunctionMut` mutably borrows `total`,
// it will need to be dropped before `total` can be accessed again.
@ -92,7 +92,7 @@ fn main() {
// We have to manually specify the concrete generic type we want to use.
let function = stringify::<i32>.into_function();
let args = ArgList::new().push_owned(123_i32);
let args = ArgList::new().with_owned(123_i32);
let return_value = function.call(args).unwrap();
let value: Box<dyn PartialReflect> = return_value.unwrap_owned();
assert_eq!(value.try_take::<String>().unwrap(), "123");
@ -106,7 +106,7 @@ fn main() {
.with_overload(stringify::<f32>);
// Now our `function` accepts both `i32` and `f32` arguments.
let args = ArgList::new().push_owned(1.23_f32);
let args = ArgList::new().with_owned(1.23_f32);
let return_value = function.call(args).unwrap();
let value: Box<dyn PartialReflect> = return_value.unwrap_owned();
assert_eq!(value.try_take::<String>().unwrap(), "1.23");
@ -119,9 +119,9 @@ fn main() {
.with_overload(|a: i32, b: i32, c: i32| a + b + c);
let args = ArgList::new()
.push_owned(1_i32)
.push_owned(2_i32)
.push_owned(3_i32);
.with_owned(1_i32)
.with_owned(2_i32)
.with_owned(3_i32);
let return_value = function.call(args).unwrap();
let value: Box<dyn PartialReflect> = return_value.unwrap_owned();
assert_eq!(value.try_take::<i32>().unwrap(), 6);
@ -151,12 +151,12 @@ fn main() {
let mut data = Data::default();
let set_value = dbg!(Data::set_value.into_function());
let args = dbg!(ArgList::new().push_mut(&mut data)).push_owned(String::from("Hello, world!"));
let args = dbg!(ArgList::new().with_mut(&mut data)).with_owned(String::from("Hello, world!"));
dbg!(set_value.call(args).unwrap());
assert_eq!(data.value, "Hello, world!");
let get_value = dbg!(Data::get_value.into_function());
let args = dbg!(ArgList::new().push_ref(&data));
let args = dbg!(ArgList::new().with_ref(&data));
let return_value = dbg!(get_value.call(args).unwrap());
let value: &dyn PartialReflect = return_value.unwrap_ref();
assert_eq!(value.try_downcast_ref::<String>().unwrap(), "Hello, world!");
@ -210,11 +210,11 @@ fn main() {
let mut container: Option<i32> = None;
let args = dbg!(ArgList::new().push_owned(5_i32).push_mut(&mut container));
let args = dbg!(ArgList::new().with_owned(5_i32).with_mut(&mut container));
let value = dbg!(get_or_insert_function.call(args).unwrap()).unwrap_ref();
assert_eq!(value.try_downcast_ref::<i32>(), Some(&5));
let args = dbg!(ArgList::new().push_owned(500_i32).push_mut(&mut container));
let args = dbg!(ArgList::new().with_owned(500_i32).with_mut(&mut container));
let value = dbg!(get_or_insert_function.call(args).unwrap()).unwrap_ref();
assert_eq!(value.try_downcast_ref::<i32>(), Some(&5));
}