Remove thiserror from bevy_math (#15769)

# Objective

- Contributes to #15460

## Solution

- Removed `thiserror` from `bevy_math`
This commit is contained in:
Zachary Harrold 2024-10-10 01:23:23 +11:00 committed by GitHub
parent 5e89acacb4
commit 9366b95006
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 76 additions and 94 deletions

View file

@ -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 }

View file

@ -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<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,
}

View file

@ -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<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
@ -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,
@ -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<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,

View file

@ -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<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,
}
@ -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 {
@ -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 {
@ -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,

View file

@ -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,
}

View file

@ -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,
}

View file

@ -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<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 {

View file

@ -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<Arc2d> 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<Arc2d> 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<const N: usize> {
impl<const N: usize> Primitive2d for ConvexPolygon<N> {}
/// 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,
}