-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Remove incorrect equality comparisons for asset load error types #14428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7b2012e
51a6f59
ca326f3
761f9e3
e61aa3f
49d65dd
7b0aa5d
af20011
c1c50e1
6b55958
12d418c
0374b2d
79ba225
33c4d81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ use info::*; | |
use loaders::*; | ||
use parking_lot::RwLock; | ||
use std::future::Future; | ||
use std::{any::Any, path::PathBuf}; | ||
use std::path::PathBuf; | ||
use std::{any::TypeId, path::Path, sync::Arc}; | ||
use thiserror::Error; | ||
|
||
|
@@ -930,7 +930,7 @@ impl AssetServer { | |
/// Returns true if the asset and all of its dependencies (recursive) have been loaded. | ||
pub fn is_loaded_with_dependencies(&self, id: impl Into<UntypedAssetId>) -> bool { | ||
let id = id.into(); | ||
self.load_state(id) == LoadState::Loaded | ||
self.load_state(id).is_loaded() | ||
&& self.recursive_dependency_load_state(id) == RecursiveDependencyLoadState::Loaded | ||
} | ||
|
||
|
@@ -1327,7 +1327,7 @@ pub(crate) enum InternalAssetEvent { | |
} | ||
|
||
/// The load state of an asset. | ||
#[derive(Component, Clone, Debug, PartialEq, Eq)] | ||
#[derive(Component, Clone, Debug)] | ||
pub enum LoadState { | ||
/// The asset has not started loading yet | ||
NotLoaded, | ||
|
@@ -1339,6 +1339,33 @@ pub enum LoadState { | |
Failed(Box<AssetLoadError>), | ||
} | ||
|
||
impl LoadState { | ||
/// Returns `true` if this instance is [`LoadState::Loading`] | ||
pub fn is_loading(&self) -> bool { | ||
matches!(self, Self::Loading) | ||
} | ||
/// Returns `true` if this instance is [`LoadState::Loaded`] | ||
pub fn is_loaded(&self) -> bool { | ||
matches!(self, Self::Loaded) | ||
} | ||
// NOTE: an `is_not_loaded` method is intentionally not included, as it may mislead some users | ||
// into thinking it is complementary to `is_loaded`. | ||
// `NotLoaded` is a very specific failure mode and in most cases it is not necessary to directly check for it. | ||
// If this is necessary the `matches!` macro can be used instead of a helper method. | ||
|
||
/// Returns `true` if the asset is loaded or in the process of being loaded. If true true, | ||
/// then the asset can be considered to be in a "normal" state: the asset either exists | ||
/// or will exist, and no errors have been encountered yet. | ||
pub fn is_loaded_or_loading(&self) -> bool { | ||
self.is_loaded() || self.is_loading() | ||
} | ||
Comment on lines
+1356
to
+1361
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a common thing yo check for? I imagine you'd generally want to handle these two cases separately, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not tied to this method, I'd be okay with removing it. I just imagined that some users would want a quick way of checking if an asset is in a valid state or not, and allowing you to call |
||
|
||
/// Returns `true` if this instance is [`LoadState::Failed`] | ||
pub fn is_failed(&self) -> bool { | ||
matches!(self, Self::Failed(_)) | ||
} | ||
} | ||
|
||
/// The load state of an asset's dependencies. | ||
#[derive(Component, Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)] | ||
pub enum DependencyLoadState { | ||
|
@@ -1366,7 +1393,7 @@ pub enum RecursiveDependencyLoadState { | |
} | ||
|
||
/// An error that occurs during an [`Asset`] load. | ||
#[derive(Error, Debug, Clone, PartialEq, Eq)] | ||
#[derive(Error, Debug, Clone)] | ||
pub enum AssetLoadError { | ||
#[error("Requested handle of type {requested:?} for asset '{path}' does not match actual asset type '{actual_asset_name}', which used loader '{loader_name}'")] | ||
RequestedHandleTypeMismatch { | ||
|
@@ -1429,18 +1456,6 @@ pub struct AssetLoaderError { | |
error: Arc<dyn std::error::Error + Send + Sync + 'static>, | ||
} | ||
|
||
impl PartialEq for AssetLoaderError { | ||
/// Equality comparison for `AssetLoaderError::error` is not full (only through `TypeId`) | ||
#[inline] | ||
fn eq(&self, other: &Self) -> bool { | ||
self.path == other.path | ||
&& self.loader_name == other.loader_name | ||
&& self.error.type_id() == other.error.type_id() | ||
} | ||
} | ||
|
||
impl Eq for AssetLoaderError {} | ||
|
||
impl AssetLoaderError { | ||
pub fn path(&self) -> &AssetPath<'static> { | ||
&self.path | ||
|
@@ -1453,16 +1468,6 @@ pub struct AddAsyncError { | |
error: Arc<dyn std::error::Error + Send + Sync + 'static>, | ||
} | ||
|
||
impl PartialEq for AddAsyncError { | ||
/// Equality comparison is not full (only through `TypeId`) | ||
#[inline] | ||
fn eq(&self, other: &Self) -> bool { | ||
self.error.type_id() == other.error.type_id() | ||
} | ||
} | ||
|
||
impl Eq for AddAsyncError {} | ||
|
||
/// An error that occurs when an [`AssetLoader`] is not registered for a given extension. | ||
#[derive(Error, Debug, Clone, PartialEq, Eq)] | ||
#[error("no `AssetLoader` found{}", format_missing_asset_ext(.extensions))] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR but it might be worth renaming the
NotLoaded
variant toIdle
or something to help clarify the distinction on the enum itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There might be a better name, but I like how
NotLoaded
avoids making any statements about the asset. If an asset handle isNotLoaded
, it could mean that it hasn't started loading yet, or it could mean that there is no asset associated with the handle and never will be, or that the handle is invalid. A name likeIdle
implies that the asset does exist in some form which isn't necessarily trueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good point, I hadn't considered that 🤔