poison-guard 0.1.0

Utilities for maintaining sane state in the presence of panics and failures.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented2 out of 2 items with examples
  • Size
  • Source code size: 79.71 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.24 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • KodrAus

poison-guard

Utilities for maintaining sane state in the presence of panics and failures. It's a bit like the poison and with_drop crates.

What is poisoning?

Poisoning is a general strategy for keeping state consistent by blocking direct access to state if a previous user did something unexpected with it.

Use cases

  • You have some external resource, like a File, that might become corrupted, and you want to know about it.
  • You're sharing some state across threads, and you want any panics that happen while sharing to propagate.
  • You're using a non-poisoning container (like once_cell::sync::Lazy or parking_lot::Mutex) and want to add poisoning to them.

Getting started

Add poison-guard to your Cargo.toml:

[dependencies.poison-guard]
version = "0.1.0"

Then wrap your state in a Poison<T>:

use poison_guard::Poison;

pub struct MyData {
    state: Poison<MyState>,
}

When you want to access your state, you can acquire a poison guard:

let mut guard = Poison::on_unwind(&mut my_data.state).unwrap();

do_something_with_state(&mut guard);

If a panic unwinds through a poison guard it'll panic the value, blocking future callers from accessing it. Poisoned values can be recovered, or the original failure can be propagated to those future callers.

For more details, see the documentation.