Skip to content

Commit

Permalink
Enable lenses to avoid change detection (#124)
Browse files Browse the repository at this point in the history
Add the ability for all `Lens`es to avoid triggering change detection if
they don't make any actual change to their target (component or asset).

This change modifies the signature of `Lerp::lerp()` to take `&mut dyn
Targetable<T>` instead of `&mut T`. The `Targetable<T>` acts as a
`Mut<T>`, marking the target as changed only if actually dereferenced
mutably.

Note that we cannot use `Mut<T>` directly because we can't obtain a
`Mut<A: Asset>` without marking the asset as mutable; see
bevyengine/bevy#13104.

Fixes #91
  • Loading branch information
djeedai committed Apr 26, 2024
1 parent d484c5a commit 63ea415
Show file tree
Hide file tree
Showing 5 changed files with 722 additions and 89 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,28 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- `Lens::lerp()` now takes a `&mut dyn Targetable<T>` instead of `&mut T`.
This ensures the lens can skip change detection if needed.
`Targetable<T>` conceptually acts like a `Mut<T>`, but allows encapsulating assets too.
There should be no other change than the function signature to upgrade custom lenses,
because `dyn Targetable<T>` now implements `Defer` and `DeferMut`, so can be used in place of `&mut T`.
- `AssetTarget::new()` now takes a simple `Mut<Assets<T>>` instead of `ResMut<Assets<T>>`.

### Fixed

- Fixed change detection such that lenses which do not dereference their target
do not unconditionally mark that target (component or asset) as changed from the point of view of ECS. (#91)

### Added

- Added `Targetable::target(&self) -> &T`.
- `dyn Targetable<T>` now implements `Defer` and `DeferMut`, so can be used in place of `&T` and `&mut T`.
- Added `ComponentTarget::to_mut(&mut self) -> Mut<'_, T>` to "reborrow" the component target as a `Mut<T>`.

## [0.10.0] - 2024-02-27

### Changed
Expand Down

0 comments on commit 63ea415

Please sign in to comment.