diff options
| author | Mica White <botahamec@outlook.com> | 2026-09-13 15:12:08 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-09-13 15:12:08 -0400 |
| commit | 16d6bd7ece4c4b9b72f6bd51c9b034270b745525 (patch) | |
| tree | 4ff744fc41319ce73abe06416fabeaa7c17fbd46 /src/handle_unwind.rs | |
| parent | 9fd7b94cd7c20abe66cea875a0e83e76a7a00fbc (diff) | |
Support no_stdno-std
Diffstat (limited to 'src/handle_unwind.rs')
| -rwxr-xr-x | src/handle_unwind.rs | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/handle_unwind.rs b/src/handle_unwind.rs index 42b6fc5..e00bbbd 100755 --- a/src/handle_unwind.rs +++ b/src/handle_unwind.rs @@ -1,13 +1,31 @@ +#[cfg(feature = "std")] use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe}; -/// Runs `try_fn`. If it unwinds, it will run `catch` and then continue -/// unwinding. This is used instead of `scopeguard` to ensure the `catch` -/// function doesn't run if the thread is already panicking. The unwind -/// must specifically be caused by the `try_fn` -pub fn handle_unwind<R, F: FnOnce() -> R, G: FnOnce()>(try_fn: F, catch: G) -> R { - let try_fn = AssertUnwindSafe(try_fn); - catch_unwind(try_fn).unwrap_or_else(|e| { - catch(); - resume_unwind(e) - }) +cfg_select! { + feature = "std" => { + /// Runs `try_fn`. If it unwinds, it will run `catch` and then continue + /// unwinding. This is used instead of `scopeguard` to ensure the `catch` + /// function doesn't run if the thread is already panicking. The unwind + /// must specifically be caused by the `try_fn` + /// + /// This has no effect in `no_std` environments + pub fn handle_unwind<R, F: FnOnce() -> R, G: FnOnce()>(try_fn: F, catch: G) -> R { + let try_fn = AssertUnwindSafe(try_fn); + catch_unwind(try_fn).unwrap_or_else(|e| { + catch(); + resume_unwind(e) + }) + } + } + _ => { + /// Runs `try_fn`. If it unwinds, it will run `catch` and then continue + /// unwinding. This is used instead of `scopeguard` to ensure the `catch` + /// function doesn't run if the thread is already panicking. The unwind + /// must specifically be caused by the `try_fn` + /// + /// This has no effect in `no_std` environments + pub fn handle_unwind<R, F: FnOnce() -> R, G: FnOnce()>(try_fn: F, _catch: G) -> R { + try_fn() + } + } } |
