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

Remove thiserror from bevy_math #15769

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ rust-version = "1.68.2"

[dependencies]
glam = { version = "0.29", features = ["bytemuck"] }
thiserror = "1.0"
derive_more = { version = "1", default-features = false, features = [
"error",
"from",
"display",
"into",
] }
itertools = "0.13.0"
serde = { version = "1", features = ["derive"], optional = true }
libm = { version = "0.2", optional = true }
Expand Down
19 changes: 6 additions & 13 deletions crates/bevy_math/src/aspect_ratio.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Provides a simple aspect ratio struct to help with calculations.

use crate::Vec2;
use thiserror::Error;
use derive_more::derive::{Display, Error, Into};

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;

/// An `AspectRatio` is the ratio of width to height.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Into)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
pub struct AspectRatio(f32);

Expand Down Expand Up @@ -83,23 +83,16 @@ impl TryFrom<Vec2> for AspectRatio {
}
}

impl From<AspectRatio> for f32 {
#[inline]
fn from(aspect_ratio: AspectRatio) -> Self {
aspect_ratio.0
}
}

/// An Error type for when [`super::AspectRatio`] is provided invalid width or height values
#[derive(Error, Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Error, Display, Debug, PartialEq, Eq, Clone, Copy)]
pub enum AspectRatioError {
/// Error due to width or height having zero as a value.
#[error("AspectRatio error: width or height is zero")]
#[display("AspectRatio error: width or height is zero")]
Zero,
/// Error due towidth or height being infinite.
#[error("AspectRatio error: width or height is infinite")]
#[display("AspectRatio error: width or height is infinite")]
Infinite,
/// Error due to width or height being Not a Number (NaN).
#[error("AspectRatio error: width or height is NaN")]
#[display("AspectRatio error: width or height is NaN")]
NaN,
}
22 changes: 11 additions & 11 deletions crates/bevy_math/src/cubic_splines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
Vec2, VectorSpace,
};

use derive_more::derive::{Display, Error};
use itertools::Itertools;
use thiserror::Error;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
Expand Down Expand Up @@ -94,8 +94,8 @@ impl<P: VectorSpace> CubicGenerator<P> for CubicBezier<P> {

/// An error returned during cubic curve generation for cubic Bezier curves indicating that a
/// segment of control points was not present.
#[derive(Clone, Debug, Error)]
#[error("Unable to generate cubic curve: at least one set of control points is required")]
#[derive(Clone, Debug, Error, Display)]
#[display("Unable to generate cubic curve: at least one set of control points is required")]
pub struct CubicBezierError;

/// A spline interpolated continuously between the nearest two control points, with the position and
Expand Down Expand Up @@ -500,10 +500,10 @@ impl<P: VectorSpace> CyclicCubicGenerator<P> for CubicBSpline<P> {
}

/// Error during construction of [`CubicNurbs`]
#[derive(Clone, Debug, Error)]
#[derive(Clone, Debug, Error, Display)]
pub enum CubicNurbsError {
/// Provided the wrong number of knots.
#[error("Wrong number of knots: expected {expected}, provided {provided}")]
#[display("Wrong number of knots: expected {expected}, provided {provided}")]
KnotsNumberMismatch {
/// Expected number of knots
expected: usize,
Expand All @@ -512,21 +512,21 @@ pub enum CubicNurbsError {
},
/// The provided knots had a descending knot pair. Subsequent knots must
/// either increase or stay the same.
#[error("Invalid knots: contains descending knot pair")]
#[display("Invalid knots: contains descending knot pair")]
DescendingKnots,
/// The provided knots were all equal. Knots must contain at least one increasing pair.
#[error("Invalid knots: all knots are equal")]
#[display("Invalid knots: all knots are equal")]
ConstantKnots,
/// Provided a different number of weights and control points.
#[error("Incorrect number of weights: expected {expected}, provided {provided}")]
#[display("Incorrect number of weights: expected {expected}, provided {provided}")]
WeightsNumberMismatch {
/// Expected number of weights
expected: usize,
/// Provided number of weights
provided: usize,
},
/// The number of control points provided is less than 4.
#[error("Not enough control points, at least 4 are required, {provided} were provided")]
#[display("Not enough control points, at least 4 are required, {provided} were provided")]
NotEnoughControlPoints {
/// The number of control points provided
provided: usize,
Expand Down Expand Up @@ -870,8 +870,8 @@ impl<P: VectorSpace> CyclicCubicGenerator<P> for LinearSpline<P> {
}

/// An error indicating that a spline construction didn't have enough control points to generate a curve.
#[derive(Clone, Debug, Error)]
#[error("Not enough data to build curve: needed at least {expected} control points but was only given {given}")]
#[derive(Clone, Debug, Error, Display)]
#[display("Not enough data to build curve: needed at least {expected} control points but was only given {given}")]
pub struct InsufficientDataError {
expected: usize,
given: usize,
Expand Down
28 changes: 14 additions & 14 deletions crates/bevy_math/src/curve/cores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

use super::interval::Interval;
use core::fmt::Debug;
use derive_more::derive::{Display, Error};
use itertools::Itertools;
use thiserror::Error;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -131,18 +131,18 @@ pub struct EvenCore<T> {
}

/// An error indicating that an [`EvenCore`] could not be constructed.
#[derive(Debug, Error)]
#[error("Could not construct an EvenCore")]
#[derive(Debug, Error, Display)]
#[display("Could not construct an EvenCore")]
pub enum EvenCoreError {
/// Not enough samples were provided.
#[error("Need at least two samples to create an EvenCore, but {samples} were provided")]
#[display("Need at least two samples to create an EvenCore, but {samples} were provided")]
NotEnoughSamples {
/// The number of samples that were provided.
samples: usize,
},

/// Unbounded domains are not compatible with `EvenCore`.
#[error("Cannot create a EvenCore over an unbounded domain")]
#[display("Cannot create a EvenCore over an unbounded domain")]
UnboundedDomain,
}

Expand Down Expand Up @@ -333,11 +333,11 @@ pub struct UnevenCore<T> {
}

/// An error indicating that an [`UnevenCore`] could not be constructed.
#[derive(Debug, Error)]
#[error("Could not construct an UnevenCore")]
#[derive(Debug, Error, Display)]
#[display("Could not construct an UnevenCore")]
pub enum UnevenCoreError {
/// Not enough samples were provided.
#[error(
#[display(
"Need at least two unique samples to create an UnevenCore, but {samples} were provided"
)]
NotEnoughSamples {
Expand Down Expand Up @@ -472,15 +472,15 @@ pub struct ChunkedUnevenCore<T> {
}

/// An error that indicates that a [`ChunkedUnevenCore`] could not be formed.
#[derive(Debug, Error)]
#[error("Could not create a ChunkedUnevenCore")]
#[derive(Debug, Error, Display)]
#[display("Could not create a ChunkedUnevenCore")]
pub enum ChunkedUnevenCoreError {
/// The width of a `ChunkedUnevenCore` cannot be zero.
#[error("Chunk width must be at least 1")]
#[display("Chunk width must be at least 1")]
ZeroWidth,

/// At least two sample times are necessary to interpolate in `ChunkedUnevenCore`.
#[error(
#[display(
"Need at least two unique samples to create a ChunkedUnevenCore, but {samples} were provided"
)]
NotEnoughSamples {
Expand All @@ -489,7 +489,7 @@ pub enum ChunkedUnevenCoreError {
},

/// The length of the value buffer is supposed to be the `width` times the number of samples.
#[error("Expected {expected} total values based on width, but {actual} were provided")]
#[display("Expected {expected} total values based on width, but {actual} were provided")]
MismatchedLengths {
/// The expected length of the value buffer.
expected: usize,
Expand All @@ -498,7 +498,7 @@ pub enum ChunkedUnevenCoreError {
},

/// Tried to infer the width, but the ratio of lengths wasn't an integer, so no such length exists.
#[error("The length of the list of values ({values_len}) was not divisible by that of the list of times ({times_len})")]
#[display("The length of the list of values ({values_len}) was not divisible by that of the list of times ({times_len})")]
NonDivisibleLengths {
/// The length of the value buffer.
values_len: usize,
Expand Down
18 changes: 9 additions & 9 deletions crates/bevy_math/src/curve/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::{
cmp::{max_by, min_by},
ops::RangeInclusive,
};
use derive_more::derive::{Display, Error};
use itertools::Either;
use thiserror::Error;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
Expand All @@ -29,26 +29,26 @@ pub struct Interval {
}

/// An error that indicates that an operation would have returned an invalid [`Interval`].
#[derive(Debug, Error)]
#[error("The resulting interval would be invalid (empty or with a NaN endpoint)")]
#[derive(Debug, Error, Display)]
#[display("The resulting interval would be invalid (empty or with a NaN endpoint)")]
pub struct InvalidIntervalError;

/// An error indicating that spaced points could not be extracted from an unbounded interval.
#[derive(Debug, Error)]
#[error("Cannot extract spaced points from an unbounded interval")]
#[derive(Debug, Error, Display)]
#[display("Cannot extract spaced points from an unbounded interval")]
pub struct SpacedPointsError;

/// An error indicating that a linear map between intervals could not be constructed because of
/// unboundedness.
#[derive(Debug, Error)]
#[error("Could not construct linear function to map between intervals")]
#[derive(Debug, Error, Display)]
#[display("Could not construct linear function to map between intervals")]
pub(super) enum LinearMapError {
/// The source interval being mapped out of was unbounded.
#[error("The source interval is unbounded")]
#[display("The source interval is unbounded")]
SourceUnbounded,

/// The target interval being mapped into was unbounded.
#[error("The target interval is unbounded")]
#[display("The target interval is unbounded")]
TargetUnbounded,
}

Expand Down
45 changes: 23 additions & 22 deletions crates/bevy_math/src/curve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use cores::{EvenCore, UnevenCore};

use crate::{StableInterpolate, VectorSpace};
use core::{marker::PhantomData, ops::Deref};
use derive_more::derive::{Display, Error};
use interval::InvalidIntervalError;
use itertools::Itertools;
use thiserror::Error;

/// A trait for a type that can represent values of type `T` parametrized over a fixed interval.
///
Expand Down Expand Up @@ -624,73 +624,74 @@ where

/// An error indicating that a linear reparametrization couldn't be performed because of
/// malformed inputs.
#[derive(Debug, Error)]
#[error("Could not build a linear function to reparametrize this curve")]
#[derive(Debug, Error, Display)]
#[display("Could not build a linear function to reparametrize this curve")]
pub enum LinearReparamError {
/// The source curve that was to be reparametrized had unbounded domain.
#[error("This curve has unbounded domain")]
#[display("This curve has unbounded domain")]
SourceCurveUnbounded,

/// The target interval for reparametrization was unbounded.
#[error("The target interval for reparametrization is unbounded")]
#[display("The target interval for reparametrization is unbounded")]
TargetIntervalUnbounded,
}

/// An error indicating that a reversion of a curve couldn't be performed because of
/// malformed inputs.
#[derive(Debug, Error)]
#[error("Could not reverse this curve")]
#[derive(Debug, Error, Display)]
#[display("Could not reverse this curve")]
pub enum ReverseError {
/// The source curve that was to be reversed had unbounded domain end.
#[error("This curve has an unbounded domain end")]
#[display("This curve has an unbounded domain end")]
SourceDomainEndInfinite,
}

/// An error indicating that a repetition of a curve couldn't be performed because of malformed
/// inputs.
#[derive(Debug, Error)]
#[error("Could not repeat this curve")]
#[derive(Debug, Error, Display)]
#[display("Could not repeat this curve")]
pub enum RepeatError {
/// The source curve that was to be repeated had unbounded domain.
#[error("This curve has an unbounded domain")]
#[display("This curve has an unbounded domain")]
SourceDomainUnbounded,
}

/// An error indicating that a ping ponging of a curve couldn't be performed because of
/// malformed inputs.
#[derive(Debug, Error)]
#[error("Could not ping pong this curve")]
#[derive(Debug, Error, Display)]
#[display("Could not ping pong this curve")]
pub enum PingPongError {
/// The source curve that was to be ping ponged had unbounded domain end.
#[error("This curve has an unbounded domain end")]
#[display("This curve has an unbounded domain end")]
SourceDomainEndInfinite,
}

/// An error indicating that an end-to-end composition couldn't be performed because of
/// malformed inputs.
#[derive(Debug, Error)]
#[error("Could not compose these curves together")]
#[derive(Debug, Error, Display)]
#[display("Could not compose these curves together")]
pub enum ChainError {
/// The right endpoint of the first curve was infinite.
#[error("The first curve's domain has an infinite end")]
#[display("The first curve's domain has an infinite end")]
FirstEndInfinite,

/// The left endpoint of the second curve was infinite.
#[error("The second curve's domain has an infinite start")]
#[display("The second curve's domain has an infinite start")]
SecondStartInfinite,
}

/// An error indicating that a resampling operation could not be performed because of
/// malformed inputs.
#[derive(Debug, Error)]
#[error("Could not resample from this curve because of bad inputs")]
#[derive(Debug, Error, Display)]
#[display("Could not resample from this curve because of bad inputs")]
pub enum ResamplingError {
/// This resampling operation was not provided with enough samples to have well-formed output.
#[error("Not enough unique samples to construct resampled curve")]
#[display("Not enough unique samples to construct resampled curve")]
#[error(ignore)]
NotEnoughSamples(usize),

/// This resampling operation failed because of an unbounded interval.
#[error("Could not resample because this curve has unbounded domain")]
#[display("Could not resample because this curve has unbounded domain")]
UnboundedDomain,
}

Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_math/src/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};

use core::f32::consts::FRAC_1_SQRT_2;
use derive_more::derive::Into;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -356,7 +357,7 @@ impl approx::UlpsEq for Dir2 {
}

/// A normalized vector pointing in a direction in 3D space
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Into)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(
Expand Down Expand Up @@ -534,12 +535,6 @@ impl TryFrom<Vec3> for Dir3 {
}
}

impl From<Dir3> for Vec3 {
fn from(value: Dir3) -> Self {
value.0
}
}

impl core::ops::Deref for Dir3 {
type Target = Vec3;
fn deref(&self) -> &Self::Target {
Expand Down
Loading
Loading