-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Consider relaxing the Sized
requirement in impl Error for Box<T>
#104485
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
Comments
@rustbot label +T-libs-api |
I may be wrong, but I think this is a known issue that can't be fixed because it would break backwards compatibility. But if I'm wrong will other people please chime in. (It's much quicker to get an answer on the internet by posting something incorrect than by asking a question 😛) |
@derekdreery I came across this comment while working on #113464 which supports your backwards compatibility claim: https://github.com/rust-lang/rust/blob/master/library/core/src/error.rs#L409
I think I just realized the issue here -- it's the downstream usages of Box (ie contexts receiving and using Box such as an error-reporting library like anyhow that rely on it having a size known at compile time) that potentially rely on the Sized bound which would be broken if it's removed, not the Box itself. |
I believe this is because It might still be acceptable to relax the bounds though, if a crater run shows no breakage. |
Okay I just tried this out:
But when I try compiling I get
So Is this happening because of monomorphization? At compile time because one possible known implementation of
And this would also be monomorphed from Is there a good way to work around this? Add a |
This is also a thing i am currently stumbling over. This issue essentially forces you to implement your own type as the concrete type. When trying to specify a trait type with bounds
to
Which is enough in most cases, but some libraries like Snafu require you to implement the Error trait on your source types. Which doesn't match well for a library, trying to generically give users the ability to specify handler error types, that shouldn't have to satisfy this requirement. It could be fixed downstream, but I don't think this is a downstream problem. It literally takes away the ability to use Box in development for generic contexts under these conditions. |
@Veetaha thanks, and I'm somewhat aware of this but I feel like this shouldn't have to be worked around. |
Uh oh!
There was an error while loading. Please reload this page.
As for today the following blanket implementation exists:
rust/library/alloc/src/boxed.rs
Lines 2443 to 2458 in e702534
The problem with this is that
T
is required to beSized
, so, for example,Box<dyn Error>
doesn't implementError
. It looks like a straightforward and intuitive assumption thatBox<dyn Error>
should implementError
trait, but it doesn't.Going even further, there is an inconsistency between the blanket impls for
Arc
as well, because there exists an analogous impl forArc
that has relaxedSized?
requirement:rust/library/alloc/src/sync.rs
Lines 2769 to 2788 in e702534
Use Case
The use case where I stumbled with this problem is where I wanted to put
Box<dyn Error>
as asource
error inthiserror
error enum, but I couldn't because that type doesn't implement Error trait.Workarounds
The workaround for this problem is to use
Arc<T>
instead ofBox<T>
when an Error impl is required forSized?
type. It's also possible to convertBox<dyn Error>
into anArc<dyn Error>
via thisFrom
impl.The text was updated successfully, but these errors were encountered: