From 63119fba4550ae366c7b3f34b18092592178aa99 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Wed, 9 Oct 2024 19:31:39 +1100 Subject: [PATCH] `bevy_reflect` --- crates/bevy_reflect/Cargo.toml | 6 ++++- crates/bevy_reflect/src/enums/dynamic_enum.rs | 15 ++--------- crates/bevy_reflect/src/enums/variants.rs | 6 ++--- crates/bevy_reflect/src/func/args/error.rs | 12 +++++---- crates/bevy_reflect/src/func/error.rs | 16 ++++++------ crates/bevy_reflect/src/generics.rs | 15 ++--------- crates/bevy_reflect/src/kind.rs | 6 ++--- crates/bevy_reflect/src/path/mod.rs | 23 +++++----------- crates/bevy_reflect/src/path/parse.rs | 26 +++++++++++-------- crates/bevy_reflect/src/reflect.rs | 14 +++++----- crates/bevy_reflect/src/type_info.rs | 6 ++--- 11 files changed, 62 insertions(+), 83 deletions(-) diff --git a/crates/bevy_reflect/Cargo.toml b/crates/bevy_reflect/Cargo.toml index 1ce6ebc07bd39..2d7fff5e7c358 100644 --- a/crates/bevy_reflect/Cargo.toml +++ b/crates/bevy_reflect/Cargo.toml @@ -38,7 +38,11 @@ bevy_ptr = { path = "../bevy_ptr", version = "0.15.0-dev" } erased-serde = "0.4" disqualified = "1.0" downcast-rs = "1.2" -thiserror = "1.0" +derive_more = { version = "1", default-features = false, features = [ + "error", + "from", + "display", +] } serde = "1" smallvec = { version = "1.11", optional = true } assert_type_match = "0.1.1" diff --git a/crates/bevy_reflect/src/enums/dynamic_enum.rs b/crates/bevy_reflect/src/enums/dynamic_enum.rs index d925377b2f462..d114b6d86e0d8 100644 --- a/crates/bevy_reflect/src/enums/dynamic_enum.rs +++ b/crates/bevy_reflect/src/enums/dynamic_enum.rs @@ -7,9 +7,10 @@ use crate::{ }; use core::fmt::Formatter; +use derive_more::derive::From; /// A dynamic representation of an enum variant. -#[derive(Debug, Default)] +#[derive(Debug, Default, From)] pub enum DynamicVariant { #[default] Unit, @@ -27,18 +28,6 @@ impl Clone for DynamicVariant { } } -impl From for DynamicVariant { - fn from(dyn_tuple: DynamicTuple) -> Self { - Self::Tuple(dyn_tuple) - } -} - -impl From for DynamicVariant { - fn from(dyn_struct: DynamicStruct) -> Self { - Self::Struct(dyn_struct) - } -} - impl From<()> for DynamicVariant { fn from(_: ()) -> Self { Self::Unit diff --git a/crates/bevy_reflect/src/enums/variants.rs b/crates/bevy_reflect/src/enums/variants.rs index 6323e2a3c92d0..0830914794da1 100644 --- a/crates/bevy_reflect/src/enums/variants.rs +++ b/crates/bevy_reflect/src/enums/variants.rs @@ -6,7 +6,7 @@ use bevy_utils::HashMap; use core::slice::Iter; use alloc::sync::Arc; -use thiserror::Error; +use derive_more::derive::{Display, Error}; /// Describes the form of an enum variant. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] @@ -40,12 +40,12 @@ pub enum VariantType { } /// A [`VariantInfo`]-specific error. -#[derive(Debug, Error)] +#[derive(Debug, Error, Display)] pub enum VariantInfoError { /// Caused when a variant was expected to be of a certain [type], but was not. /// /// [type]: VariantType - #[error("variant type mismatch: expected {expected:?}, received {received:?}")] + #[display("variant type mismatch: expected {expected:?}, received {received:?}")] TypeMismatch { expected: VariantType, received: VariantType, diff --git a/crates/bevy_reflect/src/func/args/error.rs b/crates/bevy_reflect/src/func/args/error.rs index 13ebc0a7d3de4..65b7ea603e68a 100644 --- a/crates/bevy_reflect/src/func/args/error.rs +++ b/crates/bevy_reflect/src/func/args/error.rs @@ -1,23 +1,25 @@ use alloc::borrow::Cow; -use thiserror::Error; +use derive_more::derive::{Display, Error}; use crate::func::args::Ownership; /// An error that occurs when converting an [argument]. /// /// [argument]: crate::func::args::Arg -#[derive(Debug, Error, PartialEq)] +#[derive(Debug, Error, Display, PartialEq)] pub enum ArgError { /// 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 { index: usize, expected: Cow<'static, str>, received: Cow<'static, str>, }, /// 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 { index: usize, expected: Ownership, @@ -26,6 +28,6 @@ pub enum ArgError { /// Occurs when attempting to access an argument from an empty [`ArgList`]. /// /// [`ArgList`]: crate::func::args::ArgList - #[error("expected an argument but received none")] + #[display("expected an argument but received none")] EmptyArgList, } diff --git a/crates/bevy_reflect/src/func/error.rs b/crates/bevy_reflect/src/func/error.rs index 3d4f3d0a8b371..520fb78860116 100644 --- a/crates/bevy_reflect/src/func/error.rs +++ b/crates/bevy_reflect/src/func/error.rs @@ -1,18 +1,17 @@ use crate::func::{args::ArgError, Return}; use alloc::borrow::Cow; -use thiserror::Error; +use derive_more::derive::{Display, Error, From}; /// An error that occurs when calling a [`DynamicFunction`] or [`DynamicFunctionMut`]. /// /// [`DynamicFunction`]: crate::func::DynamicFunction /// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut -#[derive(Debug, Error, PartialEq)] +#[derive(Debug, Error, Display, PartialEq, From)] pub enum FunctionError { /// An error occurred while converting an argument. - #[error(transparent)] - ArgError(#[from] ArgError), + ArgError(ArgError), /// 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 }, } @@ -28,14 +27,15 @@ pub type FunctionResult<'a> = Result, FunctionError>; /// An error that occurs when registering a function into a [`FunctionRegistry`]. /// /// [`FunctionRegistry`]: crate::func::FunctionRegistry -#[derive(Debug, Error, PartialEq)] +#[derive(Debug, Error, Display, PartialEq)] pub enum FunctionRegistrationError { /// A function with the given name has already been registered. /// /// 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>), /// The function is missing a name by which it can be registered. - #[error("function name is missing")] + #[display("function name is missing")] MissingName, } diff --git a/crates/bevy_reflect/src/generics.rs b/crates/bevy_reflect/src/generics.rs index 4e92e8023a2df..f24c4493dea82 100644 --- a/crates/bevy_reflect/src/generics.rs +++ b/crates/bevy_reflect/src/generics.rs @@ -3,6 +3,7 @@ use crate::{Reflect, Type, TypePath}; use alloc::borrow::Cow; use alloc::sync::Arc; use core::ops::Deref; +use derive_more::derive::From; /// The generic parameters of a type. /// @@ -63,7 +64,7 @@ impl Deref for Generics { } /// An enum representing a generic parameter. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, From)] pub enum GenericInfo { /// A type parameter. /// @@ -100,18 +101,6 @@ impl GenericInfo { }); } -impl From for GenericInfo { - fn from(info: TypeParamInfo) -> Self { - Self::Type(info) - } -} - -impl From for GenericInfo { - fn from(info: ConstParamInfo) -> Self { - Self::Const(info) - } -} - /// Type information for a generic type parameter. /// /// An example of a type parameter would be `T` in `struct Foo`. diff --git a/crates/bevy_reflect/src/kind.rs b/crates/bevy_reflect/src/kind.rs index 0cb2f037c2ea1..2e3928162a73f 100644 --- a/crates/bevy_reflect/src/kind.rs +++ b/crates/bevy_reflect/src/kind.rs @@ -1,4 +1,4 @@ -use thiserror::Error; +use derive_more::derive::{Display, Error}; #[cfg(feature = "functions")] 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. /// /// [kind]: ReflectKind -#[derive(Debug, Error)] -#[error("kind mismatch: expected {expected:?}, received {received:?}")] +#[derive(Debug, Error, Display)] +#[display("kind mismatch: expected {expected:?}, received {received:?}")] pub struct ReflectKindMismatchError { pub expected: ReflectKind, pub received: ReflectKind, diff --git a/crates/bevy_reflect/src/path/mod.rs b/crates/bevy_reflect/src/path/mod.rs index bbfa6cad52bcd..9fc7d1b9f2213 100644 --- a/crates/bevy_reflect/src/path/mod.rs +++ b/crates/bevy_reflect/src/path/mod.rs @@ -10,24 +10,23 @@ use parse::PathParser; use crate::{PartialReflect, Reflect}; use core::fmt; -use thiserror::Error; +use derive_more::derive::{Display, From}; type PathResult<'a, T> = Result>; /// An error returned from a failed path string query. -#[derive(Debug, PartialEq, Eq, Error)] +#[derive(Debug, PartialEq, Eq, Display, From)] pub enum ReflectPathError<'a> { /// An error caused by trying to access a path that's not able to be accessed, /// see [`AccessError`] for details. - #[error(transparent)] InvalidAccess(AccessError<'a>), /// 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, /// 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 { /// Position in `path`. offset: usize, @@ -37,11 +36,8 @@ pub enum ReflectPathError<'a> { error: ParseError<'a>, }, } -impl<'a> From> for ReflectPathError<'a> { - fn from(value: AccessError<'a>) -> Self { - Self::InvalidAccess(value) - } -} + +impl<'a> core::error::Error for ReflectPathError<'a> {} /// Something that can be interpreted as a reflection path in [`GetPath`]. pub trait ReflectPath<'a>: Sized { @@ -355,7 +351,7 @@ impl From> for OffsetAccess { /// ]; /// 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( /// This is a vector of pre-parsed [`OffsetAccess`]es. pub Vec, @@ -447,11 +443,6 @@ impl<'a> ReflectPath<'a> for &'a ParsedPath { Ok(root) } } -impl From> for ParsedPath { - fn from(value: Vec) -> Self { - ParsedPath(value) - } -} impl From<[OffsetAccess; N]> for ParsedPath { fn from(value: [OffsetAccess; N]) -> Self { ParsedPath(value.to_vec()) diff --git a/crates/bevy_reflect/src/path/parse.rs b/crates/bevy_reflect/src/path/parse.rs index 0c85c4b93331a..40b39365ff054 100644 --- a/crates/bevy_reflect/src/path/parse.rs +++ b/crates/bevy_reflect/src/path/parse.rs @@ -4,34 +4,38 @@ use core::{ str::from_utf8_unchecked, }; -use thiserror::Error; +use derive_more::derive::{Display, Error, From}; use super::{Access, ReflectPathError}; /// An error that occurs when parsing reflect path strings. -#[derive(Debug, PartialEq, Eq, Error)] -#[error(transparent)] +#[derive(Debug, PartialEq, Eq, Error, Display)] +#[error(ignore)] pub struct ParseError<'a>(Error<'a>); /// A parse error for a path string. -#[derive(Debug, PartialEq, Eq, Error)] +#[derive(Debug, PartialEq, Eq, Error, Display, From)] enum Error<'a> { - #[error("expected an identifier, but reached end of path string")] + #[display("expected an identifier, but reached end of path string")] NoIdent, - #[error("expected an identifier, got '{0}' instead")] + #[display("expected an identifier, got '{_0}' instead")] + #[error(ignore)] + #[from(ignore)] ExpectedIdent(Token<'a>), - #[error("failed to parse index as integer")] - InvalidIndex(#[from] ParseIntError), + #[display("failed to parse index as integer")] + 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, - #[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>), - #[error("a ']' was found before an opening '['")] + #[display("a ']' was found before an opening '['")] CloseBeforeOpen, } diff --git a/crates/bevy_reflect/src/reflect.rs b/crates/bevy_reflect/src/reflect.rs index b572780896375..993366e61dd6d 100644 --- a/crates/bevy_reflect/src/reflect.rs +++ b/crates/bevy_reflect/src/reflect.rs @@ -8,39 +8,39 @@ use core::{ fmt::Debug, }; -use thiserror::Error; +use derive_more::derive::{Display, Error}; use crate::utility::NonGenericTypeInfoCell; /// 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 { - #[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. MismatchedKinds { from_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. MissingEnumField { variant_name: Box, field_name: Box, }, - #[error("`{from_type}` is not `{to_type}`")] + #[display("`{from_type}` is not `{to_type}`")] /// Tried to apply incompatible types. MismatchedTypes { from_type: Box, to_type: Box, }, - #[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]. 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. UnknownVariant { enum_name: Box, diff --git a/crates/bevy_reflect/src/type_info.rs b/crates/bevy_reflect/src/type_info.rs index 9330b8e4aaa25..3493e1e06d744 100644 --- a/crates/bevy_reflect/src/type_info.rs +++ b/crates/bevy_reflect/src/type_info.rs @@ -8,7 +8,7 @@ use core::{ fmt::{Debug, Formatter}, hash::Hash, }; -use thiserror::Error; +use derive_more::derive::{Display, Error}; /// A static accessor to compile-time type information. /// @@ -163,12 +163,12 @@ impl DynamicTyped for T { } /// A [`TypeInfo`]-specific error. -#[derive(Debug, Error)] +#[derive(Debug, Error, Display)] pub enum TypeInfoError { /// Caused when a type was expected to be of a certain [kind], but was not. /// /// [kind]: ReflectKind - #[error("kind mismatch: expected {expected:?}, received {received:?}")] + #[display("kind mismatch: expected {expected:?}, received {received:?}")] KindMismatch { expected: ReflectKind, received: ReflectKind,