wishawa / async_ui

Lifetime-Friendly, Component-Based, Retained-Mode UI Powered by Async Rust
Mozilla Public License 2.0
551 stars 11 forks source link

Use a forgettable Drop impl to abort on panics #7

Closed Lej77 closed 1 year ago

Lej77 commented 1 year ago

Its probably better to use a forgettable zero sized type with a custom Drop implementation to abort on panic instead of using std::panic::catch_unwind.

I think I first saw this pattern described at dtolnay/case-studies/function-epilogue.

This pattern works on no_std and is also more future proof in case Rust ever supports unwinds that can't be caught (maybe from c++?) as this pattern should abort on those unwinds as well.

wishawa commented 1 year ago

I like this trick. One question: is the panic on unwind = abort behavior guaranteed? I read about it somewhere but can't remember if it was official documentation or not.

Lej77 commented 1 year ago

I am not sure actually! That is why I added that nested let _bomb = AbortBomb; statement, to cause infinite recursion if it somehow doesn't lead to an abort. Still I can't think of any reason they would change that. From what I understand there has been talk about making all panics in Drop implementations lead to abort. So it seems likely that if there was a change it would be to more aggressively abort.

If you want to be safer and you are using std anyways, then just call the std::process::abort function inside the AbortBomb's Drop implementation.

Edit: "double panic" seems to be mentioned in RFC 1328.

wishawa commented 1 year ago

Awesome. Since it's abort or infinite recursion anyway I don't think we need to use std::process::abort.