bevyengine / bevyengine/bevy

reflect: make structural methods (e.g. `DynamicTuple::insert`) take by value

Open
#7,122 0 comments 0 reactions 0 assignees View on GitHub
A-Reflection C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?

Dynamic types should behave as closely as reasonably possible to their concrete equivalents. Tuples have a compile-time-fixed number of elements (and structs a fixed number of fields) that cannot be modified by mutable reference. Whereas `insert(_boxed)` on `DynamicTuple`, `DynamicStruct` and `DynamicTupleStruct` allows for element/field insertions by reference.

## What solution would you like?

It is unreasonable to have seperate types for each tuple length (e.g. `DynamicTuple`) however we could replace `DynamicTuple::insert(&mut self, ...)` with `DynamicTuple::with_field(&mut self, ...) -> Self` to reduce confusion.

**NB:** it would still *technically* be possible to change the structure of a dynamic type by reference, e.g.
```rust
fn add_field(tuple: &mut DynamicTuple, field: impl Reflect) {
let empty_tuple = DynamicTuple::default();
let old_tuple = core::mem::replace(tuple, empty_tuple); // or `mem::swap` or `mem::take`.
// Proposed new API:
let field_added = old_tuple.with_field(field);
*tuple = field_added;
}
```

but this is awkward.

## What alternative(s) have you considered?

Not doing this. This should not be implemented if there are significant performance concerns.

## Additional context

None, AFIAK.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.