From 0a61f04d9b435306e4664e2614bdf2a438e2a766 Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Thu, 10 Oct 2024 01:20:58 +1100 Subject: [PATCH] Remove `thiserror` from `bevy_ecs` (#15774) # Objective - Contributes to #15460 ## Solution - Removed `thiserror` from `bevy_ecs` --- crates/bevy_ecs/Cargo.toml | 8 ++++- crates/bevy_ecs/src/component.rs | 12 +++++--- crates/bevy_ecs/src/query/access.rs | 9 ++---- crates/bevy_ecs/src/query/error.rs | 10 ++++--- crates/bevy_ecs/src/removal_detection.rs | 10 ++----- crates/bevy_ecs/src/schedule/schedule.rs | 29 ++++++++++--------- crates/bevy_ecs/src/schedule/stepping.rs | 11 ++++--- crates/bevy_ecs/src/system/system.rs | 7 +++-- crates/bevy_ecs/src/system/system_name.rs | 23 ++------------- crates/bevy_ecs/src/system/system_registry.rs | 14 ++++----- crates/bevy_ecs/src/world/entity_ref.rs | 12 +++++--- crates/bevy_ecs/src/world/error.rs | 23 +++++++++------ crates/bevy_ecs/src/world/reflect.rs | 16 ++++++---- 13 files changed, 94 insertions(+), 90 deletions(-) diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index 9b10c1f981e32..b3939d97894a2 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -32,7 +32,13 @@ concurrent-queue = "2.4.0" disqualified = "1.0" fixedbitset = "0.5" serde = { version = "1", optional = true, default-features = false } -thiserror = "1.0" +derive_more = { version = "1", default-features = false, features = [ + "error", + "from", + "display", + "into", + "as_ref", +] } nonmax = "0.5" arrayvec = { version = "0.7.4", optional = true } smallvec = "1" diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 5c443e8bcc1c5..d9bdbedc333ad 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -27,7 +27,7 @@ use core::{ marker::PhantomData, mem::needs_drop, }; -use thiserror::Error; +use derive_more::derive::{Display, Error}; /// A data type that can be used to store data for an [entity]. /// @@ -1623,14 +1623,18 @@ impl FromWorld for InitComponentId { } /// An error returned when the registration of a required component fails. -#[derive(Error, Debug)] +#[derive(Error, Display, Debug)] #[non_exhaustive] pub enum RequiredComponentsError { /// The component is already a directly required component for the requiree. - #[error("Component {0:?} already directly requires component {1:?}")] + #[display("Component {0:?} already directly requires component {_1:?}")] + #[error(ignore)] DuplicateRegistration(ComponentId, ComponentId), /// An archetype with the component that requires other components already exists - #[error("An archetype with the component {0:?} that requires other components already exists")] + #[display( + "An archetype with the component {_0:?} that requires other components already exists" + )] + #[error(ignore)] ArchetypeExists(ComponentId), } diff --git a/crates/bevy_ecs/src/query/access.rs b/crates/bevy_ecs/src/query/access.rs index 927e8ebcab72d..28dba05f87f6a 100644 --- a/crates/bevy_ecs/src/query/access.rs +++ b/crates/bevy_ecs/src/query/access.rs @@ -2,6 +2,7 @@ use crate::component::ComponentId; use crate::storage::SparseSetIndex; use crate::world::World; use core::{fmt, fmt::Debug, marker::PhantomData}; +use derive_more::derive::From; use fixedbitset::FixedBitSet; /// A wrapper struct to make Debug representations of [`FixedBitSet`] easier @@ -856,7 +857,7 @@ impl From> for FilteredAccessSet { } /// Records how two accesses conflict with each other -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, From)] pub enum AccessConflicts { /// Conflict is for all indices All, @@ -908,12 +909,6 @@ impl AccessConflicts { } } -impl From for AccessConflicts { - fn from(value: FixedBitSet) -> Self { - Self::Individual(value) - } -} - impl From> for AccessConflicts { fn from(value: Vec) -> Self { Self::Individual(value.iter().map(T::sparse_set_index).collect()) diff --git a/crates/bevy_ecs/src/query/error.rs b/crates/bevy_ecs/src/query/error.rs index 6778a75856249..90df07b6a9308 100644 --- a/crates/bevy_ecs/src/query/error.rs +++ b/crates/bevy_ecs/src/query/error.rs @@ -1,4 +1,4 @@ -use thiserror::Error; +use derive_more::derive::{Display, Error}; use crate::{entity::Entity, world::unsafe_world_cell::UnsafeWorldCell}; @@ -90,13 +90,15 @@ impl<'w> Eq for QueryEntityError<'w> {} /// An error that occurs when evaluating a [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState) as a single expected result via /// [`get_single`](crate::system::Query::get_single) or [`get_single_mut`](crate::system::Query::get_single_mut). -#[derive(Debug, Error)] +#[derive(Debug, Error, Display)] pub enum QuerySingleError { /// No entity fits the query. - #[error("No entities fit the query {0}")] + #[display("No entities fit the query {_0}")] + #[error(ignore)] NoEntities(&'static str), /// Multiple entities fit the query. - #[error("Multiple entities fit the query {0}")] + #[display("Multiple entities fit the query {_0}")] + #[error(ignore)] MultipleEntities(&'static str), } diff --git a/crates/bevy_ecs/src/removal_detection.rs b/crates/bevy_ecs/src/removal_detection.rs index e4ecaad7431a8..2ef74e10a91d3 100644 --- a/crates/bevy_ecs/src/removal_detection.rs +++ b/crates/bevy_ecs/src/removal_detection.rs @@ -11,6 +11,8 @@ use crate::{ world::{unsafe_world_cell::UnsafeWorldCell, World}, }; +use derive_more::derive::Into; + use core::{ fmt::Debug, iter, @@ -21,15 +23,9 @@ use core::{ /// Wrapper around [`Entity`] for [`RemovedComponents`]. /// Internally, `RemovedComponents` uses these as an `Events`. -#[derive(Event, Debug, Clone)] +#[derive(Event, Debug, Clone, Into)] pub struct RemovedComponentEntity(Entity); -impl From for Entity { - fn from(value: RemovedComponentEntity) -> Self { - value.0 - } -} - /// Wrapper around a [`EventCursor`] so that we /// can differentiate events between components. #[derive(Debug)] diff --git a/crates/bevy_ecs/src/schedule/schedule.rs b/crates/bevy_ecs/src/schedule/schedule.rs index 65e62b0b9eb1b..fd2837adb0dc7 100644 --- a/crates/bevy_ecs/src/schedule/schedule.rs +++ b/crates/bevy_ecs/src/schedule/schedule.rs @@ -8,10 +8,10 @@ use bevy_utils::{ tracing::{error, info, warn}, HashMap, HashSet, }; +use derive_more::derive::{Display, Error}; use disqualified::ShortName; use fixedbitset::FixedBitSet; use petgraph::{algo::TarjanScc, prelude::*}; -use thiserror::Error; use crate::{ self as bevy_ecs, @@ -1934,42 +1934,43 @@ impl ScheduleGraph { } /// Category of errors encountered during schedule construction. -#[derive(Error, Debug)] +#[derive(Error, Display, Debug)] +#[error(ignore)] #[non_exhaustive] pub enum ScheduleBuildError { /// A system set contains itself. - #[error("System set `{0}` contains itself.")] + #[display("System set `{_0}` contains itself.")] HierarchyLoop(String), /// The hierarchy of system sets contains a cycle. - #[error("System set hierarchy contains cycle(s).\n{0}")] + #[display("System set hierarchy contains cycle(s).\n{_0}")] HierarchyCycle(String), /// The hierarchy of system sets contains redundant edges. /// /// This error is disabled by default, but can be opted-in using [`ScheduleBuildSettings`]. - #[error("System set hierarchy contains redundant edges.\n{0}")] + #[display("System set hierarchy contains redundant edges.\n{_0}")] HierarchyRedundancy(String), /// A system (set) has been told to run before itself. - #[error("System set `{0}` depends on itself.")] + #[display("System set `{_0}` depends on itself.")] DependencyLoop(String), /// The dependency graph contains a cycle. - #[error("System dependencies contain cycle(s).\n{0}")] + #[display("System dependencies contain cycle(s).\n{_0}")] DependencyCycle(String), /// Tried to order a system (set) relative to a system set it belongs to. - #[error("`{0}` and `{1}` have both `in_set` and `before`-`after` relationships (these might be transitive). This combination is unsolvable as a system cannot run before or after a set it belongs to.")] + #[display("`{0}` and `{_1}` have both `in_set` and `before`-`after` relationships (these might be transitive). This combination is unsolvable as a system cannot run before or after a set it belongs to.")] CrossDependency(String, String), /// Tried to order system sets that share systems. - #[error("`{0}` and `{1}` have a `before`-`after` relationship (which may be transitive) but share systems.")] + #[display("`{0}` and `{_1}` have a `before`-`after` relationship (which may be transitive) but share systems.")] SetsHaveOrderButIntersect(String, String), /// Tried to order a system (set) relative to all instances of some system function. - #[error("Tried to order against `{0}` in a schedule that has more than one `{0}` instance. `{0}` is a `SystemTypeSet` and cannot be used for ordering if ambiguous. Use a different set without this restriction.")] + #[display("Tried to order against `{0}` in a schedule that has more than one `{0}` instance. `{_0}` is a `SystemTypeSet` and cannot be used for ordering if ambiguous. Use a different set without this restriction.")] SystemTypeSetAmbiguity(String), /// Systems with conflicting access have indeterminate run order. /// /// This error is disabled by default, but can be opted-in using [`ScheduleBuildSettings`]. - #[error("Systems with conflicting access have indeterminate run order.\n{0}")] + #[display("Systems with conflicting access have indeterminate run order.\n{_0}")] Ambiguity(String), /// Tried to run a schedule before all of its systems have been initialized. - #[error("Systems in schedule have not been initialized.")] + #[display("Systems in schedule have not been initialized.")] Uninitialized, } @@ -2040,8 +2041,8 @@ impl ScheduleBuildSettings { /// Error to denote that [`Schedule::initialize`] or [`Schedule::run`] has not yet been called for /// this schedule. -#[derive(Error, Debug)] -#[error("executable schedule has not been built")] +#[derive(Error, Display, Debug)] +#[display("executable schedule has not been built")] pub struct ScheduleNotInitialized; #[cfg(test)] diff --git a/crates/bevy_ecs/src/schedule/stepping.rs b/crates/bevy_ecs/src/schedule/stepping.rs index 622ec6ea03e45..61cfc3ca244cb 100644 --- a/crates/bevy_ecs/src/schedule/stepping.rs +++ b/crates/bevy_ecs/src/schedule/stepping.rs @@ -7,10 +7,13 @@ use crate::{ system::{IntoSystem, ResMut, Resource}, }; use bevy_utils::{ - tracing::{error, info, warn}, + tracing::{info, warn}, TypeIdMap, }; -use thiserror::Error; +use derive_more::derive::{Display, Error}; + +#[cfg(not(feature = "bevy_debug_stepping"))] +use bevy_utils::tracing::error; #[cfg(test)] use bevy_utils::tracing::debug; @@ -87,8 +90,8 @@ enum Update { ClearBehavior(InternedScheduleLabel, SystemIdentifier), } -#[derive(Error, Debug)] -#[error("not available until all configured schedules have been run; try again next frame")] +#[derive(Error, Display, Debug)] +#[display("not available until all configured schedules have been run; try again next frame")] pub struct NotReady; #[derive(Resource, Default)] diff --git a/crates/bevy_ecs/src/system/system.rs b/crates/bevy_ecs/src/system/system.rs index c1c628cb2e1aa..e68287c699880 100644 --- a/crates/bevy_ecs/src/system/system.rs +++ b/crates/bevy_ecs/src/system/system.rs @@ -1,6 +1,6 @@ use bevy_utils::tracing::warn; use core::fmt::Debug; -use thiserror::Error; +use derive_more::derive::{Display, Error}; use crate::{ archetype::ArchetypeComponentId, @@ -357,12 +357,13 @@ impl RunSystemOnce for &mut World { } /// Running system failed. -#[derive(Error)] +#[derive(Error, Display)] pub enum RunSystemError { /// System could not be run due to parameters that failed validation. /// /// This can occur because the data required by the system was not present in the world. - #[error("The data required by the system {0:?} was not found in the world and the system did not run due to failed parameter validation.")] + #[display("The data required by the system {_0:?} was not found in the world and the system did not run due to failed parameter validation.")] + #[error(ignore)] InvalidParams(Cow<'static, str>), } diff --git a/crates/bevy_ecs/src/system/system_name.rs b/crates/bevy_ecs/src/system/system_name.rs index 5a3763465cd8a..3ecc901baae2b 100644 --- a/crates/bevy_ecs/src/system/system_name.rs +++ b/crates/bevy_ecs/src/system/system_name.rs @@ -6,6 +6,7 @@ use crate::{ }; use alloc::borrow::Cow; use core::ops::Deref; +use derive_more::derive::{AsRef, Display, Into}; /// [`SystemParam`] that returns the name of the system which it is used in. /// @@ -33,7 +34,8 @@ use core::ops::Deref; /// logger.log("Hello"); /// } /// ``` -#[derive(Debug)] +#[derive(Debug, Into, Display, AsRef)] +#[as_ref(str)] pub struct SystemName<'s>(&'s str); impl<'s> SystemName<'s> { @@ -50,25 +52,6 @@ impl<'s> Deref for SystemName<'s> { } } -impl<'s> AsRef for SystemName<'s> { - fn as_ref(&self) -> &str { - self.name() - } -} - -impl<'s> From> for &'s str { - fn from(name: SystemName<'s>) -> &'s str { - name.0 - } -} - -impl<'s> core::fmt::Display for SystemName<'s> { - #[inline(always)] - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - core::fmt::Display::fmt(&self.name(), f) - } -} - // SAFETY: no component value access unsafe impl SystemParam for SystemName<'_> { type State = Cow<'static, str>; diff --git a/crates/bevy_ecs/src/system/system_registry.rs b/crates/bevy_ecs/src/system/system_registry.rs index fd4518e633e67..1aa17709345e3 100644 --- a/crates/bevy_ecs/src/system/system_registry.rs +++ b/crates/bevy_ecs/src/system/system_registry.rs @@ -12,7 +12,7 @@ use bevy_ecs_macros::{Component, Resource}; #[cfg(feature = "bevy_reflect")] use bevy_reflect::Reflect; use core::marker::PhantomData; -use thiserror::Error; +use derive_more::derive::{Display, Error}; /// A small wrapper for [`BoxedSystem`] that also keeps track whether or not the system has been initialized. #[derive(Component)] @@ -587,28 +587,28 @@ where } /// An operation with stored systems failed. -#[derive(Error)] +#[derive(Error, Display)] pub enum RegisteredSystemError { /// A system was run by id, but no system with that id was found. /// /// Did you forget to register it? - #[error("System {0:?} was not registered")] + #[display("System {_0:?} was not registered")] SystemIdNotRegistered(SystemId), /// A cached system was removed by value, but no system with its type was found. /// /// Did you forget to register it? - #[error("Cached system was not found")] + #[display("Cached system was not found")] SystemNotCached, /// A system tried to run itself recursively. - #[error("System {0:?} tried to run itself recursively")] + #[display("System {_0:?} tried to run itself recursively")] Recursive(SystemId), /// A system tried to remove itself. - #[error("System {0:?} tried to remove itself")] + #[display("System {_0:?} tried to remove itself")] SelfRemove(SystemId), /// System could not be run due to parameters that failed validation. /// /// This can occur because the data required by the system was not present in the world. - #[error("The data required by the system {0:?} was not found in the world and the system did not run due to failed parameter validation.")] + #[display("The data required by the system {_0:?} was not found in the world and the system did not run due to failed parameter validation.")] InvalidParams(SystemId), } diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 57d45366c6940..1097dba575644 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -15,7 +15,7 @@ use crate::{ use bevy_ptr::{OwningPtr, Ptr}; use bevy_utils::{HashMap, HashSet}; use core::{any::TypeId, marker::PhantomData, mem::MaybeUninit}; -use thiserror::Error; +use derive_more::derive::{Display, Error}; use super::{unsafe_world_cell::UnsafeEntityCell, Ref, ON_REMOVE, ON_REPLACE}; @@ -2774,12 +2774,16 @@ impl<'a> From<&'a mut EntityWorldMut<'_>> for FilteredEntityMut<'a> { } } -#[derive(Error, Debug)] +#[derive(Error, Display, Debug)] pub enum TryFromFilteredError { - #[error("Conversion failed, filtered entity ref does not have read access to all components")] + #[display( + "Conversion failed, filtered entity ref does not have read access to all components" + )] MissingReadAllAccess, - #[error("Conversion failed, filtered entity ref does not have write access to all components")] + #[display( + "Conversion failed, filtered entity ref does not have write access to all components" + )] MissingWriteAllAccess, } diff --git a/crates/bevy_ecs/src/world/error.rs b/crates/bevy_ecs/src/world/error.rs index 91cd2fa034018..71589789df52d 100644 --- a/crates/bevy_ecs/src/world/error.rs +++ b/crates/bevy_ecs/src/world/error.rs @@ -1,34 +1,39 @@ //! Contains error types returned by bevy's schedule. -use thiserror::Error; +use derive_more::derive::{Display, Error}; use crate::{component::ComponentId, entity::Entity, schedule::InternedScheduleLabel}; /// The error type returned by [`World::try_run_schedule`] if the provided schedule does not exist. /// /// [`World::try_run_schedule`]: crate::world::World::try_run_schedule -#[derive(Error, Debug)] -#[error("The schedule with the label {0:?} was not found.")] +#[derive(Error, Display, Debug)] +#[display("The schedule with the label {_0:?} was not found.")] +#[error(ignore)] pub struct TryRunScheduleError(pub InternedScheduleLabel); /// An error that occurs when dynamically retrieving components from an entity. -#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Error, Display, Debug, Clone, Copy, PartialEq, Eq)] pub enum EntityComponentError { /// The component with the given [`ComponentId`] does not exist on the entity. - #[error("The component with ID {0:?} does not exist on the entity.")] + #[display("The component with ID {_0:?} does not exist on the entity.")] + #[error(ignore)] MissingComponent(ComponentId), /// The component with the given [`ComponentId`] was requested mutably more than once. - #[error("The component with ID {0:?} was requested mutably more than once.")] + #[display("The component with ID {_0:?} was requested mutably more than once.")] + #[error(ignore)] AliasedMutability(ComponentId), } /// An error that occurs when fetching entities mutably from a world. -#[derive(Error, Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Error, Display, Debug, Clone, Copy, PartialEq, Eq)] pub enum EntityFetchError { /// The entity with the given ID does not exist. - #[error("The entity with ID {0:?} does not exist.")] + #[display("The entity with ID {_0:?} does not exist.")] + #[error(ignore)] NoSuchEntity(Entity), /// The entity with the given ID was requested mutably more than once. - #[error("The entity with ID {0:?} was requested mutably more than once.")] + #[display("The entity with ID {_0:?} was requested mutably more than once.")] + #[error(ignore)] AliasedMutability(Entity), } diff --git a/crates/bevy_ecs/src/world/reflect.rs b/crates/bevy_ecs/src/world/reflect.rs index f0f982926549b..cb6c76920eee3 100644 --- a/crates/bevy_ecs/src/world/reflect.rs +++ b/crates/bevy_ecs/src/world/reflect.rs @@ -2,7 +2,7 @@ use core::any::TypeId; -use thiserror::Error; +use derive_more::derive::{Display, Error}; use bevy_reflect::{Reflect, ReflectFromPtr}; @@ -198,7 +198,7 @@ impl World { } /// The error type returned by [`World::get_reflect`] and [`World::get_reflect_mut`]. -#[derive(Error, Debug)] +#[derive(Error, Display, Debug)] pub enum GetComponentReflectError { /// There is no [`ComponentId`] corresponding to the given [`TypeId`]. /// @@ -208,11 +208,14 @@ pub enum GetComponentReflectError { /// See the documentation for [`bevy_reflect`] for more information. /// /// [`App::register_type`]: ../../../bevy_app/struct.App.html#method.register_type - #[error("No `ComponentId` corresponding to {0:?} found (did you call App::register_type()?)")] + #[display( + "No `ComponentId` corresponding to {_0:?} found (did you call App::register_type()?)" + )] + #[error(ignore)] NoCorrespondingComponentId(TypeId), /// The given [`Entity`] does not have a [`Component`] corresponding to the given [`TypeId`]. - #[error("The given `Entity` {entity:?} does not have a `{component_name:?}` component ({component_id:?}, which corresponds to {type_id:?})")] + #[display("The given `Entity` {entity:?} does not have a `{component_name:?}` component ({component_id:?}, which corresponds to {type_id:?})")] EntityDoesNotHaveComponent { /// The given [`Entity`]. entity: Entity, @@ -226,7 +229,7 @@ pub enum GetComponentReflectError { }, /// The [`World`] was missing the [`AppTypeRegistry`] resource. - #[error("The `World` was missing the `AppTypeRegistry` resource")] + #[display("The `World` was missing the `AppTypeRegistry` resource")] MissingAppTypeRegistry, /// The [`World`]'s [`TypeRegistry`] did not contain [`TypeData`] for [`ReflectFromPtr`] for the given [`TypeId`]. @@ -240,7 +243,8 @@ pub enum GetComponentReflectError { /// [`TypeRegistry`]: bevy_reflect::TypeRegistry /// [`ReflectFromPtr`]: bevy_reflect::ReflectFromPtr /// [`App::register_type`]: ../../../bevy_app/struct.App.html#method.register_type - #[error("The `World`'s `TypeRegistry` did not contain `TypeData` for `ReflectFromPtr` for the given {0:?} (did you call `App::register_type()`?)")] + #[display("The `World`'s `TypeRegistry` did not contain `TypeData` for `ReflectFromPtr` for the given {_0:?} (did you call `App::register_type()`?)")] + #[error(ignore)] MissingReflectFromPtrTypeData(TypeId), }