summaryrefslogtreecommitdiffstats
path: root/vendor/mac/src/lib.rs
blob: 9287f32bb6296c35a5e5ed960598b9c1b7d4d3ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]

//! # mac
//!
//! A collection of great and ubiqutitous macros.
//!

pub mod test;
pub mod mem;
pub mod format;
pub mod syntax_ext;
pub mod matches;
pub mod inspect;
pub mod cfg;

/// Unwraps an `Option` or returns from the function with the specified return
/// value.
///
/// Can be used on `Result`s by first calling `.ok()` or `.err()` on them.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate mac;
/// fn take_pair<I:Iterator>(iter: &mut I) -> Option<(<I as Iterator>::Item, <I as Iterator>::Item)> {
///    let first = unwrap_or_return!(iter.next(), None);
///    Some((first, unwrap_or_return!(iter.next(), None)))
/// }
/// # fn main() { }
/// ```
#[macro_export]
macro_rules! unwrap_or_return {
    ($e:expr, $r:expr) => (match $e { Some(e) => e, None => return $r, })
}

/// Do-while loop.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate mac;
/// # fn main() {
/// let mut i = 0;
/// let mut n = 0;
///
/// do_while!({
///     n += i;
///     i += 1;
/// } while i < 5);
///
/// assert_eq!(n, 10);
/// # }
/// ```
///
/// The loop always executes at least once.
///
/// ```
/// # #[macro_use] extern crate mac;
/// # fn main() {
/// let mut ran = false;
/// do_while!({ ran = true } while false);
/// assert!(ran);
/// # }
/// ```
#[macro_export]
macro_rules! do_while {
    ($body:block while $condition:expr) => {
        while { $body; $condition } { }
    }
}