From 9366b950067a7c18266eb5f2bfb900a9f825ff9c Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Thu, 10 Oct 2024 01:23:23 +1100 Subject: [PATCH] Remove `thiserror` from `bevy_math` (#15769) # Objective - Contributes to #15460 ## Solution - Removed `thiserror` from `bevy_math` --- crates/bevy_math/Cargo.toml | 7 +++- crates/bevy_math/src/aspect_ratio.rs | 19 ++++------- crates/bevy_math/src/cubic_splines.rs | 22 ++++++------ crates/bevy_math/src/curve/cores.rs | 28 +++++++-------- crates/bevy_math/src/curve/interval.rs | 18 +++++----- crates/bevy_math/src/curve/mod.rs | 45 +++++++++++++------------ crates/bevy_math/src/direction.rs | 9 ++--- crates/bevy_math/src/primitives/dim2.rs | 22 +++--------- 8 files changed, 76 insertions(+), 94 deletions(-) diff --git a/crates/bevy_math/Cargo.toml b/crates/bevy_math/Cargo.toml index 8662b029b2..bfd6535a86 100644 --- a/crates/bevy_math/Cargo.toml +++ b/crates/bevy_math/Cargo.toml @@ -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 } diff --git a/crates/bevy_math/src/aspect_ratio.rs b/crates/bevy_math/src/aspect_ratio.rs index 5b0fc47946..a318ff0296 100644 --- a/crates/bevy_math/src/aspect_ratio.rs +++ b/crates/bevy_math/src/aspect_ratio.rs @@ -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); @@ -83,23 +83,16 @@ impl TryFrom for AspectRatio { } } -impl From 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, } diff --git a/crates/bevy_math/src/cubic_splines.rs b/crates/bevy_math/src/cubic_splines.rs index bccc8dfb06..66d7df3d32 100644 --- a/crates/bevy_math/src/cubic_splines.rs +++ b/crates/bevy_math/src/cubic_splines.rs @@ -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}; @@ -94,8 +94,8 @@ impl CubicGenerator

for CubicBezier

{ /// 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 @@ -500,10 +500,10 @@ impl CyclicCubicGenerator

for CubicBSpline

{ } /// 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, @@ -512,13 +512,13 @@ 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, @@ -526,7 +526,7 @@ pub enum CubicNurbsError { 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, @@ -870,8 +870,8 @@ impl CyclicCubicGenerator

for LinearSpline

{ } /// 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, diff --git a/crates/bevy_math/src/curve/cores.rs b/crates/bevy_math/src/curve/cores.rs index 6e637c4ba7..6c63eabb29 100644 --- a/crates/bevy_math/src/curve/cores.rs +++ b/crates/bevy_math/src/curve/cores.rs @@ -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; @@ -131,18 +131,18 @@ pub struct EvenCore { } /// 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, } @@ -333,11 +333,11 @@ pub struct UnevenCore { } /// 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 { @@ -472,15 +472,15 @@ pub struct ChunkedUnevenCore { } /// 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 { @@ -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, @@ -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, diff --git a/crates/bevy_math/src/curve/interval.rs b/crates/bevy_math/src/curve/interval.rs index 244fd1b4c4..291d0499c0 100644 --- a/crates/bevy_math/src/curve/interval.rs +++ b/crates/bevy_math/src/curve/interval.rs @@ -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; @@ -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, } diff --git a/crates/bevy_math/src/curve/mod.rs b/crates/bevy_math/src/curve/mod.rs index ceb7bf2937..e5db3b9c74 100644 --- a/crates/bevy_math/src/curve/mod.rs +++ b/crates/bevy_math/src/curve/mod.rs @@ -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. /// @@ -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, } diff --git a/crates/bevy_math/src/direction.rs b/crates/bevy_math/src/direction.rs index fe796c6460..acbc5816ad 100644 --- a/crates/bevy_math/src/direction.rs +++ b/crates/bevy_math/src/direction.rs @@ -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; @@ -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( @@ -534,12 +535,6 @@ impl TryFrom for Dir3 { } } -impl From for Vec3 { - fn from(value: Dir3) -> Self { - value.0 - } -} - impl core::ops::Deref for Dir3 { type Target = Vec3; fn deref(&self) -> &Self::Target { diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 1bf4157151..53397fe07a 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -1,5 +1,5 @@ use core::f32::consts::{FRAC_1_SQRT_2, FRAC_PI_2, FRAC_PI_3, PI}; -use thiserror::Error; +use derive_more::derive::{Display, Error, From}; use super::{Measured2d, Primitive2d, WindingOrder}; use crate::{ @@ -267,7 +267,7 @@ impl Arc2d { /// /// **Warning:** Circular sectors with negative angle or radius, or with angle greater than an entire circle, are not officially supported. /// We recommend normalizing circular sectors to have an angle in [0, 2π]. -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, From)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( feature = "bevy_reflect", @@ -292,12 +292,6 @@ impl Default for CircularSector { } } -impl From for CircularSector { - fn from(arc: Arc2d) -> Self { - Self { arc } - } -} - impl CircularSector { /// Create a new [`CircularSector`] from a `radius` and an `angle` #[inline(always)] @@ -406,7 +400,7 @@ impl CircularSector { /// /// **Warning:** Circular segments with negative angle or radius, or with angle greater than an entire circle, are not officially supported. /// We recommend normalizing circular segments to have an angle in [0, 2π]. -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, From)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr( feature = "bevy_reflect", @@ -431,12 +425,6 @@ impl Default for CircularSegment { } } -impl From for CircularSegment { - fn from(arc: Arc2d) -> Self { - Self { arc } - } -} - impl CircularSegment { /// Create a new [`CircularSegment`] from a `radius`, and an `angle` #[inline(always)] @@ -1620,10 +1608,10 @@ pub struct ConvexPolygon { impl Primitive2d for ConvexPolygon {} /// An error that happens when creating a [`ConvexPolygon`]. -#[derive(Error, Debug, Clone)] +#[derive(Error, Display, Debug, Clone)] pub enum ConvexPolygonError { /// The created polygon is not convex. - #[error("The created polygon is not convex")] + #[display("The created polygon is not convex")] Concave, }