bevy/crates
Gino Valente c2854a2a05
bevy_reflect: Deprecate PartialReflect::clone_value (#18284)
# Objective

#13432 added proper reflection-based cloning. This is a better method
than cloning via `clone_value` for reasons detailed in the description
of that PR. However, it may not be immediately apparent to users why one
should be used over the other, and what the gotchas of `clone_value`
are.

## Solution

This PR marks `PartialReflect::clone_value` as deprecated, with the
deprecation notice pointing users to `PartialReflect::reflect_clone`.
However, it also suggests using a new method introduced in this PR:
`PartialReflect::to_dynamic`.

`PartialReflect::to_dynamic` is essentially a renaming of
`PartialReflect::clone_value`. By naming it `to_dynamic`, we make it
very obvious that what's returned is a dynamic type. The one caveat to
this is that opaque types still use `reflect_clone` as they have no
corresponding dynamic type.

Along with changing the name, the method is now optional, and comes with
a default implementation that calls out to the respective reflection
subtrait method. This was done because there was really no reason to
require manual implementors provide a method that almost always calls
out to a known set of methods.

Lastly, to make this default implementation work, this PR also did a
similar thing with the `clone_dynamic ` methods on the reflection
subtraits. For example, `Struct::clone_dynamic` has been marked
deprecated and is superseded by `Struct::to_dynamic_struct`. This was
necessary to avoid the "multiple names in scope" issue.

### Open Questions

This PR maintains the original signature of `clone_value` on
`to_dynamic`. That is, it takes `&self` and returns `Box<dyn
PartialReflect>`.

However, in order for this to work, it introduces a panic if the value
is opaque and doesn't override the default `reflect_clone`
implementation.

One thing we could do to avoid the panic would be to make the conversion
fallible, either returning `Option<Box<dyn PartialReflect>>` or
`Result<Box<dyn PartialReflect>, ReflectCloneError>`.

This makes using the method a little more involved (i.e. users have to
either unwrap or handle the rare possibility of an error), but it would
set us up for a world where opaque types don't strictly need to be
`Clone`. Right now this bound is sort of implied by the fact that
`clone_value` is a required trait method, and the default behavior of
the macro is to use `Clone` for opaque types.

Alternatively, we could keep the signature but make the method required.
This maintains that implied bound where manual implementors must provide
some way of cloning the value (or YOLO it and just panic), but also
makes the API simpler to use.

Finally, we could just leave it with the panic. It's unlikely this would
occur in practice since our macro still requires `Clone` for opaque
types, and thus this would only ever be an issue if someone were to
manually implement `PartialReflect` without a valid `to_dynamic` or
`reflect_clone` method.

## Testing

You can test locally using the following command:

```
cargo test --package bevy_reflect --all-features
```

---

## Migration Guide

`PartialReflect::clone_value` is being deprecated. Instead, use
`PartialReflect::to_dynamic` if wanting to create a new dynamic instance
of the reflected value. Alternatively, use
`PartialReflect::reflect_clone` to attempt to create a true clone of the
underlying value.

Similarly, the following methods have been deprecated and should be
replaced with these alternatives:
- `Array::clone_dynamic` → `Array::to_dynamic_array`
- `Enum::clone_dynamic` → `Enum::to_dynamic_enum`
- `List::clone_dynamic` → `List::to_dynamic_list`
- `Map::clone_dynamic` → `Map::to_dynamic_map`
- `Set::clone_dynamic` → `Set::to_dynamic_set`
- `Struct::clone_dynamic` → `Struct::to_dynamic_struct`
- `Tuple::clone_dynamic` → `Tuple::to_dynamic_tuple`
- `TupleStruct::clone_dynamic` → `TupleStruct::to_dynamic_tuple_struct`
2025-03-14 19:33:57 +00:00
..
bevy_a11y Automatically enable portable-atomic when required (#17570) 2025-02-24 20:52:46 +00:00
bevy_animation Update petgraph requirement from 0.6 to 0.7 (#18224) 2025-03-10 07:12:27 +00:00
bevy_app Small cleanup for ECS error handling (#18280) 2025-03-13 00:13:02 +00:00
bevy_asset bevy_reflect: Deprecate PartialReflect::clone_value (#18284) 2025-03-14 19:33:57 +00:00
bevy_audio Support for non-browser wasm (#17499) 2025-03-07 21:22:28 +00:00
bevy_color Add Saturation trait to bevy_color (#18202) 2025-03-10 21:52:04 +00:00
bevy_core_pipeline Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_derive allow Call and Closure expressions in hook macro attributes (#18017) 2025-03-06 16:39:11 +00:00
bevy_dev_tools Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_diagnostic Add no_std support to bevy (#17955) 2025-03-07 03:39:46 +00:00
bevy_dylib Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
bevy_ecs bevy_reflect: Deprecate PartialReflect::clone_value (#18284) 2025-03-14 19:33:57 +00:00
bevy_encase_derive Internalize BevyManifest logic. Switch to RwLock (#18263) 2025-03-12 00:46:01 +00:00
bevy_gilrs Replace some !Send resources with thread_local! (#17730) 2025-03-04 07:48:02 +00:00
bevy_gizmos Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_gltf Add no_std support to bevy (#17955) 2025-03-07 03:39:46 +00:00
bevy_image Add no_std support to bevy (#17955) 2025-03-07 03:39:46 +00:00
bevy_input Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_input_focus Make Query::single (and friends) return a Result (#18082) 2025-03-02 19:51:56 +00:00
bevy_internal don't use bevy_pbr for base bevy_gizmos plugin (#17581) 2025-03-10 21:16:52 +00:00
bevy_log Add print_stdout and print_stderr lints (#17446) (#18233) 2025-03-11 19:35:48 +00:00
bevy_macro_utils Switch to ImDocument in BevyManifest (#18272) 2025-03-12 20:15:39 +00:00
bevy_math implement Bounded2d for ConvexPolygon (#18286) 2025-03-13 16:35:52 +00:00
bevy_mesh Fix mesh tangent attribute matching in mesh transform operations (#17992) 2025-03-07 17:39:42 +00:00
bevy_mikktspace Add print_stdout and print_stderr lints (#17446) (#18233) 2025-03-11 19:35:48 +00:00
bevy_pbr Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_picking Sync up the Derive of DragEntry to match the other events (#18220) 2025-03-11 19:58:20 +00:00
bevy_platform_support Support for non-browser wasm (#17499) 2025-03-07 21:22:28 +00:00
bevy_ptr moved Debug from derive to impl_ptr in bevy_ptr (#18042) 2025-02-28 02:54:46 +00:00
bevy_reflect bevy_reflect: Deprecate PartialReflect::clone_value (#18284) 2025-03-14 19:33:57 +00:00
bevy_remote RPC Discover endpoint with basic informations (#18068) 2025-03-12 23:32:06 +00:00
bevy_render Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_scene bevy_reflect: Deprecate PartialReflect::clone_value (#18284) 2025-03-14 19:33:57 +00:00
bevy_sprite Add no_std support to bevy (#17955) 2025-03-07 03:39:46 +00:00
bevy_state Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_tasks Add print_stdout and print_stderr lints (#17446) (#18233) 2025-03-11 19:35:48 +00:00
bevy_text Upgrade to cosmic-text 0.13 (#18239) 2025-03-12 18:03:45 +00:00
bevy_time Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_transform Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_ui Remove the entity index from the UI phase's sort key (#18273) 2025-03-12 17:11:02 +00:00
bevy_utils Automatically enable portable-atomic when required (#17570) 2025-02-24 20:52:46 +00:00
bevy_window Rework WindowMode::Fullscreen API (#17525) 2025-03-11 01:20:53 +00:00
bevy_winit Rework WindowMode::Fullscreen API (#17525) 2025-03-11 01:20:53 +00:00