Skip to content

Commit

Permalink
Remove thiserror from bevy_asset (#15778)
Browse files Browse the repository at this point in the history
# Objective

- Contributes to #15460

## Solution

- Removed `thiserror` from `bevy_asset`
  • Loading branch information
bushrat011899 authored Oct 9, 2024
1 parent ecd04c1 commit 35edb25
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 179 deletions.
6 changes: 5 additions & 1 deletion crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ blake3 = "1.5"
parking_lot = { version = "0.12", features = ["arc_lock", "send_guard"] }
ron = "0.8"
serde = { version = "1", features = ["derive"] }
thiserror = "1.0"
derive_more = { version = "1", default-features = false, features = [
"error",
"from",
"display",
] }
uuid = { version = "1.0", features = ["v4"] }

[target.'cfg(target_os = "android")'.dependencies]
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use bevy_reflect::{Reflect, TypePath};
use bevy_utils::HashMap;
use core::{any::TypeId, iter::Enumerate, marker::PhantomData, sync::atomic::AtomicU32};
use crossbeam_channel::{Receiver, Sender};
use derive_more::derive::{Display, Error};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

/// A generational runtime-only identifier for a specific [`Asset`] stored in [`Assets`]. This is optimized for efficient runtime
Expand Down Expand Up @@ -613,8 +613,8 @@ impl<'a, A: Asset> Iterator for AssetsMutIterator<'a, A> {
}
}

#[derive(Error, Debug)]
#[error("AssetIndex {index:?} has an invalid generation. The current generation is: '{current_generation}'.")]
#[derive(Error, Display, Debug)]
#[display("AssetIndex {index:?} has an invalid generation. The current generation is: '{current_generation}'.")]
pub struct InvalidGenerationError {
index: AssetIndex,
current_generation: u32,
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use core::{
hash::{Hash, Hasher},
};
use crossbeam_channel::{Receiver, Sender};
use derive_more::derive::{Display, Error};
use disqualified::ShortName;
use thiserror::Error;
use uuid::Uuid;

/// Provides [`Handle`] and [`UntypedHandle`] _for a specific asset type_.
Expand Down Expand Up @@ -503,11 +503,11 @@ impl<A: Asset> TryFrom<UntypedHandle> for Handle<A> {
}

/// Errors preventing the conversion of to/from an [`UntypedHandle`] and a [`Handle`].
#[derive(Error, Debug, PartialEq, Clone)]
#[derive(Error, Display, Debug, PartialEq, Clone)]
#[non_exhaustive]
pub enum UntypedAssetConversionError {
/// Caused when trying to convert an [`UntypedHandle`] into a [`Handle`] of the wrong type.
#[error(
#[display(
"This UntypedHandle is for {found:?} and cannot be converted into a Handle<{expected:?}>"
)]
TypeIdMismatch { expected: TypeId, found: TypeId },
Expand Down
29 changes: 5 additions & 24 deletions crates/bevy_asset/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use core::{
hash::Hash,
marker::PhantomData,
};
use thiserror::Error;
use derive_more::derive::{Display, Error, From};

/// A unique runtime-only identifier for an [`Asset`]. This is cheap to [`Copy`]/[`Clone`] and is not directly tied to the
/// lifetime of the Asset. This means it _can_ point to an [`Asset`] that no longer exists.
///
/// For an identifier tied to the lifetime of an asset, see [`Handle`](`crate::Handle`).
///
/// For an "untyped" / "generic-less" id, see [`UntypedAssetId`].
#[derive(Reflect, Serialize, Deserialize)]
#[derive(Reflect, Serialize, Deserialize, From)]
pub enum AssetId<A: Asset> {
/// A small / efficient runtime identifier that can be used to efficiently look up an asset stored in [`Assets`]. This is
/// the "default" identifier used for assets. The alternative(s) (ex: [`AssetId::Uuid`]) will only be used if assets are
Expand Down Expand Up @@ -154,13 +154,6 @@ impl<A: Asset> From<AssetIndex> for AssetId<A> {
}
}

impl<A: Asset> From<Uuid> for AssetId<A> {
#[inline]
fn from(value: Uuid) -> Self {
Self::Uuid { uuid: value }
}
}

/// An "untyped" / "generic-less" [`Asset`] identifier that behaves much like [`AssetId`], but stores the [`Asset`] type
/// information at runtime instead of compile-time. This increases the size of the type, but it enables storing asset ids
/// across asset types together and enables comparisons between them.
Expand Down Expand Up @@ -310,7 +303,7 @@ impl PartialOrd for UntypedAssetId {
/// Do not _ever_ use this across asset types for comparison.
/// [`InternalAssetId`] contains no type information and will happily collide
/// with indices across types.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, From)]
pub(crate) enum InternalAssetId {
Index(AssetIndex),
Uuid(Uuid),
Expand All @@ -337,18 +330,6 @@ impl InternalAssetId {
}
}

impl From<AssetIndex> for InternalAssetId {
fn from(value: AssetIndex) -> Self {
Self::Index(value)
}
}

impl From<Uuid> for InternalAssetId {
fn from(value: Uuid) -> Self {
Self::Uuid(value)
}
}

// Cross Operations

impl<A: Asset> PartialEq<UntypedAssetId> for AssetId<A> {
Expand Down Expand Up @@ -417,11 +398,11 @@ impl<A: Asset> TryFrom<UntypedAssetId> for AssetId<A> {
}

/// Errors preventing the conversion of to/from an [`UntypedAssetId`] and an [`AssetId`].
#[derive(Error, Debug, PartialEq, Clone)]
#[derive(Error, Display, Debug, PartialEq, Clone)]
#[non_exhaustive]
pub enum UntypedAssetIdConversionError {
/// Caused when trying to convert an [`UntypedAssetId`] into an [`AssetId`] of the wrong type.
#[error("This UntypedAssetId is for {found:?} and cannot be converted into an AssetId<{expected:?}>")]
#[display("This UntypedAssetId is for {found:?} and cannot be converted into an AssetId<{expected:?}>")]
TypeIdMismatch { expected: TypeId, found: TypeId },
}

Expand Down
18 changes: 10 additions & 8 deletions crates/bevy_asset/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ use core::{
pin::Pin,
task::{Context, Poll},
};
use derive_more::derive::{Display, Error, From};
use futures_io::{AsyncRead, AsyncWrite};
use futures_lite::{ready, Stream};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Errors that occur while loading assets.
#[derive(Error, Debug, Clone)]
#[derive(Error, Display, Debug, Clone)]
pub enum AssetReaderError {
/// Path not found.
#[error("Path not found: {0}")]
#[display("Path not found: {}", _0.display())]
#[error(ignore)]
NotFound(PathBuf),

/// Encountered an I/O error while loading an asset.
#[error("Encountered an I/O error while loading asset: {0}")]
#[display("Encountered an I/O error while loading asset: {_0}")]
Io(Arc<std::io::Error>),

/// The HTTP request completed but returned an unhandled [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).
/// If the request fails before getting a status code (e.g. request timeout, interrupted connection, etc), expect [`AssetReaderError::Io`].
#[error("Encountered HTTP status {0:?} when loading asset")]
#[display("Encountered HTTP status {_0:?} when loading asset")]
#[error(ignore)]
HttpError(u16),
}

Expand Down Expand Up @@ -296,11 +298,11 @@ pub type Writer = dyn AsyncWrite + Unpin + Send + Sync;
pub type PathStream = dyn Stream<Item = PathBuf> + Unpin + Send;

/// Errors that occur while loading assets.
#[derive(Error, Debug)]
#[derive(Error, Display, Debug, From)]
pub enum AssetWriterError {
/// Encountered an I/O error while loading an asset.
#[error("encountered an io error while loading asset: {0}")]
Io(#[from] std::io::Error),
#[display("encountered an io error while loading asset: {_0}")]
Io(std::io::Error),
}

/// Preforms write operations on an asset storage. [`AssetWriter`] exposes a "virtual filesystem"
Expand Down
22 changes: 13 additions & 9 deletions crates/bevy_asset/src/io/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_utils::{
Duration, HashMap,
};
use core::{fmt::Display, hash::Hash};
use thiserror::Error;
use derive_more::derive::{Display, Error};

use super::{ErasedAssetReader, ErasedAssetWriter};

Expand Down Expand Up @@ -629,23 +629,27 @@ impl AssetSources {
}

/// An error returned when an [`AssetSource`] does not exist for a given id.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[error("Asset Source '{0}' does not exist")]
#[derive(Error, Display, Debug, Clone, PartialEq, Eq)]
#[display("Asset Source '{_0}' does not exist")]
#[error(ignore)]
pub struct MissingAssetSourceError(AssetSourceId<'static>);

/// An error returned when an [`AssetWriter`](crate::io::AssetWriter) does not exist for a given id.
#[derive(Error, Debug, Clone)]
#[error("Asset Source '{0}' does not have an AssetWriter.")]
#[derive(Error, Display, Debug, Clone)]
#[display("Asset Source '{_0}' does not have an AssetWriter.")]
#[error(ignore)]
pub struct MissingAssetWriterError(AssetSourceId<'static>);

/// An error returned when a processed [`AssetReader`](crate::io::AssetReader) does not exist for a given id.
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[error("Asset Source '{0}' does not have a processed AssetReader.")]
#[derive(Error, Display, Debug, Clone, PartialEq, Eq)]
#[display("Asset Source '{_0}' does not have a processed AssetReader.")]
#[error(ignore)]
pub struct MissingProcessedAssetReaderError(AssetSourceId<'static>);

/// An error returned when a processed [`AssetWriter`](crate::io::AssetWriter) does not exist for a given id.
#[derive(Error, Debug, Clone)]
#[error("Asset Source '{0}' does not have a processed AssetWriter.")]
#[derive(Error, Display, Debug, Clone)]
#[display("Asset Source '{_0}' does not have a processed AssetWriter.")]
#[error(ignore)]
pub struct MissingProcessedAssetWriterError(AssetSourceId<'static>);

const MISSING_DEFAULT_SOURCE: &str =
Expand Down
14 changes: 7 additions & 7 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,9 @@ mod tests {
use bevy_log::LogPlugin;
use bevy_reflect::TypePath;
use bevy_utils::{Duration, HashMap};
use derive_more::derive::{Display, Error, From};
use serde::{Deserialize, Serialize};
use std::path::Path;
use thiserror::Error;

#[derive(Asset, TypePath, Debug, Default)]
pub struct CoolText {
Expand Down Expand Up @@ -661,14 +661,14 @@ mod tests {
#[derive(Default)]
pub struct CoolTextLoader;

#[derive(Error, Debug)]
#[derive(Error, Display, Debug, From)]
pub enum CoolTextLoaderError {
#[error("Could not load dependency: {dependency}")]
#[display("Could not load dependency: {dependency}")]
CannotLoadDependency { dependency: AssetPath<'static> },
#[error("A RON error occurred during loading")]
RonSpannedError(#[from] ron::error::SpannedError),
#[error("An IO error occurred during loading")]
Io(#[from] std::io::Error),
#[display("A RON error occurred during loading")]
RonSpannedError(ron::error::SpannedError),
#[display("An IO error occurred during loading")]
Io(std::io::Error),
}

impl AssetLoader for CoolTextLoader {
Expand Down
34 changes: 15 additions & 19 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use atomicow::CowArc;
use bevy_ecs::world::World;
use bevy_utils::{BoxedFuture, ConditionalSendFuture, HashMap, HashSet};
use core::any::{Any, TypeId};
use derive_more::derive::{Display, Error, From};
use downcast_rs::{impl_downcast, Downcast};
use ron::error::SpannedError;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Loads an [`Asset`] from a given byte [`Reader`]. This can accept [`AssetLoader::Settings`], which configure how the [`Asset`]
/// should be loaded.
Expand Down Expand Up @@ -295,19 +295,20 @@ impl<A: Asset> AssetContainer for A {
///
/// [`NestedLoader::load`]: crate::NestedLoader::load
/// [immediately]: crate::Immediate
#[derive(Error, Debug)]
#[error("Failed to load dependency {dependency:?} {error}")]
#[derive(Error, Display, Debug)]
#[display("Failed to load dependency {dependency:?} {error}")]
pub struct LoadDirectError {
pub dependency: AssetPath<'static>,
pub error: AssetLoadError,
}

/// An error that occurs while deserializing [`AssetMeta`].
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[derive(Error, Display, Debug, Clone, PartialEq, Eq, From)]
pub enum DeserializeMetaError {
#[error("Failed to deserialize asset meta: {0:?}")]
DeserializeSettings(#[from] SpannedError),
#[error("Failed to deserialize minimal asset meta: {0:?}")]
#[display("Failed to deserialize asset meta: {_0:?}")]
DeserializeSettings(SpannedError),
#[display("Failed to deserialize minimal asset meta: {_0:?}")]
#[from(ignore)]
DeserializeMinimal(SpannedError),
}

Expand Down Expand Up @@ -572,23 +573,18 @@ impl<'a> LoadContext<'a> {
}

/// An error produced when calling [`LoadContext::read_asset_bytes`]
#[derive(Error, Debug)]
#[derive(Error, Display, Debug, From)]
pub enum ReadAssetBytesError {
#[error(transparent)]
DeserializeMetaError(#[from] DeserializeMetaError),
#[error(transparent)]
AssetReaderError(#[from] AssetReaderError),
#[error(transparent)]
MissingAssetSourceError(#[from] MissingAssetSourceError),
#[error(transparent)]
MissingProcessedAssetReaderError(#[from] MissingProcessedAssetReaderError),
DeserializeMetaError(DeserializeMetaError),
AssetReaderError(AssetReaderError),
MissingAssetSourceError(MissingAssetSourceError),
MissingProcessedAssetReaderError(MissingProcessedAssetReaderError),
/// Encountered an I/O error while loading an asset.
#[error("Encountered an io error while loading asset at `{path}`: {source}")]
#[display("Encountered an io error while loading asset at `{}`: {source}", path.display())]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("The LoadContext for this read_asset_bytes call requires hash metadata, but it was not provided. This is likely an internal implementation error.")]
#[display("The LoadContext for this read_asset_bytes call requires hash metadata, but it was not provided. This is likely an internal implementation error.")]
MissingAssetHash,
}
12 changes: 6 additions & 6 deletions crates/bevy_asset/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use core::{
hash::Hash,
ops::Deref,
};
use derive_more::derive::{Display, Error};
use serde::{de::Visitor, Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Represents a path to an asset in a "virtual filesystem".
///
Expand Down Expand Up @@ -76,19 +76,19 @@ impl<'a> Display for AssetPath<'a> {
}

/// An error that occurs when parsing a string type to create an [`AssetPath`] fails, such as during [`AssetPath::parse`].
#[derive(Error, Debug, PartialEq, Eq)]
#[derive(Error, Display, Debug, PartialEq, Eq)]
pub enum ParseAssetPathError {
/// Error that occurs when the [`AssetPath::source`] section of a path string contains the [`AssetPath::label`] delimiter `#`. E.g. `bad#source://file.test`.
#[error("Asset source must not contain a `#` character")]
#[display("Asset source must not contain a `#` character")]
InvalidSourceSyntax,
/// Error that occurs when the [`AssetPath::label`] section of a path string contains the [`AssetPath::source`] delimiter `://`. E.g. `source://file.test#bad://label`.
#[error("Asset label must not contain a `://` substring")]
#[display("Asset label must not contain a `://` substring")]
InvalidLabelSyntax,
/// Error that occurs when a path string has an [`AssetPath::source`] delimiter `://` with no characters preceding it. E.g. `://file.test`.
#[error("Asset source must be at least one character. Either specify the source before the '://' or remove the `://`")]
#[display("Asset source must be at least one character. Either specify the source before the '://' or remove the `://`")]
MissingSource,
/// Error that occurs when a path string has an [`AssetPath::label`] delimiter `#` with no characters succeeding it. E.g. `file.test#`
#[error("Asset label must be at least one character. Either specify the label after the '#' or remove the '#'")]
#[display("Asset label must be at least one character. Either specify the label after the '#' or remove the '#'")]
MissingLabel,
}

Expand Down
Loading

0 comments on commit 35edb25

Please sign in to comment.