1 | #![cfg_attr (test, deny(warnings))] |
2 | #![deny (missing_docs)] |
3 | |
4 | //! # mac |
5 | //! |
6 | //! A collection of great and ubiqutitous macros. |
7 | //! |
8 | |
9 | pub mod test; |
10 | pub mod mem; |
11 | pub mod format; |
12 | pub mod syntax_ext; |
13 | pub mod matches; |
14 | pub mod inspect; |
15 | pub mod cfg; |
16 | |
17 | /// Unwraps an `Option` or returns from the function with the specified return |
18 | /// value. |
19 | /// |
20 | /// Can be used on `Result`s by first calling `.ok()` or `.err()` on them. |
21 | /// |
22 | /// # Examples |
23 | /// |
24 | /// ``` |
25 | /// # #[macro_use ] extern crate mac; |
26 | /// fn take_pair<I:Iterator>(iter: &mut I) -> Option<(<I as Iterator>::Item, <I as Iterator>::Item)> { |
27 | /// let first = unwrap_or_return!(iter.next(), None); |
28 | /// Some((first, unwrap_or_return!(iter.next(), None))) |
29 | /// } |
30 | /// # fn main() { } |
31 | /// ``` |
32 | #[macro_export ] |
33 | macro_rules! unwrap_or_return { |
34 | ($e:expr, $r:expr) => (match $e { Some(e) => e, None => return $r, }) |
35 | } |
36 | |
37 | /// Do-while loop. |
38 | /// |
39 | /// # Examples |
40 | /// |
41 | /// ``` |
42 | /// # #[macro_use ] extern crate mac; |
43 | /// # fn main() { |
44 | /// let mut i = 0; |
45 | /// let mut n = 0; |
46 | /// |
47 | /// do_while!({ |
48 | /// n += i; |
49 | /// i += 1; |
50 | /// } while i < 5); |
51 | /// |
52 | /// assert_eq!(n, 10); |
53 | /// # } |
54 | /// ``` |
55 | /// |
56 | /// The loop always executes at least once. |
57 | /// |
58 | /// ``` |
59 | /// # #[macro_use ] extern crate mac; |
60 | /// # fn main() { |
61 | /// let mut ran = false; |
62 | /// do_while!({ ran = true } while false); |
63 | /// assert!(ran); |
64 | /// # } |
65 | /// ``` |
66 | #[macro_export ] |
67 | macro_rules! do_while { |
68 | ($body:block while $condition:expr) => { |
69 | while { $body; $condition } { } |
70 | } |
71 | } |
72 | |