3 ms·
> - Rust lets you catch panics at thread boundaries (one of the most controversial decisions in Rust; I think it was the right one, but I definitely acknowledge
by tomjakubowski 11y ago
> - Rust lets you catch panics at thread boundaries (one of the most controversial decisions in Rust; I think it was the right one, but I definitely acknowledge that it's a big tradeoff).
(Unstable) Rust lets you catch panics anywhere. You have to be able to do that for sane FFI - as you know (but perhaps other readers don't), a panic that unwinds across the Rust/C FFI boundary causes undefined behavior, so AIUI extern functions in Rust should all look like:
pub extern "C" fn foo_the_quuxes(x: i32) -> i32 {
std::catch_panic(|| do_stuff_in_rust_land_that_may_panic(x))
.unwrap_or(-1)
}
(Actually, checking several libraries off the top of my head that expose C APIs, most don't do this -- probably because std::catch_panic is relatively new.)
Let's hope developers don't abuse std::catch_panic for regular Rust code. Maybe a lint that warns about uses outside of an extern fn is worth having.
- masklinn 11y ago> (Unstable) Rust lets you catch panics anywhere. Note that you're replying to the original creator of Rust, he's most likely aware of that discussion.
- tomjakubowski 11y agoYes, I know pcwalton is and was heavily involved in Rust development. That's why I parenthetically stated that he probably already knew these things (catch_panic is actually newer than the bulk of pcwalton' work on Rust's language and standard library, so there is a small chance he doesn't know about it). It's still worth noting this point though, for the benefit of other readers.
- kibwen 11y agopcwalton has been heavily involved with Rust since long before anyone remembers, but you're confusing him with Graydon Hoare, Rust's true original author.
- masklinn 11y agoI am for some reason…
- kibwen 11y ago> Let's hope developers don't abuse std::catch_panic for > regular Rust code. This has been taken into account on multiple occasions, and the developers have introduced mechanisms to prevent people from treating `recover` as a general error-handling mechanism: https://github.com/rust-lang/rfcs/pull/1323 https://github.com/rust-lang/rfcs/pull/1323