bevy/crates/bevy_math/src
Matty Weatherley 97909df6c0
Refactor non-core Curve methods into extension traits (#16930)
# Objective

The way `Curve` presently achieves dyn-compatibility involves shoving
`Self: Sized` bounds on a bunch of methods to forbid them from appearing
in vtables. (This is called *explicit non-dispatchability*.) The `Curve`
trait probably also just has way too many methods on its own.

In the past, using extension traits instead to achieve similar
functionality has been discussed. The upshot is that this would allow
the "core" of the curve trait, on which all the automatic methods rely,
to live in a very simple dyn-compatible trait, while other functionality
is implemented by extensions. For instance, `dyn Curve<T>` cannot use
the `Sized` methods, but `Box<dyn Curve<T>>` is `Sized`, hence would
automatically implement the extension trait, containing the methods
which are currently non-dispatchable.

Other motivations for this include modularity and code organization: the
`Curve` trait itself has grown quite large with the addition of numerous
adaptors, and refactoring it to demonstrate the separation of
functionality that is already present makes a lot of sense. Furthermore,
resampling behavior in particular is dependent on special traits that
may be mimicked or analogized in user-space, and creating extension
traits to achieve similar behavior in user-space is something we ought
to encourage by example.

## Solution

`Curve` now contains only `domain` and the `sample` methods. 

`CurveExt` has been created, and it contains all adaptors, along with
the other sampling convenience methods (`samples`, `sample_iter`, etc.).
It is implemented for all `C` where `C: Curve<T> + Sized`.

`CurveResampleExt` has been created, and it contains all resampling
methods. It is implemented for all `C` where `C: Curve<T> + ?Sized`.

## Testing

It compiles and `cargo doc` succeeds.

---

## Future work

- Consider writing extension traits for resampling curves in related
domains (e.g. resampling for `Curve<T>` where `T: Animatable` into an
`AnimatableKeyframeCurve`).
- `CurveExt` might be further broken down to separate the adaptor and
sampling methods.

---

## Migration Guide

`Curve` has been refactored so that much of its functionality is now in
extension traits. Adaptors such as `map`, `reparametrize`, `reverse`,
and so on now require importing `CurveExt`, while the resampling methods
`resample_*` require importing `CurveResampleExt`. Both of these new
traits are exported through `bevy::math::curve` and through
`bevy::math::prelude`.
2024-12-29 19:26:49 +00:00
..
bounding f32 -> Rot2 in bounding volume docs (#16848) 2024-12-17 00:12:49 +00:00
cubic_splines Derivative access patterns for curves (#16503) 2024-12-10 20:27:37 +00:00
curve Refactor non-core Curve methods into extension traits (#16930) 2024-12-29 19:26:49 +00:00
primitives Feature gate is_polygon_simple behind the alloc feature. (#16739) 2024-12-10 07:45:02 +00:00
rects Add no_std Support to bevy_math (#15810) 2024-12-03 17:14:51 +00:00
sampling Prefer Display over Debug (#16112) 2024-12-27 00:40:06 +00:00
affine3.rs Implemented Reflect for (almost) all bevy_math types (#13537) 2024-05-27 18:18:10 +00:00
aspect_ratio.rs Deny derive_more error feature and replace it with thiserror (#16684) 2024-12-06 17:03:55 +00:00
common_traits.rs Use variadics_please to implement StableInterpolate on tuples. (#16931) 2024-12-24 02:53:43 +00:00
compass.rs Add no_std Support to bevy_math (#15810) 2024-12-03 17:14:51 +00:00
direction.rs ✏️ Fix typos across bevy (#16702) 2024-12-08 01:18:39 +00:00
float_ord.rs Add no_std Support to bevy_math (#15810) 2024-12-03 17:14:51 +00:00
isometry.rs Implement From translation and rotation for isometries (#15733) 2024-10-08 16:09:28 +00:00
lib.rs Rust 1.83, allow -> expect (missing_docs) (#16561) 2024-12-16 23:27:57 +00:00
ops.rs Fix atan2 docs (#16673) 2024-12-10 03:19:05 +00:00
ray.rs Add no_std Support to bevy_math (#15810) 2024-12-03 17:14:51 +00:00
rotation2d.rs Add no_std Support to bevy_math (#15810) 2024-12-03 17:14:51 +00:00