Stream: project-error-handling

Topic: Error downcast design


view this post on Zulip jam1garner (Mar 28 2021 at 17:06):

hopefully quick question to clear up some confusion for me: is there a specific reason Error::downcast is implemented by casting Box<Error> -> raw ptr -> Box<T>? I was implementing a similar error type for a crate and we were able to achieve the same thing using a blanket implementation for upcasting to Box<Any> then downcasting + unwrapping after the Any::is typecheck. curious because I'm worried I'm missing some downside that std::error::Error wanted to avoid

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:07):

I think it's because the downcast functionality on Error is a reimplementation of Any

view this post on Zulip jam1garner (Mar 28 2021 at 17:07):

for reference, this is effectively the change I'm describing: https://github.com/jam1garner/binrw/commit/e055ab6e73401e435c331cbe46173ddc73ab5390
and this is what such a blanket impl would look like: https://github.com/jam1garner/binrw/blob/e055ab6e73401e435c331cbe46173ddc73ab5390/binrw/src/error.rs#L19-L42

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:08):

So presumably the downcast on Any is doing the same thing Error does

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:08):

I'm guessing it was just copied

view this post on Zulip jam1garner (Mar 28 2021 at 17:09):

I see. Would a implementation like the above be desirable to keep Error in sync with Any? not that I expect much changing for either implementation...

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:10):

I doubt it, I imagine that going through Any would require extra indirection and vtables

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:10):

Not sure on that though

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:10):

This is a bit lower level than I tend to work at

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:10):

Also worth noting, with generic member access your AsAny stuff will essentially be part of the Error trait

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:11):

Because generic member access supports arbitrary up casting

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:11):

I really need to finish that RFC and get it implemented in std

view this post on Zulip Jane Lusby (Mar 28 2021 at 17:11):

Bah

view this post on Zulip jam1garner (Mar 28 2021 at 17:12):

heh. yeah I suppose most of this is just a fancy upcasting hack. thanks, good to know :)


Last updated: Jan 26 2022 at 14:46 UTC