commit | 4ab9e3575f0eeabf632d167e9385852d171dff2a | [log] [tgz] |
---|---|---|
author | Xin Li <[email protected]> | Mon Apr 29 22:16:23 2024 +0000 |
committer | Automerger Merge Worker <[email protected]> | Mon Apr 29 22:16:23 2024 +0000 |
tree | 5d36c4198f67562c7c61c96f1a4da7cb52e71d9a | |
parent | 5531a9ec358db2e4ccea8df2131b184ccfc2a95d [diff] | |
parent | 842b9ea0fdd1b94497a5a06889bbaa0cc7c82de1 [diff] |
[automerger skipped] Empty merge of Android 24Q2 Release (ab/11526283) to aosp-main-future am: 842b9ea0fd -s ours am skip reason: Merged-In I9e1d3ce9f41a316d287e6313807225608e453147 with SHA-1 3fa0e2d43f is already in history Original change: https://googleplex-android-review.googlesource.com/c/platform/external/rust/crates/scopeguard/+/27143966 Change-Id: Ide6957c43dede278474e3ea79a5a3700085ec8e0 Signed-off-by: Automerger Merge Worker <[email protected]>
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 unwinding strategies require linking to std
. By default, the use_std
crate feature is enabled. Disable the default features for no_std
support.
Please read the API documentation here.
Minimum supported Rust version: 1.20
#[macro_use(defer)] extern crate scopeguard; use scopeguard::guard; fn f() { defer! { println!("Called at return or panic"); } panic!(); } use std::fs::File; use std::io::Write; fn g() { let f = File::create("newfile.txt").unwrap(); let mut file = guard(f, |f| { // write file at return or panic let _ = f.sync_all(); }); // access the file through the scope guard itself file.write_all(b"test me\n").unwrap(); }
1.2.0
1.1.0
defer!
, defer_on_success!
and defer_on_unwind!
) to accept statements. (by @konsumlamm)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 defer_on_unwind!
/ guard_on_unwind()
/ OnUnwind
.
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
#[inline]
on a few more functions by @stjepang (#14)0.3.2
0.3.1
defer_on_unwind!
, Strategy
traitGuard
→ ScopeGuard
ScopeGuard::with_strategy
.ScopeGuard
now implements Debug
.0.2.0
no_std
unconditionally0.1.2
defer!