#panic #unwind #scope-guard #defer

no-std scopeguard

A RAII scope guard that will run a given closure when it goes out of scope, even if the code between panics (assuming unwinding panic). Defines the macros defer!, defer_on_unwind!, defer_on_success! as shorthands for guards with one of the implemented strategies

9 releases (1 stable)

1.0.0 Dec 19, 2018
0.3.3 Oct 13, 2017
0.3.2 Jan 21, 2017
0.3.1 Dec 11, 2016
0.1.0 Apr 30, 2015

#8 in Rust patterns

Download history 73502/week @ 2019-03-24 83321/week @ 2019-03-31 86085/week @ 2019-04-07 78787/week @ 2019-04-14 79655/week @ 2019-04-21 77747/week @ 2019-04-28 82701/week @ 2019-05-05 90191/week @ 2019-05-12 97226/week @ 2019-05-19 103975/week @ 2019-05-26 115284/week @ 2019-06-02 120023/week @ 2019-06-09 117163/week @ 2019-06-16 121816/week @ 2019-06-23 110787/week @ 2019-06-30

363,906 downloads per month
Used in 3,843 crates (31 directly)

MIT/Apache

21KB
245 lines

scopeguard

Rust crate for a convenient RAII scope guard that will run a given closure when it goes out of scope, even if the code between panics (assuming unwinding panic).

The defer! macro and guard are no_std compatible (require only core), but the on unwinding / not on uwinding strategies requires linking to std.

Requires Rust 1.20.

Please read the API documentation here

build_status crates

How to use

Recent Changes

  • 1.0.0
    • Change the closure type from FnMut(&mut T) to FnOnce(T): Passing the inner value by value instead of a mutable reference is a breaking change, but allows the guard closure to consume it. (by @tormol)
    • Add defer_on_success!{}, guard_on_success() and OnSuccess strategy, which triggers when scope is exited without panic. It's the opposite to OnUnwind / guard_on_unwind() / defer_on_unwind!{}.
    • Add ScopeGuard::into_inner(), which "defuses" the guard and returns the guarded value. (by @tormol)
    • Implement Sync for guards with non-Sync closures.
    • Require Rust 1.20
  • 0.3.3
    • Use #[inline] on a few more functions by @stjepang (#14)
    • Add examples to crate documentation
  • 0.3.2
    • Add crate categories
  • 0.3.1
    • Add defer_on_unwind!, Strategy trait
    • Rename GuardScopeGuard
    • Add ScopeGuard::with_strategy.
    • ScopeGuard now implements Debug.
    • Require Rust 1.11
  • 0.2.0
    • Require Rust 1.6
    • Use no_std unconditionally
    • No other changes
  • 0.1.2
    • Add macro defer!()

No runtime deps