Skip to content

Commit

Permalink
Rename ArgList::push methods to with and add new push methods w…
Browse files Browse the repository at this point in the history
…hich take `&mut self` (bevyengine#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>
  • Loading branch information
atornity authored Jan 28, 2025
1 parent 514a35c commit 15f0027
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 153 deletions.
72 changes: 36 additions & 36 deletions benches/benches/bevy_reflect/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_reflect/src/func/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
/// ```
Expand Down Expand Up @@ -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);
/// ```
Expand Down Expand Up @@ -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);
/// ```
Expand Down
Loading

0 comments on commit 15f0027

Please sign in to comment.