README.OpenSource
1[
2 {
3 "Name": "scopeguard",
4 "License": "Apache License V2.0",
5 "License File": "LICENSE-APACHE",
6 "Version Number": "1.1.0",
7 "Owner": "xuelei3@huawei.com",
8 "Upstream URL": "https://github.com/bluss/scopeguard",
9 "Description": "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."
10 }
11]
12
README.rst
1
2scopeguard
3==========
4
5Rust crate for a convenient RAII scope guard that will run a given closure when
6it goes out of scope, even if the code between panics (assuming unwinding panic).
7
8The `defer!` macro and `guard` are `no_std` compatible (require only core),
9but the on unwinding / not on uwinding strategies requires linking to `std`.
10
11Requires Rust 1.20.
12
13
14Please read the `API documentation here`__
15
16__ https://docs.rs/scopeguard/
17
18|build_status|_ |crates|_
19
20.. |build_status| image:: https://travis-ci.org/bluss/scopeguard.svg
21.. _build_status: https://travis-ci.org/bluss/scopeguard
22
23.. |crates| image:: http://meritbadge.herokuapp.com/scopeguard
24.. _crates: https://crates.io/crates/scopeguard
25
26How to use
27----------
28
29.. code:: rust
30
31 #[macro_use(defer)] extern crate scopeguard;
32
33 use scopeguard::guard;
34
35 fn f() {
36 defer!(println!("Called at return or panic"));
37 panic!();
38 }
39
40 use std::fs::File;
41 use std::io::Write;
42
43 fn g() {
44 let f = File::create("newfile.txt").unwrap();
45 let mut file = guard(f, |f| {
46 // write file at return or panic
47 let _ = f.sync_all();
48 });
49 // Access the file through the scope guard itself
50 file.write_all(b"test me\n").unwrap();
51 }
52
53Recent Changes
54--------------
55
56- 1.1.0
57
58 - Change macros (``defer!``, ``defer_on_success!`` and ``defer_on_unwind!``)
59 to accept statements. (by @konsumlamm)
60
61- 1.0.0
62
63 - Change the closure type from ``FnMut(&mut T)`` to ``FnOnce(T)``:
64 Passing the inner value by value instead of a mutable reference is a
65 breaking change, but allows the guard closure to consume it. (by @tormol)
66
67 - Add ``defer_on_success!{}``, ``guard_on_success()`` and ``OnSuccess``
68 strategy, which triggers when scope is exited *without* panic. It's the
69 opposite to ``OnUnwind`` / ``guard_on_unwind()`` / ``defer_on_unwind!{}``.
70
71 - Add ``ScopeGuard::into_inner()``, which "defuses" the guard and returns the
72 guarded value. (by @tormol)
73
74 - Implement ``Sync`` for guards with non-``Sync`` closures.
75
76 - Require Rust 1.20
77
78- 0.3.3
79
80 - Use ``#[inline]`` on a few more functions by @stjepang (#14)
81 - Add examples to crate documentation
82
83- 0.3.2
84
85 - Add crate categories
86
87- 0.3.1
88
89 - Add ``defer_on_unwind!``, ``Strategy`` trait
90 - Rename ``Guard`` → ``ScopeGuard``
91 - Add ``ScopeGuard::with_strategy``.
92 - ``ScopeGuard`` now implements ``Debug``.
93 - Require Rust 1.11
94
95- 0.2.0
96
97 - Require Rust 1.6
98 - Use `no_std` unconditionally
99 - No other changes
100
101- 0.1.2
102
103 - Add macro ``defer!()``
104