From d0ffbff33b6624d674a32d065bcead611092e25b Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Sun, 2 Feb 2020 16:21:28 +0100 Subject: [PATCH] Change macros to accept statements. Change the macros (defer, defer_on_success and defer_on_unwind) to accept statements instead of just an expression. Update version to "1.1.0". --- Cargo.toml | 2 +- src/lib.rs | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d400bfb..0c727ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "scopeguard" -version = "1.0.0" +version = "1.1.0" license = "MIT/Apache-2.0" repository = "https://github.com/bluss/scopeguard" diff --git a/src/lib.rs b/src/lib.rs index d1db003..b34f0b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,9 +44,9 @@ //! let drop_counter = Cell::new(0); //! { //! // Create a scope guard using `defer!` for the current scope -//! defer! {{ +//! defer! { //! drop_counter.set(1 + drop_counter.get()); -//! }}; +//! } //! //! // Do regular operations here in the meantime. //! @@ -245,21 +245,22 @@ impl Strategy for OnSuccess { /// Macro to create a `ScopeGuard` (always run). /// -/// The macro takes one expression `$e`, which is the body of a closure -/// that will run when the scope is exited. The expression can -/// be a whole block. +/// The macro takes statements, which are the body of a closure +/// that will run when the scope is exited. #[macro_export] macro_rules! defer { - ($e:expr) => { - let _guard = $crate::guard((), |()| $e); - } + (@block $b:expr) => { + let _guard = $crate::guard((), |()| $b); + }; + ($($t:tt)*) => { + defer!(@block { $($t)* }) + }; } /// Macro to create a `ScopeGuard` (run on successful scope exit). /// -/// The macro takes one expression `$e`, which is the body of a closure -/// that will run when the scope is exited. The expression can -/// be a whole block. +/// The macro takes statements, which are the body of a closure +/// that will run when the scope is exited. /// /// Requires crate feature `use_std`. #[cfg(feature = "use_std")] @@ -267,14 +268,16 @@ macro_rules! defer { macro_rules! defer_on_success { ($e:expr) => { let _guard = $crate::guard_on_success((), |()| $e); - } + }; + ($($t:tt)*) => { + defer_on_success!(@block { $($t)* }) + }; } /// Macro to create a `ScopeGuard` (run on unwinding from panic). /// -/// The macro takes one expression `$e`, which is the body of a closure -/// that will run when the scope is exited. The expression can -/// be a whole block. +/// The macro takes statements, which are the body of a closure +/// that will run when the scope is exited. /// /// Requires crate feature `use_std`. #[cfg(feature = "use_std")] @@ -282,7 +285,10 @@ macro_rules! defer_on_success { macro_rules! defer_on_unwind { ($e:expr) => { let _guard = $crate::guard_on_unwind((), |()| $e); - } + }; + ($($t:tt)*) => { + defer_on_unwind!(@block { $($t)* }) + }; } /// `ScopeGuard` is a scope guard that may own a protected value.