Skip to content

Commit 91ebe06

Browse files
authored
add core::panic::abort_unwind
1 parent 34e22e8 commit 91ebe06

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

core/src/panic.rs

+30
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ pub macro unreachable_2021 {
140140
),
141141
}
142142

143+
/// Invokes a closure, aborting if the closure unwinds.
144+
///
145+
/// When compiled with aborting panics, this function is effectively a no-op.
146+
/// With unwinding panics, an unwind results in another call into the panic
147+
/// hook followed by a process abort.
148+
///
149+
/// # Notes
150+
///
151+
/// Instead of using this function, code should attempt to support unwinding.
152+
/// Implementing [`Drop`] allows you to restore invariants uniformly in both
153+
/// return and unwind paths.
154+
///
155+
/// If an unwind can lead to logical issues but not soundness issues, you
156+
/// should allow the unwind. Opting out of [`UnwindSafe`] indicates to your
157+
/// consumers that they need to consider correctness in the face of unwinds.
158+
///
159+
/// If an unwind would be unsound, then this function should be used in order
160+
/// to prevent unwinds. However, note that `extern "C" fn` will automatically
161+
/// convert unwinds to aborts, so using this function isn't necessary for FFI.
162+
#[unstable(feature = "abort_unwind", issue = "130338")]
163+
pub fn abort_unwind<F: FnOnce() -> R, R>(f: F) -> R {
164+
// This attribute adds the "unwinding out of nounwind function" guard.
165+
#[rustc_nounwind]
166+
fn abort_unwind_inner<F: FnOnce() -> R, R>(f: F) -> R {
167+
f()
168+
}
169+
170+
abort_unwind_inner(f)
171+
}
172+
143173
/// An internal trait used by std to pass data from std to `panic_unwind` and
144174
/// other panic runtimes. Not intended to be stabilized any time soon, do not
145175
/// use.

0 commit comments

Comments
 (0)