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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/// Create a `BTreeMap` containing the arguments
/// 
/// `btreemap!` allows `BTreeMap`s to be constructed using minimal syntax:
/// 
/// - Create a `BTreeMap` using a list of `key => value` pairs:
/// 
/// ```
/// # #[macro_use] extern crate collection_macros;
/// use std::collections::BTreeMap;
/// # fn main() {
/// let m = btreemap!{
///     1 => "foo",
///     2 => "bar",
///     3 => "baz",
///     5 => "quux",
/// };
/// assert_eq!(m.get(&1), Some(&"foo"));
/// assert_eq!(m.get(&2), Some(&"bar"));
/// assert_eq!(m.get(&3), Some(&"baz"));
/// assert_eq!(m.get(&5), Some(&"quux"));
/// # }
/// ```
/// 
#[cfg_attr(feature = "nightly", doc = r#"If you are using nightly, there is a second form to this macro:"#)]
#[cfg_attr(feature = "nightly", doc = r#""#)]
#[cfg_attr(feature = "nightly", doc = r#"- Create a `BTreeMap` using the `BTreeMap::with_b` constructor, and a list"#)]
#[cfg_attr(feature = "nightly", doc = r#"  of `key => value` pairs. This is currently only available on nightly"#)]
#[cfg_attr(feature = "nightly", doc = r#""#)]
#[cfg_attr(feature = "nightly", doc = r#"```"#)]
#[cfg_attr(feature = "nightly", doc = r#"# #![feature(collections)]"#)]
#[cfg_attr(feature = "nightly", doc = r#"# #[macro_use] extern crate collection_macros;"#)]
#[cfg_attr(feature = "nightly", doc = r#"use std::collections::BTreeMap;"#)]
#[cfg_attr(feature = "nightly", doc = r#"# fn main() {"#)]
#[cfg_attr(feature = "nightly", doc = r#"let m = btreemap!{"#)]
#[cfg_attr(feature = "nightly", doc = r#"    6;"#)]
#[cfg_attr(feature = "nightly", doc = r#"    1 => "foo","#)]
#[cfg_attr(feature = "nightly", doc = r#"    2 => "bar","#)]
#[cfg_attr(feature = "nightly", doc = r#"    3 => "baz","#)]
#[cfg_attr(feature = "nightly", doc = r#"    5 => "quux","#)]
#[cfg_attr(feature = "nightly", doc = r#"};"#)]
#[cfg_attr(feature = "nightly", doc = r#"assert_eq!(m.get(&1), Some(&"foo"));"#)]
#[cfg_attr(feature = "nightly", doc = r#"assert_eq!(m.get(&2), Some(&"bar"));"#)]
#[cfg_attr(feature = "nightly", doc = r#"assert_eq!(m.get(&3), Some(&"baz"));"#)]
#[cfg_attr(feature = "nightly", doc = r#"assert_eq!(m.get(&5), Some(&"quux"));"#)]
#[cfg_attr(feature = "nightly", doc = r#"# }"#)]
#[cfg_attr(feature = "nightly", doc = "```")]
#[cfg_attr(feature = "nightly", doc = "")]
#[macro_export]
macro_rules! btreemap {
    ( $b:expr; $($x:expr => $y:expr),* ) => ({
        let mut temp_map = BTreeMap::with_b($b);
        $(
            temp_map.insert($x, $y);
        )*
        temp_map
    });
    ( $($x:expr => $y:expr),* ) => ({
        let mut temp_map = BTreeMap::new();
        $(
            temp_map.insert($x, $y);
        )*
        temp_map
    });
    ( $b:expr; $($x:expr => $y:expr,)* ) => (
        btreemap!{$b; $($x => $y),*}
    );
    ( $($x:expr => $y:expr,)* ) => (
        btreemap!{$($x => $y),*}
    );
}

#[cfg(test)] mod tests {
    use std::collections::{BTreeMap};

    #[cfg(feature = "nightly")]
    #[test]
    fn test_btreemap_with_b() {
        let bmap = btreemap! {
            6;
            1 => "hello",
            3 => "blah",
        };

        let mut should_be = BTreeMap::new();
        should_be.insert(1, "hello");
        should_be.insert(3, "blah");

        assert_eq!(bmap, should_be);
    }

    #[test]
    fn test_btreemap() {
        let map = btreemap! {
            1 => "hello",
            3 => "blah",
        };

        let mut should_be = BTreeMap::new();
        should_be.insert(1, "hello");
        should_be.insert(3, "blah");

        assert_eq!(map, should_be);

    }
}