bevy/crates
Serv 3b1b60e7dc
add MutUntyped::map_unchanged (#9194)
### **Adopted #6430**

# Objective

`MutUntyped` is the untyped variant of `Mut<T>` that stores a `PtrMut`
instead of a `&mut T`. Working with a `MutUntyped` is a bit annoying,
because as soon you want to use the ptr e.g. as a `&mut dyn Reflect` you
cannot use a type like `Mut<dyn Reflect>` but instead need to carry
around a `&mut dyn Reflect` and a `impl FnMut()` to mark the value as
changed.
## Solution

* Provide a method `map_unchanged` to turn a `MutUntyped` into a
`Mut<T>` by mapping the `PtrMut<'a>` to a `&'a mut T`
      This can be used like this:


```rust
// SAFETY: ptr is of type `u8`
let val: Mut<u8> = mut_untyped.map_unchanged(|ptr| unsafe { ptr.deref_mut::<u8>() });

// SAFETY: from the context it is known that `ReflectFromPtr` was made for the type of the `MutUntyped`
let val: Mut<dyn Reflect> = mut_untyped.map_unchanged(|ptr| unsafe { reflect_from_ptr.as_reflect_ptr_mut(ptr) });
```

Note that nothing prevents you from doing

```rust
mut_untyped.map_unchanged(|ptr| &mut ());
```

or using any other mutable reference you can get, but IMO that is fine
since that will only result in a `Mut` that will dereference to that
value and mark the original value as changed. The lifetimes here prevent
anything bad from happening.
## Alternatives

1. Make `Ticks` public and provide a method to get construct a `Mut`
from `Ticks` and `&mut T`. More powerful and more easy to misuse.
2. Do nothing. People can still do everything they want, but they need
to pass (`&mut dyn Reflect, impl FnMut() + '_)` around instead of
`Mut<dyn Reflect>`

## Changelog

- add `MutUntyped::map_unchanged` to turn a `MutUntyped` into its typed
counterpart

---------

Co-authored-by: Jakob Hellermann <jakob.hellermann@protonmail.com>
Co-authored-by: JoJoJet <21144246+JoJoJet@users.noreply.github.com>
2023-07-23 01:17:31 +00:00
..
bevy_a11y Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_animation Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_app Add track_caller to App::add_plugins (#9174) 2023-07-23 01:02:20 +00:00
bevy_asset impl From<&AssetPath> for HandleId (#9132) 2023-07-15 21:32:17 +00:00
bevy_audio Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_core fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_core_pipeline Return URect instead of (UVec2, UVec2) in Camera::physical_viewport_rect (#9085) 2023-07-15 21:25:22 +00:00
bevy_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_diagnostic Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dylib Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dynamic_plugin Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_ecs add MutUntyped::map_unchanged (#9194) 2023-07-23 01:17:31 +00:00
bevy_ecs_compile_fail_tests Resolve clippy issues for rust 1.70.0 (#8738) 2023-06-01 21:05:05 +00:00
bevy_encase_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gilrs Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gizmos gizmo plugin lag bugfix (#9166) 2023-07-23 01:01:45 +00:00
bevy_gltf Add GltfLoader::new. (#9120) 2023-07-13 23:54:59 +00:00
bevy_hierarchy Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_input Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_internal fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_log Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_macro_utils Add some more helpful errors to BevyManifest when it doesn't find Cargo.toml (#9207) 2023-07-19 12:05:04 +00:00
bevy_macros_compile_fail_tests bevy_derive: Add #[deref] attribute (#8552) 2023-05-16 18:29:09 +00:00
bevy_math Add reflect impls to IRect and URect (#9191) 2023-07-23 01:02:00 +00:00
bevy_mikktspace Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_pbr Register AlphaMode type (#9222) 2023-07-20 21:26:03 +00:00
bevy_ptr Put #[repr(transparent)] attr to bevy_ptr types (#9068) 2023-07-14 18:55:15 +00:00
bevy_reflect Add reflect impls to IRect and URect (#9191) 2023-07-23 01:02:00 +00:00
bevy_reflect_compile_fail_tests fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_render Stop using unwrap in the pipelined rendering thread (#9052) 2023-07-23 01:06:25 +00:00
bevy_scene Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_sprite Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_tasks fix clippy::default_constructed_unit_structs and trybuild errors (#9144) 2023-07-13 22:23:04 +00:00
bevy_text Fix for vertical text bounds and alignment (#9133) 2023-07-13 23:35:32 +00:00
bevy_time Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_transform Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_ui Fix UI corruption for AMD gpus with Vulkan (#9169) 2023-07-19 07:29:14 +00:00
bevy_utils Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_window Add option to toggle window control buttons (#9083) 2023-07-23 01:02:40 +00:00
bevy_winit Add option to toggle window control buttons (#9083) 2023-07-23 01:02:40 +00:00