literal/
lib.rs

1
2#[cfg(test)]
3mod tests;
4
5use std::collections::{HashMap,BTreeMap};
6use std::collections::{HashSet,BTreeSet};
7
8/// Combines the array literal with `.into()`.
9/// # Examples
10/// ```
11/// use literal::array_from;
12/// let a: &[String] = &array_from!["a", "b", "c", "d"];
13/// ```
14#[macro_export]
15macro_rules! array_from {
16    ($($item:expr),* $(,)?) => {[$($item.into(),)*]}
17}
18
19/// Combines the vector literal with `.into()`.
20/// # Examples
21/// ```
22/// use literal::vec_from;
23/// let a: Vec<String> = vec_from!["a", "b", "c", "d"];
24/// ```
25#[macro_export]
26macro_rules! vec_from {
27    ($($item:expr),* $(,)?) => {vec![$($item.into(),)*]}
28}
29
30/// Literal for `LinkedList`.
31/// # Examples
32/// ```
33/// use std::collections::LinkedList;
34/// use literal::list;
35///
36/// let a: LinkedList<i32> = list![1, 2, 3, 4];
37#[macro_export]
38macro_rules! list {
39    ($($item:expr),* $(,)?) => {{
40        let mut _list = LinkedList::new();
41        $(_list.push_back($item);)*
42        _list
43    }}
44}
45
46/// Combines the list literal with `.into()`.
47/// # Examples
48/// ```
49/// use std::collections::LinkedList;
50/// use literal::list_from;
51///
52/// let a: LinkedList<String> = list_from!["a", "b", "c", "d"];
53/// ```
54#[macro_export]
55macro_rules! list_from {
56    ($($item:expr),* $(,)?) => {{
57        let mut _list = LinkedList::new();
58        $(_list.push_back($item.into());)*
59        _list
60    }}
61}
62
63/// Interface between set literals and set data types.
64pub trait SetLiteral<Item>: Sized {
65    fn new() -> Self;
66    fn insert(m: &mut Self, item: Item);
67    fn with_capacity(_n: usize) -> Self {Self::new()}
68}
69
70impl<Item> SetLiteral<Item> for HashSet<Item>
71where Item: std::cmp::Eq+std::hash::Hash
72{
73    fn new() -> Self {HashSet::new()}
74    fn with_capacity(n: usize) -> Self {HashSet::with_capacity(n)}
75    fn insert(m: &mut Self, item: Item){m.insert(item);}
76}
77
78impl<Item> SetLiteral<Item> for BTreeSet<Item>
79where Item: std::cmp::Ord
80{
81    fn new() -> Self {BTreeSet::new()}
82    fn insert(m: &mut Self, item: Item){m.insert(item);}
83}
84
85/// Set literal with `.into()` for each element.
86/// # Examples
87/// ```
88/// use std::collections::{HashSet,BTreeSet};
89/// use literal::{set,SetLiteral};
90///
91/// let a: HashSet<String> = set!{"x", "y"};
92///
93/// let a: BTreeSet<String> = set!{"x", "y"};
94/// ```
95#[macro_export]
96macro_rules! set {
97    ($( $item:expr ),* $(,)?) => {{
98        let mut _a = SetLiteral::new();
99        $(SetLiteral::insert(&mut _a,$item.into());)*
100        _a
101    }};
102}
103
104/// Interface between map literals and map data types.
105pub trait MapLiteral<K,V>: Sized {
106    fn new() -> Self;
107    fn insert(m: &mut Self, k: K, v: V);
108    fn with_capacity(_n: usize) -> Self {Self::new()}
109}
110
111impl<K,V> MapLiteral<K,V> for HashMap<K,V>
112where K: std::cmp::Eq+std::hash::Hash
113{
114    fn new() -> Self {HashMap::new()}
115    fn with_capacity(n: usize) -> Self {HashMap::with_capacity(n)}
116    fn insert(m: &mut Self, k: K, v: V){m.insert(k,v);}
117}
118
119impl<K,V> MapLiteral<K,V> for BTreeMap<K,V>
120where K: std::cmp::Ord
121{
122    fn new() -> Self {BTreeMap::new()}
123    fn insert(m: &mut Self, k: K, v: V){m.insert(k,v);}
124}
125
126/// Map literal with `.into()` for each key and value.
127/// # Examples
128/// ```
129/// use std::collections::{HashMap,BTreeMap};
130/// use literal::{map,MapLiteral};
131///
132/// let m: HashMap<String,i32> = map!{"x": 1, "y": 2};
133///
134/// let m: BTreeMap<String,i32> = map!{"x": 1, "y": 2};
135/// ```
136#[macro_export]
137macro_rules! map {
138    ($( $key:tt: $value:expr ),* $(,)?) => {{
139        let mut _temp_map = MapLiteral::new();
140        $(MapLiteral::insert(&mut _temp_map,$key.into(),$value.into());)*
141        _temp_map
142    }};
143    ({$init:expr} {$( $key:tt: $value:expr ),* $(,)?}) => {{
144        let mut _temp_map = $init;
145        $(MapLiteral::insert(&mut _temp_map, $key.into(),$value.into());)*
146        _temp_map
147    }};
148    ({$init:expr; $tk:expr, $tv:expr} {$( $key:tt: $value:expr ),* $(,)?}) => {{
149        let mut _temp_map = $init;
150        $(MapLiteral::insert(&mut _temp_map,$tk($key),$tv($value));)*
151        _temp_map
152    }};
153    ({$tk:expr, $tv:expr} {$( $key:tt: $value:expr ),* $(,)?}) => {{
154        let mut _temp_map = MapLiteral::new();
155        $(MapLiteral::insert(&mut _temp_map,$tk($key),$tv($value));)*
156        _temp_map
157    }};
158}
159