Skip to content

Commit 3cc1527

Browse files
Remove thiserror from bevy_reflect (#15766)
# Objective - Contributes to #15460 ## Solution - Removed `thiserror` from `bevy_reflect`
1 parent 46ad0b7 commit 3cc1527

File tree

11 files changed

+62
-83
lines changed

11 files changed

+62
-83
lines changed

crates/bevy_reflect/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ bevy_ptr = { path = "../bevy_ptr", version = "0.15.0-dev" }
3838
erased-serde = "0.4"
3939
disqualified = "1.0"
4040
downcast-rs = "1.2"
41-
thiserror = "1.0"
41+
derive_more = { version = "1", default-features = false, features = [
42+
"error",
43+
"from",
44+
"display",
45+
] }
4246
serde = "1"
4347
smallvec = { version = "1.11", optional = true }
4448
assert_type_match = "0.1.1"

crates/bevy_reflect/src/enums/dynamic_enum.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use crate::{
77
};
88

99
use core::fmt::Formatter;
10+
use derive_more::derive::From;
1011

1112
/// A dynamic representation of an enum variant.
12-
#[derive(Debug, Default)]
13+
#[derive(Debug, Default, From)]
1314
pub enum DynamicVariant {
1415
#[default]
1516
Unit,
@@ -27,18 +28,6 @@ impl Clone for DynamicVariant {
2728
}
2829
}
2930

30-
impl From<DynamicTuple> for DynamicVariant {
31-
fn from(dyn_tuple: DynamicTuple) -> Self {
32-
Self::Tuple(dyn_tuple)
33-
}
34-
}
35-
36-
impl From<DynamicStruct> for DynamicVariant {
37-
fn from(dyn_struct: DynamicStruct) -> Self {
38-
Self::Struct(dyn_struct)
39-
}
40-
}
41-
4231
impl From<()> for DynamicVariant {
4332
fn from(_: ()) -> Self {
4433
Self::Unit

crates/bevy_reflect/src/enums/variants.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy_utils::HashMap;
66
use core::slice::Iter;
77

88
use alloc::sync::Arc;
9-
use thiserror::Error;
9+
use derive_more::derive::{Display, Error};
1010

1111
/// Describes the form of an enum variant.
1212
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
@@ -40,12 +40,12 @@ pub enum VariantType {
4040
}
4141

4242
/// A [`VariantInfo`]-specific error.
43-
#[derive(Debug, Error)]
43+
#[derive(Debug, Error, Display)]
4444
pub enum VariantInfoError {
4545
/// Caused when a variant was expected to be of a certain [type], but was not.
4646
///
4747
/// [type]: VariantType
48-
#[error("variant type mismatch: expected {expected:?}, received {received:?}")]
48+
#[display("variant type mismatch: expected {expected:?}, received {received:?}")]
4949
TypeMismatch {
5050
expected: VariantType,
5151
received: VariantType,
+7-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
use alloc::borrow::Cow;
22

3-
use thiserror::Error;
3+
use derive_more::derive::{Display, Error};
44

55
use crate::func::args::Ownership;
66

77
/// An error that occurs when converting an [argument].
88
///
99
/// [argument]: crate::func::args::Arg
10-
#[derive(Debug, Error, PartialEq)]
10+
#[derive(Debug, Error, Display, PartialEq)]
1111
pub enum ArgError {
1212
/// The argument is not the expected type.
13-
#[error("expected `{expected}` but received `{received}` (@ argument index {index})")]
13+
#[display("expected `{expected}` but received `{received}` (@ argument index {index})")]
1414
UnexpectedType {
1515
index: usize,
1616
expected: Cow<'static, str>,
1717
received: Cow<'static, str>,
1818
},
1919
/// The argument has the wrong ownership.
20-
#[error("expected {expected} value but received {received} value (@ argument index {index})")]
20+
#[display(
21+
"expected {expected} value but received {received} value (@ argument index {index})"
22+
)]
2123
InvalidOwnership {
2224
index: usize,
2325
expected: Ownership,
@@ -26,6 +28,6 @@ pub enum ArgError {
2628
/// Occurs when attempting to access an argument from an empty [`ArgList`].
2729
///
2830
/// [`ArgList`]: crate::func::args::ArgList
29-
#[error("expected an argument but received none")]
31+
#[display("expected an argument but received none")]
3032
EmptyArgList,
3133
}

crates/bevy_reflect/src/func/error.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use crate::func::{args::ArgError, Return};
22
use alloc::borrow::Cow;
3-
use thiserror::Error;
3+
use derive_more::derive::{Display, Error, From};
44

55
/// An error that occurs when calling a [`DynamicFunction`] or [`DynamicFunctionMut`].
66
///
77
/// [`DynamicFunction`]: crate::func::DynamicFunction
88
/// [`DynamicFunctionMut`]: crate::func::DynamicFunctionMut
9-
#[derive(Debug, Error, PartialEq)]
9+
#[derive(Debug, Error, Display, PartialEq, From)]
1010
pub enum FunctionError {
1111
/// An error occurred while converting an argument.
12-
#[error(transparent)]
13-
ArgError(#[from] ArgError),
12+
ArgError(ArgError),
1413
/// The number of arguments provided does not match the expected number.
15-
#[error("expected {expected} arguments but received {received}")]
14+
#[display("expected {expected} arguments but received {received}")]
1615
ArgCountMismatch { expected: usize, received: usize },
1716
}
1817

@@ -28,14 +27,15 @@ pub type FunctionResult<'a> = Result<Return<'a>, FunctionError>;
2827
/// An error that occurs when registering a function into a [`FunctionRegistry`].
2928
///
3029
/// [`FunctionRegistry`]: crate::func::FunctionRegistry
31-
#[derive(Debug, Error, PartialEq)]
30+
#[derive(Debug, Error, Display, PartialEq)]
3231
pub enum FunctionRegistrationError {
3332
/// A function with the given name has already been registered.
3433
///
3534
/// Contains the duplicate function name.
36-
#[error("a function has already been registered with name {0:?}")]
35+
#[display("a function has already been registered with name {_0:?}")]
36+
#[error(ignore)]
3737
DuplicateName(Cow<'static, str>),
3838
/// The function is missing a name by which it can be registered.
39-
#[error("function name is missing")]
39+
#[display("function name is missing")]
4040
MissingName,
4141
}

crates/bevy_reflect/src/generics.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{Reflect, Type, TypePath};
33
use alloc::borrow::Cow;
44
use alloc::sync::Arc;
55
use core::ops::Deref;
6+
use derive_more::derive::From;
67

78
/// The generic parameters of a type.
89
///
@@ -63,7 +64,7 @@ impl Deref for Generics {
6364
}
6465

6566
/// An enum representing a generic parameter.
66-
#[derive(Clone, Debug)]
67+
#[derive(Clone, Debug, From)]
6768
pub enum GenericInfo {
6869
/// A type parameter.
6970
///
@@ -100,18 +101,6 @@ impl GenericInfo {
100101
});
101102
}
102103

103-
impl From<TypeParamInfo> for GenericInfo {
104-
fn from(info: TypeParamInfo) -> Self {
105-
Self::Type(info)
106-
}
107-
}
108-
109-
impl From<ConstParamInfo> for GenericInfo {
110-
fn from(info: ConstParamInfo) -> Self {
111-
Self::Const(info)
112-
}
113-
}
114-
115104
/// Type information for a generic type parameter.
116105
///
117106
/// An example of a type parameter would be `T` in `struct Foo<T>`.

crates/bevy_reflect/src/kind.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use thiserror::Error;
1+
use derive_more::derive::{Display, Error};
22

33
#[cfg(feature = "functions")]
44
use crate::func::Function;
@@ -130,8 +130,8 @@ macro_rules! impl_reflect_kind_conversions {
130130
/// Caused when a type was expected to be of a certain [kind], but was not.
131131
///
132132
/// [kind]: ReflectKind
133-
#[derive(Debug, Error)]
134-
#[error("kind mismatch: expected {expected:?}, received {received:?}")]
133+
#[derive(Debug, Error, Display)]
134+
#[display("kind mismatch: expected {expected:?}, received {received:?}")]
135135
pub struct ReflectKindMismatchError {
136136
pub expected: ReflectKind,
137137
pub received: ReflectKind,

crates/bevy_reflect/src/path/mod.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@ use parse::PathParser;
1010

1111
use crate::{PartialReflect, Reflect};
1212
use core::fmt;
13-
use thiserror::Error;
13+
use derive_more::derive::{Display, From};
1414

1515
type PathResult<'a, T> = Result<T, ReflectPathError<'a>>;
1616

1717
/// An error returned from a failed path string query.
18-
#[derive(Debug, PartialEq, Eq, Error)]
18+
#[derive(Debug, PartialEq, Eq, Display, From)]
1919
pub enum ReflectPathError<'a> {
2020
/// An error caused by trying to access a path that's not able to be accessed,
2121
/// see [`AccessError`] for details.
22-
#[error(transparent)]
2322
InvalidAccess(AccessError<'a>),
2423

2524
/// An error that occurs when a type cannot downcast to a given type.
26-
#[error("Can't downcast result of access to the given type")]
25+
#[display("Can't downcast result of access to the given type")]
2726
InvalidDowncast,
2827

2928
/// An error caused by an invalid path string that couldn't be parsed.
30-
#[error("Encountered an error at offset {offset} while parsing `{path}`: {error}")]
29+
#[display("Encountered an error at offset {offset} while parsing `{path}`: {error}")]
3130
ParseError {
3231
/// Position in `path`.
3332
offset: usize,
@@ -37,11 +36,8 @@ pub enum ReflectPathError<'a> {
3736
error: ParseError<'a>,
3837
},
3938
}
40-
impl<'a> From<AccessError<'a>> for ReflectPathError<'a> {
41-
fn from(value: AccessError<'a>) -> Self {
42-
Self::InvalidAccess(value)
43-
}
44-
}
39+
40+
impl<'a> core::error::Error for ReflectPathError<'a> {}
4541

4642
/// Something that can be interpreted as a reflection path in [`GetPath`].
4743
pub trait ReflectPath<'a>: Sized {
@@ -355,7 +351,7 @@ impl From<Access<'static>> for OffsetAccess {
355351
/// ];
356352
/// let my_path = ParsedPath::from(path_elements);
357353
/// ```
358-
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash)]
354+
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Hash, From)]
359355
pub struct ParsedPath(
360356
/// This is a vector of pre-parsed [`OffsetAccess`]es.
361357
pub Vec<OffsetAccess>,
@@ -447,11 +443,6 @@ impl<'a> ReflectPath<'a> for &'a ParsedPath {
447443
Ok(root)
448444
}
449445
}
450-
impl From<Vec<OffsetAccess>> for ParsedPath {
451-
fn from(value: Vec<OffsetAccess>) -> Self {
452-
ParsedPath(value)
453-
}
454-
}
455446
impl<const N: usize> From<[OffsetAccess; N]> for ParsedPath {
456447
fn from(value: [OffsetAccess; N]) -> Self {
457448
ParsedPath(value.to_vec())

crates/bevy_reflect/src/path/parse.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,38 @@ use core::{
44
str::from_utf8_unchecked,
55
};
66

7-
use thiserror::Error;
7+
use derive_more::derive::{Display, Error, From};
88

99
use super::{Access, ReflectPathError};
1010

1111
/// An error that occurs when parsing reflect path strings.
12-
#[derive(Debug, PartialEq, Eq, Error)]
13-
#[error(transparent)]
12+
#[derive(Debug, PartialEq, Eq, Error, Display)]
13+
#[error(ignore)]
1414
pub struct ParseError<'a>(Error<'a>);
1515

1616
/// A parse error for a path string.
17-
#[derive(Debug, PartialEq, Eq, Error)]
17+
#[derive(Debug, PartialEq, Eq, Error, Display, From)]
1818
enum Error<'a> {
19-
#[error("expected an identifier, but reached end of path string")]
19+
#[display("expected an identifier, but reached end of path string")]
2020
NoIdent,
2121

22-
#[error("expected an identifier, got '{0}' instead")]
22+
#[display("expected an identifier, got '{_0}' instead")]
23+
#[error(ignore)]
24+
#[from(ignore)]
2325
ExpectedIdent(Token<'a>),
2426

25-
#[error("failed to parse index as integer")]
26-
InvalidIndex(#[from] ParseIntError),
27+
#[display("failed to parse index as integer")]
28+
InvalidIndex(ParseIntError),
2729

28-
#[error("a '[' wasn't closed, reached end of path string before finding a ']'")]
30+
#[display("a '[' wasn't closed, reached end of path string before finding a ']'")]
2931
Unclosed,
3032

31-
#[error("a '[' wasn't closed properly, got '{0}' instead")]
33+
#[display("a '[' wasn't closed properly, got '{_0}' instead")]
34+
#[error(ignore)]
35+
#[from(ignore)]
3236
BadClose(Token<'a>),
3337

34-
#[error("a ']' was found before an opening '['")]
38+
#[display("a ']' was found before an opening '['")]
3539
CloseBeforeOpen,
3640
}
3741

crates/bevy_reflect/src/reflect.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,39 @@ use core::{
88
fmt::Debug,
99
};
1010

11-
use thiserror::Error;
11+
use derive_more::derive::{Display, Error};
1212

1313
use crate::utility::NonGenericTypeInfoCell;
1414

1515
/// A enumeration of all error outcomes that might happen when running [`try_apply`](PartialReflect::try_apply).
16-
#[derive(Error, Debug)]
16+
#[derive(Error, Display, Debug)]
1717
pub enum ApplyError {
18-
#[error("attempted to apply `{from_kind}` to `{to_kind}`")]
18+
#[display("attempted to apply `{from_kind}` to `{to_kind}`")]
1919
/// Attempted to apply the wrong [kind](ReflectKind) to a type, e.g. a struct to a enum.
2020
MismatchedKinds {
2121
from_kind: ReflectKind,
2222
to_kind: ReflectKind,
2323
},
2424

25-
#[error("enum variant `{variant_name}` doesn't have a field named `{field_name}`")]
25+
#[display("enum variant `{variant_name}` doesn't have a field named `{field_name}`")]
2626
/// Enum variant that we tried to apply to was missing a field.
2727
MissingEnumField {
2828
variant_name: Box<str>,
2929
field_name: Box<str>,
3030
},
3131

32-
#[error("`{from_type}` is not `{to_type}`")]
32+
#[display("`{from_type}` is not `{to_type}`")]
3333
/// Tried to apply incompatible types.
3434
MismatchedTypes {
3535
from_type: Box<str>,
3636
to_type: Box<str>,
3737
},
3838

39-
#[error("attempted to apply type with {from_size} size to a type with {to_size} size")]
39+
#[display("attempted to apply type with {from_size} size to a type with {to_size} size")]
4040
/// Attempted to apply to types with mismatched sizez, e.g. a [u8; 4] to [u8; 3].
4141
DifferentSize { from_size: usize, to_size: usize },
4242

43-
#[error("variant with name `{variant_name}` does not exist on enum `{enum_name}`")]
43+
#[display("variant with name `{variant_name}` does not exist on enum `{enum_name}`")]
4444
/// The enum we tried to apply to didn't contain a variant with the give name.
4545
UnknownVariant {
4646
enum_name: Box<str>,

crates/bevy_reflect/src/type_info.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::{
88
fmt::{Debug, Formatter},
99
hash::Hash,
1010
};
11-
use thiserror::Error;
11+
use derive_more::derive::{Display, Error};
1212

1313
/// A static accessor to compile-time type information.
1414
///
@@ -163,12 +163,12 @@ impl<T: Typed> DynamicTyped for T {
163163
}
164164

165165
/// A [`TypeInfo`]-specific error.
166-
#[derive(Debug, Error)]
166+
#[derive(Debug, Error, Display)]
167167
pub enum TypeInfoError {
168168
/// Caused when a type was expected to be of a certain [kind], but was not.
169169
///
170170
/// [kind]: ReflectKind
171-
#[error("kind mismatch: expected {expected:?}, received {received:?}")]
171+
#[display("kind mismatch: expected {expected:?}, received {received:?}")]
172172
KindMismatch {
173173
expected: ReflectKind,
174174
received: ReflectKind,

0 commit comments

Comments
 (0)