Skip to content

Commit

Permalink
Make AssetLoader/Saver Error type bounds compatible with anyhow
Browse files Browse the repository at this point in the history
anyhow::Error does not implement std::error::Error but converting to boxed
std::error::Error is possible. At the same time this bound should work fine
with custom error types like ones generated by thiserror.
Note: after this change Bevy 0.12 migration guide claim that anyhow::Error
works in this context becomes true.
Fixes #10350
  • Loading branch information
rafalh committed Nov 10, 2023
1 parent cbadc31 commit bee7d1a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 4 additions & 2 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait AssetLoader: Send + Sync + 'static {
/// The settings type used by this [`AssetLoader`].
type Settings: Settings + Default + Serialize + for<'a> Deserialize<'a>;
/// The type of [error](`std::error::Error`) which could be encountered by this loader.
type Error: std::error::Error + Send + Sync + 'static;
type Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>;
/// Asynchronously loads [`AssetLoader::Asset`] (and any other labeled assets) from the bytes provided by [`Reader`].
fn load<'a>(
&'a self,
Expand Down Expand Up @@ -90,7 +90,9 @@ where
.expect("Loader settings should exist")
.downcast_ref::<L::Settings>()
.expect("AssetLoader settings should match the loader type");
let asset = <L as AssetLoader>::load(self, reader, settings, &mut load_context).await?;
let asset = <L as AssetLoader>::load(self, reader, settings, &mut load_context)
.await
.map_err(|error| error.into())?;
Ok(load_context.finish(asset, Some(meta)).into())
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/processor/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<Loader: AssetLoader, Saver: AssetSaver<Asset = Loader::Asset>> Process
.saver
.save(writer, saved_asset, &settings.saver_settings)
.await
.map_err(|error| ProcessError::AssetSaveError(Box::new(error)))?;
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
Ok(output_settings)
})
}
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_asset/src/saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait AssetSaver: Send + Sync + 'static {
/// The type of [`AssetLoader`] used to load this [`Asset`]
type OutputLoader: AssetLoader;
/// The type of [error](`std::error::Error`) which could be encountered by this saver.
type Error: std::error::Error + Send + Sync + 'static;
type Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>;

/// Saves the given runtime [`Asset`] by writing it to a byte format using `writer`. The passed in `settings` can influence how the
/// `asset` is saved.
Expand Down Expand Up @@ -53,7 +53,9 @@ impl<S: AssetSaver> ErasedAssetSaver for S {
.downcast_ref::<S::Settings>()
.expect("AssetLoader settings should match the loader type");
let saved_asset = SavedAsset::<S::Asset>::from_loaded(asset).unwrap();
self.save(writer, saved_asset, settings).await?;
if let Err(err) = self.save(writer, saved_asset, settings).await {
return Err(err.into());
}
Ok(())
})
}
Expand Down

0 comments on commit bee7d1a

Please sign in to comment.