Commit 0439f55
Reflect DynamicStruct field removal methods (#22702)
# Objective / Solution
Add methods for field removal on `DynamicStruct`:
```rust
DynamicStruct::remove_at( &mut self, index: usize ) -> Option<(Cow<'static, str>, Box<dyn PartialReflect>)>
DynamicStruct::remove_by_name( &mut self, name: &str ) -> Option<(Cow<'static, str>, Box<dyn PartialReflect>)>
DynamicStruct::remove_if( &mut self, f: FnMut((&str, &dyn PartialReflect))->bool ) -> Option<(Cow<'static, str>, Box<dyn PartialReflect>)>
```
## Testing
4 New Tests, located in
[bevy_reflect/structs.rs](https://github.com/bevyengine/bevy/blob/aa0c0b966c3463a6f39a5b961be91722b13dfb6a/crates/bevy_reflect/src/structs.rs):
`dynamic_struct_remove_at`
`dynamic_struct_remove_by_name`
`dynamic_struct_remove_with`
`dynamic_struct_remove_combo`
---
## Showcase
<details>
<summary>Click to view showcase</summary>
```rust
#[derive(Reflect, Default)]
struct MyStruct {
a: (),
b: (),
c: (),
}
#[test]
fn dynamic_struct_remove_combo() {
let mut my_struct = MyStruct::default().to_dynamic_struct();
assert_eq!(my_struct.field_len(), 3);
let field_2 = my_struct
.remove_at(
my_struct
.index_of(
my_struct
.field("b")
.expect("Invalid name for `my_struct.field(name)`"),
)
.expect("Invalid field for `my_struct.index_of(field)`"),
)
.expect("Invalid index for `my_struct.remove_at(index)`");
assert_eq!(my_struct.field_len(), 2);
assert_eq!(field_2.0, "b");
let field_3_name = my_struct
.name_of(
my_struct
.field_at(1)
.expect("Invalid index for `my_struct.field_at(index)`"),
)
.expect("Invalid field for `my_struct.name_of(field)`")
.to_owned();
let field_3 = my_struct
.remove_by_name(field_3_name.as_ref())
.expect("Invalid name for `my_struct.remove_by_name(name)`");
assert_eq!(my_struct.field_len(), 1);
assert_eq!(field_3.0, "c");
let field_1_name = my_struct
.name_at(0)
.expect("Invalid name for `my_struct.name_at(name)`")
.to_owned();
let field_1 = my_struct
.remove_if(|(name, _field)| name == field_1_name)
.expect("No valid name/field found for `my_struct.remove_if(|(name, field)|{})`");
assert_eq!(my_struct.field_len(), 0);
assert_eq!(field_1.0, "a");
}
```
</details>
---------
Co-authored-by: Gino Valente <[email protected]>1 parent 5f1ca6c commit 0439f55
2 files changed
+174
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
333 | 393 | | |
334 | 394 | | |
335 | 395 | | |
| |||
686 | 746 | | |
687 | 747 | | |
688 | 748 | | |
| 749 | + | |
689 | 750 | | |
690 | 751 | | |
691 | 752 | | |
692 | 753 | | |
693 | 754 | | |
694 | 755 | | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
695 | 868 | | |
696 | 869 | | |
697 | 870 | | |
| |||
0 commit comments