po4yka/rust-skills / skills/rust-panic-safety
rust-panic-safety
VerifiedUse when guarding an extern "C", extern "system", or JNI entry point with catch_unwind, choosing panic = "unwind" or "abort", installing a panic hook, disposing of a panic payload, fixing an UnwindSafe bound with AssertUnwindSafe, or triaging an abort such as "panic in a function that cannot unwind". Also use when a panic can escape a foreign callback, spawned task, thread, or Drop, or when replacing unwrap or expect on an FFI-reachable path. Owns the panic policy at a C ABI boundary.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 11.5 KB |
| references/boundary-patterns.md | markdown | 16.4 KB |
| references/drop-guard.md | markdown | 2.0 KB |
| references/jni-boundary.md | markdown | 8.4 KB |
| references/panic-hook.md | markdown | 3.4 KB |
| references/panic-strategy.md | markdown | 2.2 KB |
| references/unwind-state.md | markdown | 2.3 KB |
| references/unwrap-audit.md | markdown | 10.0 KB |
references/drop-guard.md
View on GitHub# Rollback with DropGuard
`core::mem::DropGuard` is stable since Rust 1.100. Its closure consumes the
protected value when the guard drops. `DropGuard::dismiss` returns that value
without running the closure. Commit only after every fallible step succeeds.
This probe checks success, early error, and unwinding. The rollback resets a
counter; a real transaction must also restore every associated resource.
```rust,run
use std::cell::Cell;
use std::mem::DropGuard;
use std::panic::{catch_unwind, AssertUnwindSafe};
fn update(counter: &Cell<u32>, fail: bool) -> Result<(), &'static str> {
let old = counter.get();
let guard = DropGuard::new(counter, move |c| c.set(old));
guard.set(old + 1);
if fail {
return Err("update failed");
}
DropGuard::dismiss(guard);
Ok(())
}
fn main() {
let counter = Cell::new(5);
assert_eq!(update(&counter, true), Err("update failed"));
assert_eq!(counter.get(), 5);
assert_eq!(update(&counter, false), Ok(()));
assert_eq!(counter.get(), 6);
// The guard restores the counter before catch_unwind returns.
let result = catch_unwind(AssertUnwindSafe(|| {
let guard = DropGuard::new(&counter, |c| c.set(6));
guard.set(99);
panic!("rollback probe");
}));
assert!(result.is_err());
assert_eq!(counter.get(), 6);
}
```
Run this as an example with `cargo run --locked --example <name>` in an
unwinding profile. It does not prove
cleanup on abort, process exit, or `mem::forget`, which skip destructors.
Keep cleanup free of panics. A guard is not a memory-safety proof: safe code can
forget it. Use a local `Drop` type when the package MSRV is below 1.100.
The `rust-async-internals` skill, when installed, owns the cancellation case.
Poll the real future to `Pending`, drop its owner, and check rollback before
claiming cancel safety. `DropGuard` runs synchronous cleanup; a remote or async
rollback needs an explicit completion path that the caller awaits.
Source: [DropGuard](https://doc.rust-lang.org/1.100.0/core/mem/struct.DropGuard.html).
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |