Skip to content

Commit

Permalink
Fixing Curve trait not being object safe. (#14939)
Browse files Browse the repository at this point in the history
# Objective

- `Curve<T>` was meant to be object safe, but one of the latest commits
made it not object safe.
- When trying to use `Curve<T>` as `&dyn Curve<T>` this compile error is
raised:
```
error[E0038]: the trait `curve::Curve` cannot be made into an object
    --> crates/bevy_math/src/curve/mod.rs:1025:20
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
    --> crates/bevy_math/src/curve/mod.rs:60:8
     |
23   | pub trait Curve<T> {
     |           ----- this trait cannot be made into an object...
...
60   |     fn sample_iter(&self, iter: impl IntoIterator<Item = f32>) -> impl Iterator<Item = Option<T>> {
     |        ^^^^^^^^^^^                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because method `sample_iter` references an `impl Trait` type in its return type
     |        |
     |        ...because method `sample_iter` has generic type parameters
...
```

## Solution

- Making `Curve<T>` object safe again by adding `Self: Sized` to newly
added methods.

## Testing

- Added new test that ensures the `Curve<T>` trait can be made into an
objet.
  • Loading branch information
eckz authored Aug 27, 2024
1 parent e320fa0 commit 1690b28
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions crates/bevy_math/src/curve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ pub trait Curve<T> {
/// The samples are returned in the same order as the parameter values `t_n` were provided and
/// will include all results. This leaves the responsibility for things like filtering and
/// sorting to the user for maximum flexibility.
fn sample_iter(&self, iter: impl IntoIterator<Item = f32>) -> impl Iterator<Item = Option<T>> {
fn sample_iter(&self, iter: impl IntoIterator<Item = f32>) -> impl Iterator<Item = Option<T>>
where
Self: Sized,
{
iter.into_iter().map(|t| self.sample(t))
}

Expand All @@ -72,10 +75,10 @@ pub trait Curve<T> {
/// The samples are returned in the same order as the parameter values `t_n` were provided and
/// will include all results. This leaves the responsibility for things like filtering and
/// sorting to the user for maximum flexibility.
fn sample_iter_unchecked(
&self,
iter: impl IntoIterator<Item = f32>,
) -> impl Iterator<Item = T> {
fn sample_iter_unchecked(&self, iter: impl IntoIterator<Item = f32>) -> impl Iterator<Item = T>
where
Self: Sized,
{
iter.into_iter().map(|t| self.sample_unchecked(t))
}

Expand All @@ -85,7 +88,10 @@ pub trait Curve<T> {
/// The samples are returned in the same order as the parameter values `t_n` were provided and
/// will include all results. This leaves the responsibility for things like filtering and
/// sorting to the user for maximum flexibility.
fn sample_iter_clamped(&self, iter: impl IntoIterator<Item = f32>) -> impl Iterator<Item = T> {
fn sample_iter_clamped(&self, iter: impl IntoIterator<Item = f32>) -> impl Iterator<Item = T>
where
Self: Sized,
{
iter.into_iter().map(|t| self.sample_clamped(t))
}

Expand Down Expand Up @@ -760,6 +766,9 @@ where
/// must be left-finite.
///
/// Curves of this type are produced by [`Curve::chain`].
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct ChainCurve<T, C, D> {
first: C,
second: D,
Expand Down Expand Up @@ -1013,6 +1022,15 @@ mod tests {
use approx::{assert_abs_diff_eq, AbsDiffEq};
use std::f32::consts::TAU;

#[test]
fn curve_can_be_made_into_an_object() {
let curve = constant_curve(Interval::UNIT, 42.0);
let curve: &dyn Curve<f64> = &curve;

assert_eq!(curve.sample(1.0), Some(42.0));
assert_eq!(curve.sample(2.0), None);
}

#[test]
fn constant_curves() {
let curve = constant_curve(Interval::EVERYWHERE, 5.0);
Expand Down

0 comments on commit 1690b28

Please sign in to comment.