Remove thiserror from bevy_reflect (#15766)

# Objective

- Contributes to #15460

## Solution

- Removed `thiserror` from `bevy_reflect`
This commit is contained in:
Zachary Harrold 2024-10-10 01:25:41 +11:00 committed by GitHub
parent 46ad0b7513
commit 3cc1527e9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 62 additions and 83 deletions

View file

@ -38,7 +38,11 @@ bevy_ptr = { path = "../bevy_ptr", version = "0.15.0-dev" }
erased-serde = "0.4" erased-serde = "0.4"
disqualified = "1.0" disqualified = "1.0"
downcast-rs = "1.2" downcast-rs = "1.2"
thiserror = "1.0" derive_more = { version = "1", default-features = false, features = [
"error",
"from",
"display",
] }
serde = "1" serde = "1"
smallvec = { version = "1.11", optional = true } smallvec = { version = "1.11", optional = true }
assert_type_match = "0.1.1" assert_type_match = "0.1.1"

View file

@ -7,9 +7,10 @@ use crate::{
}; };
use core::fmt::Formatter; use core::fmt::Formatter;
use derive_more::derive::From;
/// A dynamic representation of an enum variant. /// A dynamic representation of an enum variant.
#[derive(Debug, Default)] #[derive(Debug, Default, From)]
pub enum DynamicVariant { pub enum DynamicVariant {
#[default] #[default]
Unit, Unit,
@ -27,18 +28,6 @@ impl Clone for DynamicVariant {
} }
} }
impl From<DynamicTuple> for DynamicVariant {
fn from(dyn_tuple: DynamicTuple) -> Self {
Self::Tuple(dyn_tuple)
}
}
impl From<DynamicStruct> for DynamicVariant {
fn from(dyn_struct: DynamicStruct) -> Self {
Self::Struct(dyn_struct)
}
}
impl From<()> for DynamicVariant { impl From<()> for DynamicVariant {
fn from(_: ()) -> Self { fn from(_: ()) -> Self {
Self::Unit Self::Unit

View file

@ -6,7 +6,7 @@ use bevy_utils::HashMap;
use core::slice::Iter; use core::slice::Iter;
use alloc::sync::Arc; use alloc::sync::Arc;
use thiserror::Error; use derive_more::derive::{Display, Error};
/// Describes the form of an enum variant. /// Describes the form of an enum variant.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
@ -40,12 +40,12 @@ pub enum VariantType {
} }
/// A [`VariantInfo`]-specific error. /// A [`VariantInfo`]-specific error.
#[derive(Debug, Error)] #[derive(Debug, Error, Display)]
pub enum VariantInfoError { pub enum VariantInfoError {
/// Caused when a variant was expected to be of a certain [type], but was not. /// Caused when a variant was expected to be of a certain [type], but was not.
/// ///
/// [type]: VariantType /// [type]: VariantType
#[error("variant type mismatch: expected {expected:?}, received {received:?}")] #[display("variant type mismatch: expected {expected:?}, received {received:?}")]
TypeMismatch { TypeMismatch {
expected: VariantType, expected: VariantType,
received: VariantType, received: VariantType,

View file

@ -1,23 +1,25 @@
use alloc::borrow::Cow; use alloc::borrow::Cow;
use thiserror::Error; use derive_more::derive::{Display, Error};
use crate::func::args::Ownership; use crate::func::args::Ownership;
/// An error that occurs when converting an [argument]. /// An error that occurs when converting an [argument].
/// ///
/// [argument]: crate::func::args::Arg /// [argument]: crate::func::args::Arg
#[derive(Debug, Error, PartialEq)] #[derive(Debug, Error, Display, PartialEq)]
pub enum ArgError { pub enum ArgError {
/// The argument is not the expected type. /// The argument is not the expected type.
#[error("expected `{expected}` but received `{received}` (@ argument index {index})")] #[display("expected `{expected}` but received `{received}` (@ argument index {index})")]
UnexpectedType { UnexpectedType {
index: usize, index: usize,
expected: Cow<'static, str>, expected: Cow<'static, str>,
received: Cow<'static, str>, received: Cow<'static, str>,
}, },
/// The argument has the wrong ownership. /// The argument has the wrong ownership.
#[error("expected {expected} value but received {received} value (@ argument index {index})")] #[display(
"expected {expected} value but received {received} value (@ argument index {index})"
)]
InvalidOwnership { InvalidOwnership {
index: usize, index: usize,
expected: Ownership, expected: Ownership,
@ -26,6 +28,6 @@ pub enum ArgError {
/// Occurs when attempting to access an argument from an empty [`ArgList`]. /// Occurs when attempting to access an argument from an empty [`ArgList`].
/// ///
/// [`ArgList`]: crate::func::args::ArgList /// [`ArgList`]: crate::func::args::ArgList
#[error("expected an argument but received none")] #[display("expected an argument but received none")]
EmptyArgList, EmptyArgList,
} }

View file

@ -1,18 +1,17 @@
use crate::func::{args::ArgError, Return}; use crate::func::{args::ArgError, Return};
use alloc::borrow::Cow; use alloc::borrow::Cow;
use thiserror::Error; use derive_more::derive::{Display, Error, From};
/// An error that occurs when calling a [`DynamicFunction`] or [`DynamicFunctionMut`]. /// An error that occurs when calling a [`DynamicFunction`] or [`DynamicFunctionMut`].
/// ///
/// [`DynamicFunction`]: crate::func::DynamicFunction /// [`DynamicFunction`]: crate::func::DynamicFunction
/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut /// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut
#[derive(Debug, Error, PartialEq)] #[derive(Debug, Error, Display, PartialEq, From)]
pub enum FunctionError { pub enum FunctionError {
/// An error occurred while converting an argument. /// An error occurred while converting an argument.
#[error(transparent)] ArgError(ArgError),
ArgError(#[from] ArgError),
/// The number of arguments provided does not match the expected number. /// The number of arguments provided does not match the expected number.
#[error("expected {expected} arguments but received {received}")] #[display("expected {expected} arguments but received {received}")]
ArgCountMismatch { expected: usize, received: usize }, ArgCountMismatch { expected: usize, received: usize },
} }
@ -28,14 +27,15 @@ pub type FunctionResult<'a> = Result<Return<'a>, FunctionError>;
/// An error that occurs when registering a function into a [`FunctionRegistry`]. /// An error that occurs when registering a function into a [`FunctionRegistry`].
/// ///
/// [`FunctionRegistry`]: crate::func::FunctionRegistry /// [`FunctionRegistry`]: crate::func::FunctionRegistry
#[derive(Debug, Error, PartialEq)] #[derive(Debug, Error, Display, PartialEq)]
pub enum FunctionRegistrationError { pub enum FunctionRegistrationError {
/// A function with the given name has already been registered. /// A function with the given name has already been registered.
/// ///
/// Contains the duplicate function name. /// Contains the duplicate function name.
#[error("a function has already been registered with name {0:?}")] #[display("a function has already been registered with name {_0:?}")]
#[error(ignore)]
DuplicateName(Cow<'static, str>), DuplicateName(Cow<'static, str>),
/// The function is missing a name by which it can be registered. /// The function is missing a name by which it can be registered.
#[error("function name is missing")] #[display("function name is missing")]
MissingName, MissingName,
} }

View file

@ -3,6 +3,7 @@ use crate::{Reflect, Type, TypePath};
use alloc::borrow::Cow; use alloc::borrow::Cow;
use alloc::sync::Arc; use alloc::sync::Arc;
use core::ops::Deref; use core::ops::Deref;
use derive_more::derive::From;
/// The generic parameters of a type. /// The generic parameters of a type.
/// ///
@ -63,7 +64,7 @@ impl Deref for Generics {
} }
/// An enum representing a generic parameter. /// An enum representing a generic parameter.
#[derive(Clone, Debug)] #[derive(Clone, Debug, From)]
pub enum GenericInfo { pub enum GenericInfo {
/// A type parameter. /// A type parameter.
/// ///
@ -100,18 +101,6 @@ impl GenericInfo {
}); });
} }
impl From<TypeParamInfo> for GenericInfo {
fn from(info: TypeParamInfo) -> Self {
Self::Type(info)
}
}
impl From<ConstParamInfo> for GenericInfo {
fn from(info: ConstParamInfo) -> Self {
Self::Const(info)
}
}
/// Type information for a generic type parameter. /// Type information for a generic type parameter.
/// ///
/// An example of a type parameter would be `T` in `struct Foo<T>`. /// An example of a type parameter would be `T` in `struct Foo<T>`.

View file

@ -1,4 +1,4 @@
use thiserror::Error; use derive_more::derive::{Display, Error};
#[cfg(feature = "functions")] #[cfg(feature = "functions")]
use crate::func::Function; use crate::func::Function;
@ -130,8 +130,8 @@ macro_rules! impl_reflect_kind_conversions {
/// Caused when a type was expected to be of a certain [kind], but was not. /// Caused when a type was expected to be of a certain [kind], but was not.
/// ///
/// [kind]: ReflectKind /// [kind]: ReflectKind
#[derive(Debug, Error)] #[derive(Debug, Error, Display)]
#[error("kind mismatch: expected {expected:?}, received {received:?}")] #[display("kind mismatch: expected {expected:?}, received {received:?}")]
pub struct ReflectKindMismatchError { pub struct ReflectKindMismatchError {
pub expected: ReflectKind, pub expected: ReflectKind,
pub received: ReflectKind, pub received: ReflectKind,

View file

@ -10,24 +10,23 @@ use parse::PathParser;
use crate::{PartialReflect, Reflect}; use crate::{PartialReflect, Reflect};
use core::fmt; use core::fmt;
use thiserror::Error; use derive_more::derive::{Display, From};
type PathResult<'a, T> = Result<T, ReflectPathError<'a>>; type PathResult<'a, T> = Result<T, ReflectPathError<'a>>;
/// An error returned from a failed path string query. /// An error returned from a failed path string query.
#[derive(Debug, PartialEq, Eq, Error)] #[derive(Debug, PartialEq, Eq, Display, From)]
pub enum ReflectPathError<'a> { pub enum ReflectPathError<'a> {
/// An error caused by trying to access a path that's not able to be accessed, /// An error caused by trying to access a path that's not able to be accessed,
/// see [`AccessError`] for details. /// see [`AccessError`] for details.
#[error(transparent)]
InvalidAccess(AccessError<'a>), InvalidAccess(AccessError<'a>),
/// An error that occurs when a type cannot downcast to a given type. /// An error that occurs when a type cannot downcast to a given type.
#[error("Can't downcast result of access to the given type")] #[display("Can't downcast result of access to the given type")]
InvalidDowncast, InvalidDowncast,
/// An error caused by an invalid path string that couldn't be parsed. /// An error caused by an invalid path string that couldn't be parsed.
#[error("Encountered an error at offset {offset} while parsing `{path}`: {error}")] #[display("Encountered an error at offset {offset} while parsing `{path}`: {error}")]
ParseError { ParseError {
/// Position in `path`. /// Position in `path`.
offset: usize, offset: usize,
@ -37,11 +36,8 @@ pub enum ReflectPathError<'a> {
error: ParseError<'a>, error: ParseError<'a>,
}, },
} }
impl<'a> From<AccessError<'a>> for ReflectPathError<'a> {
fn from(value: AccessError<'a>) -> Self { impl<'a> core::error::Error for ReflectPathError<'a> {}
Self::InvalidAccess(value)
}
}
/// Something that can be interpreted as a reflection path in [`GetPath`]. /// Something that can be interpreted as a reflection path in [`GetPath`].
pub trait ReflectPath<'a>: Sized { pub trait ReflectPath<'a>: Sized {
@ -355,7 +351,7 @@ impl From<Access<'static>> for OffsetAccess {
/// ]; /// ];
/// let my_path = ParsedPath::from(path_elements); /// let my_path = ParsedPath::from(path_elements);
/// ``` /// ```
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, From)]
pub struct ParsedPath( pub struct ParsedPath(
/// This is a vector of pre-parsed [`OffsetAccess`]es. /// This is a vector of pre-parsed [`OffsetAccess`]es.
pub Vec<OffsetAccess>, pub Vec<OffsetAccess>,
@ -447,11 +443,6 @@ impl<'a> ReflectPath<'a> for &'a ParsedPath {
Ok(root) Ok(root)
} }
} }
impl From<Vec<OffsetAccess>> for ParsedPath {
fn from(value: Vec<OffsetAccess>) -> Self {
ParsedPath(value)
}
}
impl<const N: usize> From<[OffsetAccess; N]> for ParsedPath { impl<const N: usize> From<[OffsetAccess; N]> for ParsedPath {
fn from(value: [OffsetAccess; N]) -> Self { fn from(value: [OffsetAccess; N]) -> Self {
ParsedPath(value.to_vec()) ParsedPath(value.to_vec())

View file

@ -4,34 +4,38 @@ use core::{
str::from_utf8_unchecked, str::from_utf8_unchecked,
}; };
use thiserror::Error; use derive_more::derive::{Display, Error, From};
use super::{Access, ReflectPathError}; use super::{Access, ReflectPathError};
/// An error that occurs when parsing reflect path strings. /// An error that occurs when parsing reflect path strings.
#[derive(Debug, PartialEq, Eq, Error)] #[derive(Debug, PartialEq, Eq, Error, Display)]
#[error(transparent)] #[error(ignore)]
pub struct ParseError<'a>(Error<'a>); pub struct ParseError<'a>(Error<'a>);
/// A parse error for a path string. /// A parse error for a path string.
#[derive(Debug, PartialEq, Eq, Error)] #[derive(Debug, PartialEq, Eq, Error, Display, From)]
enum Error<'a> { enum Error<'a> {
#[error("expected an identifier, but reached end of path string")] #[display("expected an identifier, but reached end of path string")]
NoIdent, NoIdent,
#[error("expected an identifier, got '{0}' instead")] #[display("expected an identifier, got '{_0}' instead")]
#[error(ignore)]
#[from(ignore)]
ExpectedIdent(Token<'a>), ExpectedIdent(Token<'a>),
#[error("failed to parse index as integer")] #[display("failed to parse index as integer")]
InvalidIndex(#[from] ParseIntError), InvalidIndex(ParseIntError),
#[error("a '[' wasn't closed, reached end of path string before finding a ']'")] #[display("a '[' wasn't closed, reached end of path string before finding a ']'")]
Unclosed, Unclosed,
#[error("a '[' wasn't closed properly, got '{0}' instead")] #[display("a '[' wasn't closed properly, got '{_0}' instead")]
#[error(ignore)]
#[from(ignore)]
BadClose(Token<'a>), BadClose(Token<'a>),
#[error("a ']' was found before an opening '['")] #[display("a ']' was found before an opening '['")]
CloseBeforeOpen, CloseBeforeOpen,
} }

View file

@ -8,39 +8,39 @@ use core::{
fmt::Debug, fmt::Debug,
}; };
use thiserror::Error; use derive_more::derive::{Display, Error};
use crate::utility::NonGenericTypeInfoCell; use crate::utility::NonGenericTypeInfoCell;
/// A enumeration of all error outcomes that might happen when running [`try_apply`](PartialReflect::try_apply). /// A enumeration of all error outcomes that might happen when running [`try_apply`](PartialReflect::try_apply).
#[derive(Error, Debug)] #[derive(Error, Display, Debug)]
pub enum ApplyError { pub enum ApplyError {
#[error("attempted to apply `{from_kind}` to `{to_kind}`")] #[display("attempted to apply `{from_kind}` to `{to_kind}`")]
/// Attempted to apply the wrong [kind](ReflectKind) to a type, e.g. a struct to a enum. /// Attempted to apply the wrong [kind](ReflectKind) to a type, e.g. a struct to a enum.
MismatchedKinds { MismatchedKinds {
from_kind: ReflectKind, from_kind: ReflectKind,
to_kind: ReflectKind, to_kind: ReflectKind,
}, },
#[error("enum variant `{variant_name}` doesn't have a field named `{field_name}`")] #[display("enum variant `{variant_name}` doesn't have a field named `{field_name}`")]
/// Enum variant that we tried to apply to was missing a field. /// Enum variant that we tried to apply to was missing a field.
MissingEnumField { MissingEnumField {
variant_name: Box<str>, variant_name: Box<str>,
field_name: Box<str>, field_name: Box<str>,
}, },
#[error("`{from_type}` is not `{to_type}`")] #[display("`{from_type}` is not `{to_type}`")]
/// Tried to apply incompatible types. /// Tried to apply incompatible types.
MismatchedTypes { MismatchedTypes {
from_type: Box<str>, from_type: Box<str>,
to_type: Box<str>, to_type: Box<str>,
}, },
#[error("attempted to apply type with {from_size} size to a type with {to_size} size")] #[display("attempted to apply type with {from_size} size to a type with {to_size} size")]
/// Attempted to apply to types with mismatched sizez, e.g. a [u8; 4] to [u8; 3]. /// Attempted to apply to types with mismatched sizez, e.g. a [u8; 4] to [u8; 3].
DifferentSize { from_size: usize, to_size: usize }, DifferentSize { from_size: usize, to_size: usize },
#[error("variant with name `{variant_name}` does not exist on enum `{enum_name}`")] #[display("variant with name `{variant_name}` does not exist on enum `{enum_name}`")]
/// The enum we tried to apply to didn't contain a variant with the give name. /// The enum we tried to apply to didn't contain a variant with the give name.
UnknownVariant { UnknownVariant {
enum_name: Box<str>, enum_name: Box<str>,

View file

@ -8,7 +8,7 @@ use core::{
fmt::{Debug, Formatter}, fmt::{Debug, Formatter},
hash::Hash, hash::Hash,
}; };
use thiserror::Error; use derive_more::derive::{Display, Error};
/// A static accessor to compile-time type information. /// A static accessor to compile-time type information.
/// ///
@ -163,12 +163,12 @@ impl<T: Typed> DynamicTyped for T {
} }
/// A [`TypeInfo`]-specific error. /// A [`TypeInfo`]-specific error.
#[derive(Debug, Error)] #[derive(Debug, Error, Display)]
pub enum TypeInfoError { pub enum TypeInfoError {
/// Caused when a type was expected to be of a certain [kind], but was not. /// Caused when a type was expected to be of a certain [kind], but was not.
/// ///
/// [kind]: ReflectKind /// [kind]: ReflectKind
#[error("kind mismatch: expected {expected:?}, received {received:?}")] #[display("kind mismatch: expected {expected:?}, received {received:?}")]
KindMismatch { KindMismatch {
expected: ReflectKind, expected: ReflectKind,
received: ReflectKind, received: ReflectKind,