-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Make AssetLoader/Saver Error type bounds compatible with anyhow::Error #10493
Conversation
Code below compiles so I assume it is compatible with pretty much all error types. It would be nice if we could get this change into Bevy 0.12.1 to make migration from 0.11 easier. trait MyTrait {
type Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>;
}
#[derive(Error, Debug)]
pub enum ThisErrorError {
#[error("Foo")]
_Foo,
}
#[derive(Debug)]
struct CustomError;
impl std::error::Error for CustomError {}
impl std::fmt::Display for CustomError {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
struct MyStructWithAnyhowError;
impl MyTrait for MyStructWithAnyhowError {
type Error = anyhow::Error;
}
struct MyStructWithThiserrorError;
impl MyTrait for MyStructWithThiserrorError {
type Error = ThisErrorError;
}
struct MyStructWithCustomError;
impl MyTrait for MyStructWithCustomError {
type Error = CustomError;
} |
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 bevyengine#10350
6f4d569
to
bee7d1a
Compare
I'm not sure I understand this comment:
What are the implications for this PR? to reviewers: the relevant std trait impl is: https://doc.rust-lang.org/stable/std/convert/trait.From.html#impl-From%3CE%3E-for-Box%3Cdyn+Error+%2B+Sync+%2B+Send+%2B+'a,+Global%3E |
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.
This makes sense to me. Would this break compatibility with people using type Error = Box<dyn Error + Send + Sync>
as error type?
I wrote this comment before preparing this PR and it is not directly related. It was about workaround proposed in #10350 that does not work (it did not involve
AFAIK there are no such people because |
Yeah then I think we can include it in 0.12.1. |
bevyengine#10493) # Objective * In Bevy 0.11 asset loaders used `anyhow::Error` for returning errors. In Bevy 0.12 `AssetLoader` (and `AssetSaver`) have associated `Error` type. Unfortunately it's type bounds does not allow `anyhow::Error` to be used despite migration guide claiming otherwise. This makes migration to 0.12 more challenging. Solve this by changing type bounds for associated `Error` type. * Fix bevyengine#10350 ## Solution Change associated `Error` type bounds to require `Into<Box<dyn std::error::Error + Send + Sync + 'static>>` to be implemented instead of `std::error::Error + Send + Sync + 'static`. Both `anyhow::Error` and errors generated by `thiserror` seems to be fine with such type bound. --- ## Changelog ### Fixed * Fixed compatibility with `anyhow::Error` in `AssetLoader` and `AssetSaver` associated `Error` type
#10493) # Objective * In Bevy 0.11 asset loaders used `anyhow::Error` for returning errors. In Bevy 0.12 `AssetLoader` (and `AssetSaver`) have associated `Error` type. Unfortunately it's type bounds does not allow `anyhow::Error` to be used despite migration guide claiming otherwise. This makes migration to 0.12 more challenging. Solve this by changing type bounds for associated `Error` type. * Fix #10350 ## Solution Change associated `Error` type bounds to require `Into<Box<dyn std::error::Error + Send + Sync + 'static>>` to be implemented instead of `std::error::Error + Send + Sync + 'static`. Both `anyhow::Error` and errors generated by `thiserror` seems to be fine with such type bound. --- ## Changelog ### Fixed * Fixed compatibility with `anyhow::Error` in `AssetLoader` and `AssetSaver` associated `Error` type
bevyengine#10493) # Objective * In Bevy 0.11 asset loaders used `anyhow::Error` for returning errors. In Bevy 0.12 `AssetLoader` (and `AssetSaver`) have associated `Error` type. Unfortunately it's type bounds does not allow `anyhow::Error` to be used despite migration guide claiming otherwise. This makes migration to 0.12 more challenging. Solve this by changing type bounds for associated `Error` type. * Fix bevyengine#10350 ## Solution Change associated `Error` type bounds to require `Into<Box<dyn std::error::Error + Send + Sync + 'static>>` to be implemented instead of `std::error::Error + Send + Sync + 'static`. Both `anyhow::Error` and errors generated by `thiserror` seems to be fine with such type bound. --- ## Changelog ### Fixed * Fixed compatibility with `anyhow::Error` in `AssetLoader` and `AssetSaver` associated `Error` type
Objective
anyhow::Error
for returning errors. In Bevy 0.12AssetLoader
(andAssetSaver
) have associatedError
type. Unfortunately it's type bounds does not allowanyhow::Error
to be used despite migration guide claiming otherwise. This makes migration to 0.12 more challenging. Solve this by changing type bounds for associatedError
type.anyhow
when writing customAssetLoader
s orAssetSaver
s #10350Solution
Change associated
Error
type bounds to requireInto<Box<dyn std::error::Error + Send + Sync + 'static>>
to be implemented instead ofstd::error::Error + Send + Sync + 'static
. Bothanyhow::Error
and errors generated bythiserror
seems to be fine with such type bound.Changelog
Fixed
anyhow::Error
inAssetLoader
andAssetSaver
associatedError
type