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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! When you literally just want a literal of the `std::collections` types.

/// Literally just a HashMap literal with keys and values into'd.
/// ```rust
/// # use std::collections::HashMap;
/// # use literally::hmap;
/// let m: HashMap<String, String> = hmap!{ "key" => "value" };
/// assert_eq!(m.get("key"), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! hmap {
    (@single $($x:tt)*) => (());
    (@count $($rest:expr),*) => (<[()]>::len(&[$(hmap!(@single $rest)),*]));

    ($($key:expr => $value:expr),* $(,)?) => {
        {
            let _cap = hmap!(@count $($key),*);
            let mut _map = ::std::collections::HashMap::with_capacity(_cap);
            $(
                let _ = _map.insert($key.into(), $value.into());
            )*
            _map
        }
    };
}

/// Literally just a HashSet literal with values into'd.
/// ```rust
/// # use std::collections::HashSet;
/// # use literally::hset;
/// let s: HashSet<String> = hset!{ "value" };
/// assert_eq!(s.get("value"), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! hset {
    (@single $($x:tt)*) => (());
    (@count $($rest:expr),*) => (<[()]>::len(&[$(hset!(@single $rest)),*]));

    ($($key:expr,)+) => { hset!($($key),+) };
    ($($key:expr),*) => {
        {
            let _cap = hset!(@count $($key),*);
            let mut _set = ::std::collections::HashSet::with_capacity(_cap);
            $(
                let _ = _set.insert($key.into());
            )*
            _set
        }
    };
}

/// Literally just a BTreeMap literal with keys and values into'd.
/// ```rust
/// # use std::collections::BTreeMap;
/// # use literally::bmap;
/// let m: BTreeMap<String, String> = bmap!{ "key" => "value" };
/// assert_eq!(m.get("key"), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! bmap {
    ( $($key:expr => $value:expr),* $(,)?) => {
        {
            let mut _map = ::std::collections::BTreeMap::new();
            $(
                let _ = _map.insert($key.into(), $value.into());
            )*
            _map
        }
    };
}

/// Literally just a HashSet literal with values into'd.
/// ```rust
/// # use std::collections::BTreeSet;
/// # use literally::bset;
/// let s: BTreeSet<String> = bset!{ "value" };
/// assert_eq!(s.get("value"), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! bset {
    ( $($key:expr),* $(,)? ) => {
        {
            let mut _set = ::std::collections::BTreeSet::new();
            $(
                _set.insert($key.into());
            )*
            _set
        }
    };
}

/// Literally just a HashSet literal with values into'd.
/// ```rust
/// # use std::collections::VecDeque;
/// # use literally::vecd;
/// let s: VecDeque<String> = vecd![ "value" ];
/// assert_eq!(s.get(0), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! vecd {
    ( $($key:expr),* $(,)? ) => {
        ::std::collections::VecDeque::from(::std::vec![$(
            $key.into()
        ),*])
    }
}

/// Literally just a HashSet literal with values into'd.
/// ```rust
/// # use std::collections::LinkedList;
/// # use literally::list;
/// let l: LinkedList<String> = list![ "value" ];
/// assert_eq!(l.front(), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! list {
    ( $($key:expr),* $(,)? ) => {
        {
            let mut _lst = ::std::collections::LinkedList::new();
            $(
                _lst.push_back($key.into());
            )*
            _lst
        }
    };
}

/// Literally just a HashSet literal with values into'd.
/// ```rust
/// # use std::collections::BinaryHeap;
/// # use literally::heap;
/// let l: BinaryHeap<String> = heap![ "value" ];
/// assert_eq!(l.peek(), Some(&"value".to_string()))
/// ```
///
#[macro_export(local_inner_macros)]
macro_rules! heap {
    ( $($key:expr),* $(,)? ) => {
        {
            let mut _heap = ::std::collections::BinaryHeap::new();
            $(
                _heap.push($key.into());
            )*
            _heap
        }
    };
}