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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
// Rust language amplification library providing multiple generic trait // implementations, type wrappers, derive macros and other language enhancements // // Written in 2019-2020 by // Dr. Maxim Orlovsky <orlovsky@pandoracore.com> // Martin Habovstiak <martin.habovstiak@gmail.com> // // To the extent possible under law, the author(s) have dedicated all // copyright and related and neighboring rights to this software to // the public domain worldwide. This software is distributed without // any warranty. // // You should have received a copy of the MIT License // along with this software. // If not, see <https://opensource.org/licenses/MIT>. /// Macro for quick & simple `&str` -> `String` conversion: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// enum Error { /// Io(String), /// } /// /// impl From<std::io::Error> for Error { /// fn from(err: std::io::Error) -> Error { /// Self::Io(s!("I/O error")) /// } /// } /// # } /// ``` #[macro_export] macro_rules! s { ( $str:literal ) => { String::from($str) }; } /// This macro allows more semantically-clear code (which can be used especially /// with structure initialization), indicating that instead of type value we are /// generating no value at all (empty collection or data structure filled with /// information indicating absence of data) #[macro_export] macro_rules! none { () => { Default::default() }; } /// Macro for creating [`HashMap`] in the same manner as `vec!` is used for /// [`Vec`]: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// let map = map! { /// s!("key") => 5, /// s!("other_key") => 10 /// }; /// # } /// ``` #[macro_export] macro_rules! map { { } => { { ::std::collections::HashMap::new() } }; { $($key:expr => $value:expr),+ } => { { let mut m = ::std::collections::HashMap::new(); $( m.insert($key, $value); )+ m } } } /// Macro for creating [`HashSet`] in the same manner as `vec!` is used for /// [`Vec`]: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// let map = set![5, 6, 7]; /// # } /// ``` /// /// NB: you can't use repeated values with [`HashSet`], unlike to [`Vec`]'s: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// assert_eq!(set![1, 2, 3, 1], set![1, 2, 3]); /// # } /// ``` #[macro_export] macro_rules! set { { } => { { ::std::collections::HashSet::new() } }; { $($value:expr),+ } => { { let mut m = ::std::collections::HashSet::new(); $( m.insert($value); )+ m } } } /// Macro for creating [`BTreeMap`] in the same manner as `vec!` is used for /// [`Vec`]: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// let map = bmap! { /// s!("key") => 5, /// s!("other_key") => 10 /// }; /// # } /// ``` #[macro_export] macro_rules! bmap { { } => { { ::std::collections::BTreeMap::new() } }; { $($key:expr => $value:expr),+ } => { { let mut m = ::std::collections::BTreeMap::new(); $( m.insert($key, $value); )+ m } } } /// Macro for creating [`BTreeSet`] in the same manner as `vec!` is used for /// [`Vec`]: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// let map = bset![5, 6, 7]; /// # } /// ``` /// /// NB: you can't use repeated values with [`HashSet`], unlike to [`Vec`]'s: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// assert_eq!(bset![1, 2, 3, 1], bset![1, 2, 3]); /// # } /// ``` #[macro_export] macro_rules! bset { { } => { { ::std::collections::BTreeSet::new() } }; { $($value:expr),+ } => { { let mut m = ::std::collections::BTreeSet::new(); $( m.insert($value); )+ m } } } /// Macro for creating [`LinkedList`] in the same manner as `vec!` is used for /// [`Vec`]: /// ``` /// #[macro_use] /// extern crate amplify; /// /// # fn main() { /// let list = list! { /// s!("item one") => /// s!("item two") => /// s!("item three") /// }; /// # } /// ``` #[macro_export] macro_rules! list { { } => { { ::std::collections::LinkedList::new() } }; { $($value:expr)=>+ } => { { let mut m = ::std::collections::LinkedList::new(); $( m.push_back($value); )+ m } } }