prog1/
lib.rs

1// Copyright 2016 Philipp Matthias Schaefer <philipp.matthias.schaefer@posteo.de>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Execute a block after computing a result.
10//!
11//! This crate provides the macro `prog1`, an implementation of the homonymous
12//! Common Lisp macro.
13//!
14//! It allows to compute a result, before executing a block that changes values
15//! previously used to compute the result.
16//!
17//! # Examples
18//!
19//! ```
20//! #[macro_use]
21//! extern crate prog1;
22//!
23//! fn main() {
24//!     let mut a = 0;
25//!
26//!     assert_eq!(prog1!(a; { a += 1 }), 0);
27//!     assert_eq!(a, 1);
28//! }
29//! ```
30
31/// Executes $after after computing the result $result.
32///
33/// For more information see the crate level documentation.
34#[macro_export]
35macro_rules! prog1 {
36    ( $result:expr; $after:block) => {{
37        let result = $result;
38        $after
39        result
40    }}
41}