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
72
73
74
75
76
77
78
79
80
// Copyright 2017 bjnyfv@users.noreply.github.com. See the LICENSE
// files at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/// Macro for prepending an element to the front of a `ConsList`.
///
/// # Examples
/// ```
/// #[macro_use] extern crate cons;
/// extern crate cons_list;
///
/// use cons_list::ConsList;
/// # fn main() {
///
/// // Create an empty ConsList
/// let list: ConsList<i32> = cons!();
/// assert_eq!(list, ConsList::<i32>::new());
///
/// // Create a ConsList with one element
/// let list = cons!(1);
/// assert_eq!(list, ConsList::new().append(1));
///
/// // Prepend an element to a ConList
/// let head = "Hello";
/// let tail = cons!("World");
/// let list = cons!(head, tail);
/// assert_eq!(list, ConsList::new().append("World").append("Hello"));
///
/// # }
/// ```
#[macro_export]
macro_rules! cons {
    () => (cons_list::ConsList::<_>::new());
    ($h:expr) => (cons_list::ConsList::new().append($h));
    ($h:expr, $t:expr) => ($t.append($h));
}

/// Macro for creating a `ConsList`.
///
/// # Examples
/// ```
/// #[macro_use] extern crate cons;
/// extern crate cons_list;
///
/// use cons_list::ConsList;
/// # fn main() {
///
/// // Create an empty ConsList
/// let list: ConsList<i32> = conslist!();
/// assert_eq!(list, ConsList::<i32>::new());
///
/// // Create a ConsList
/// let list = conslist!("A", "B", "C");
/// assert_eq!(list, ConsList::new().append("C").append("B").append("A"));
///
/// # }
/// ```
#[macro_export]
macro_rules! conslist {
    ($($x:expr),*) => {
        {
            let mut elements = Vec::new();
            $(
                elements.push($x);
            )*

            let mut list = cons_list::ConsList::new();
            while let Some(e) = elements.pop() {
                list = list.append(e);
            }
            list
        }
    };
    ($($x:expr,)*) => (conslist![$($x),*]);
}