Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor non-core Curve methods into extension traits #16930

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

mweatherley
Copy link
Contributor

@mweatherley mweatherley commented Dec 22, 2024

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.

@mweatherley mweatherley added C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Math Fundamental domain-agnostic mathematical operations S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Dec 22, 2024
crates/bevy_math/src/curve/adaptors.rs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants