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
154
155
156
157
158
159

#[cfg(test)]
mod tests;

use std::collections::{HashMap,BTreeMap};
use std::collections::{HashSet,BTreeSet};

/// Combines the array literal with `.into()`.
/// # Examples
/// ```
/// use literal::array_from;
/// let a: &[String] = &array_from!["a", "b", "c", "d"];
/// ```
#[macro_export]
macro_rules! array_from {
    ($($item:expr),* $(,)?) => {[$($item.into(),)*]}
}

/// Combines the vector literal with `.into()`.
/// # Examples
/// ```
/// use literal::vec_from;
/// let a: Vec<String> = vec_from!["a", "b", "c", "d"];
/// ```
#[macro_export]
macro_rules! vec_from {
    ($($item:expr),* $(,)?) => {vec![$($item.into(),)*]}
}

/// Literal for `LinkedList`.
/// # Examples
/// ```
/// use std::collections::LinkedList;
/// use literal::list;
///
/// let a: LinkedList<i32> = list![1, 2, 3, 4];
#[macro_export]
macro_rules! list {
    ($($item:expr),* $(,)?) => {{
        let mut _list = LinkedList::new();
        $(_list.push_back($item);)*
        _list
    }}
}

/// Combines the list literal with `.into()`.
/// # Examples
/// ```
/// use std::collections::LinkedList;
/// use literal::list_from;
///
/// let a: LinkedList<String> = list_from!["a", "b", "c", "d"];
/// ```
#[macro_export]
macro_rules! list_from {
    ($($item:expr),* $(,)?) => {{
        let mut _list = LinkedList::new();
        $(_list.push_back($item.into());)*
        _list
    }}
}

/// Interface between set literals and set data types.
pub trait SetLiteral<Item>: Sized {
    fn new() -> Self;
    fn insert(m: &mut Self, item: Item);
    fn with_capacity(_n: usize) -> Self {Self::new()}
}

impl<Item> SetLiteral<Item> for HashSet<Item>
where Item: std::cmp::Eq+std::hash::Hash
{
    fn new() -> Self {HashSet::new()}
    fn with_capacity(n: usize) -> Self {HashSet::with_capacity(n)}
    fn insert(m: &mut Self, item: Item){m.insert(item);}
}

impl<Item> SetLiteral<Item> for BTreeSet<Item>
where Item: std::cmp::Ord
{
    fn new() -> Self {BTreeSet::new()}
    fn insert(m: &mut Self, item: Item){m.insert(item);}
}

/// Set literal with `.into()` for each element.
/// # Examples
/// ```
/// use std::collections::{HashSet,BTreeSet};
/// use literal::{set,SetLiteral};
///
/// let a: HashSet<String> = set!{"x", "y"};
///
/// let a: BTreeSet<String> = set!{"x", "y"};
/// ```
#[macro_export]
macro_rules! set {
    ($( $item:expr ),* $(,)?) => {{
        let mut _a = SetLiteral::new();
        $(SetLiteral::insert(&mut _a,$item.into());)*
        _a
    }};
}

/// Interface between map literals and map data types.
pub trait MapLiteral<K,V>: Sized {
    fn new() -> Self;
    fn insert(m: &mut Self, k: K, v: V);
    fn with_capacity(_n: usize) -> Self {Self::new()}
}

impl<K,V> MapLiteral<K,V> for HashMap<K,V>
where K: std::cmp::Eq+std::hash::Hash
{
    fn new() -> Self {HashMap::new()}
    fn with_capacity(n: usize) -> Self {HashMap::with_capacity(n)}
    fn insert(m: &mut Self, k: K, v: V){m.insert(k,v);}
}

impl<K,V> MapLiteral<K,V> for BTreeMap<K,V>
where K: std::cmp::Ord
{
    fn new() -> Self {BTreeMap::new()}
    fn insert(m: &mut Self, k: K, v: V){m.insert(k,v);}
}

/// Map literal with `.into()` for each key and value.
/// # Examples
/// ```
/// use std::collections::{HashMap,BTreeMap};
/// use literal::{map,MapLiteral};
///
/// let m: HashMap<String,i32> = map!{"x": 1, "y": 2};
///
/// let m: BTreeMap<String,i32> = map!{"x": 1, "y": 2};
/// ```
#[macro_export]
macro_rules! map {
    ($( $key:tt: $value:expr ),* $(,)?) => {{
        let mut _temp_map = MapLiteral::new();
        $(MapLiteral::insert(&mut _temp_map,$key.into(),$value.into());)*
        _temp_map
    }};
    ({$init:expr} {$( $key:tt: $value:expr ),* $(,)?}) => {{
        let mut _temp_map = $init;
        $(MapLiteral::insert(&mut _temp_map, $key.into(),$value.into());)*
        _temp_map
    }};
    ({$init:expr; $tk:expr, $tv:expr} {$( $key:tt: $value:expr ),* $(,)?}) => {{
        let mut _temp_map = $init;
        $(MapLiteral::insert(&mut _temp_map,$tk($key),$tv($value));)*
        _temp_map
    }};
    ({$tk:expr, $tv:expr} {$( $key:tt: $value:expr ),* $(,)?}) => {{
        let mut _temp_map = MapLiteral::new();
        $(MapLiteral::insert(&mut _temp_map,$tk($key),$tv($value));)*
        _temp_map
    }};
}