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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/// A more flexible vector initialization macro. `velcro::vec!` is a /// drop-in replacement for the built-in `std::vec!` macro, but with extra /// functionality. In particular, it adds the `..` spread operator, which /// can insert multiple elements at once, provided that the expression /// implements `IntoIterator`, which is the case for all iterators and most /// collections. /// /// # Usage /// /// ```rust /// use velcro::vec; /// /// assert_eq!(vec![..0..7], vec![0, 1, 2, 3, 4, 5, 6]); /// assert_eq!(vec![0, 1, ..2..7], vec![0, 1, 2, 3, 4, 5, 6]); /// /// let other = vec![3, 4, 5]; /// assert_eq!(vec![0, 1, 2, ..other, 6], vec![0, 1, 2, 3, 4, 5, 6]); /// /// let mut it = (0..=3).into_iter().map(|x| x + 2); /// assert_eq!(vec![0, 1, ..it, 6], vec![0, 1, 2, 3, 4, 5, 6]); /// /// assert_eq!(vec![3; 5], vec![3, 3, 3, 3, 3]); /// let range = 3..; /// assert_eq!(vec![..range; 5], vec![3, 4, 5, 6, 7]); /// // Note that this is different from not using `..` /// let range = 3..; /// assert_eq!(vec![range; 5], vec![3.., 3.., 3.., 3.., 3..]); /// /// // If the requested length is less than the input length then the /// // result will be shorter. /// let range = 2..5; /// assert_eq!(vec![..range; 5], vec![2, 3, 4]); /// ``` /// /// # Performance /// /// For syntax that is supported by `std::vec!`, `velco::vec!` performs the same, /// since it delegates to `std::vec!` wherever the input is compatible. That is, /// if you don't use the `..` spread operator, you don't pay for it. pub use velcro_macros::vec; /// Works the same as `vec!` except that values may be of any type that can be /// converted into the item type via an implementation of `Into`. /// /// The type of the item must be known at compile time, and usually this means an /// explicit type annotation is required. /// /// # Usage /// /// ```rust /// use velcro::vec_from; /// /// #[derive(Debug, PartialEq)] /// struct Foo(u64); /// /// impl From<u64> for Foo { /// fn from(other: u64) -> Self { /// Foo(other) /// } /// } /// /// let foos: Vec<Foo> = vec_from![1, 2, Foo(3), ..4..=6, 7]; /// assert_eq!(foos, vec![Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]); /// ``` pub use velcro_macros::vec_from; /// An initializer for `BTreeSet`, allowing for items to be specified individually /// or "spread" using the `..` operator. /// /// # Usage /// /// ```rust /// # use std::collections::BTreeSet; /// use velcro::btree_set; /// let set: BTreeSet<_> = (0..7).into_iter().collect(); /// /// assert_eq!(btree_set![..0..7], set); /// assert_eq!(btree_set![0, 1, ..2..7], set); ///``` pub use velcro_macros::btree_set; /// An initializer for `BTreeSet` that works the same as `btree_set!` except that /// values can be of any type that can be converted into the collection's item type. /// /// The type of the item must be known at compile time, and usually this means an /// explicit type annotation is required. /// /// # Usage /// /// ```rust /// # use std::collections::BTreeSet; /// use velcro::{btree_set, btree_set_from}; /// /// #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] /// struct Foo(u64); /// /// impl From<u64> for Foo { /// fn from(other: u64) -> Self { /// Foo(other) /// } /// } /// /// let foos: BTreeSet<Foo> = btree_set_from![1, 2, Foo(3), ..4..=6, 7]; /// assert_eq!(foos, btree_set![Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]); ///``` pub use velcro_macros::btree_set_from; /// An initializer for `HashSet`, allowing for items to be specified individually /// or "spread" using the `..` operator. /// /// # Usage /// /// ```rust /// # use std::collections::HashSet; /// use velcro::hash_set; /// let set: HashSet<_> = (0..7).into_iter().collect(); /// /// assert_eq!(hash_set![..0..7], set); /// assert_eq!(hash_set![0, 1, ..2..7], set); ///``` pub use velcro_macros::hash_set; /// An initializer for `HashSet` that works the same as `hash_set!` except that /// values can be of any type that can be converted into the collection's item /// type via an `Into` implementation. /// /// The type of the item must be known at compile time, and usually this means an /// explicit type annotation is required. /// /// # Usage /// /// ```rust /// # use std::collections::HashSet; /// use velcro::{hash_set, hash_set_from}; /// /// #[derive(Debug, PartialEq, Eq, Hash)] /// struct Foo(u64); /// /// impl From<u64> for Foo { /// fn from(other: u64) -> Self { /// Foo(other) /// } /// } /// /// let foos: HashSet<Foo> = hash_set_from![1, 2, Foo(3), ..4..=6, 7]; /// assert_eq!(foos, hash_set![Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7)]); ///``` pub use velcro_macros::hash_set_from; /// An initializer for `HashMap`, allowing for entries to be specified individually /// or for the same value to be given to multiple keys using the `..` operator. /// /// # Usage /// /// ```rust /// # use std::collections::HashMap; /// use velcro::hash_map; /// let mut map1 = HashMap::new(); /// map1.insert('a', 0); /// map1.insert('b', 1); /// map1.insert('c', 1); /// map1.insert('d', 1); /// map1.insert('e', 1); /// map1.insert('f', 2); /// /// let map2 = hash_map! { /// 'a': 0, /// ..'b'..='e': 1, /// 'f': 2 /// }; /// /// assert_eq!(map1, map2); ///``` pub use velcro_macros::hash_map; /// An initializer for `HashMap` that works the same as `hash_map!` except that /// values can be of any type that can be converted into the collection's item /// type via an `Into` implementation. /// /// The type of the item must be known at compile time, and usually this means an /// explicit type annotation is required. /// /// # Usage /// /// ```rust /// # use std::collections::HashMap; /// use velcro::hash_map_from; /// /// #[derive(Debug, PartialEq, Eq, Hash)] /// struct Foo(u64); /// /// impl From<u64> for Foo { /// fn from(other: u64) -> Self { /// Foo(other) /// } /// } /// /// let mut map1 = HashMap::new(); /// map1.insert('a', Foo(0)); /// map1.insert('b', Foo(1)); /// map1.insert('c', Foo(1)); /// map1.insert('d', Foo(1)); /// map1.insert('e', Foo(1)); /// map1.insert('f', Foo(2)); /// /// let map2: HashMap<char, Foo> = hash_map_from! { /// 'a': 0, /// ..'b'..='e': 1, /// 'f': 2 /// }; /// /// assert_eq!(map1, map2); ///``` pub use velcro_macros::hash_map_from; /// An initializer for `BTreeMap`, allowing for entries to be specified individually /// or for the same value to be given to multiple keys using the `..` operator. /// /// # Usage /// /// ```rust /// # use std::collections::BTreeMap; /// use velcro::btree_map; /// /// let mut map1 = BTreeMap::new(); /// map1.insert('a', 0); /// map1.insert('b', 1); /// map1.insert('c', 1); /// map1.insert('d', 1); /// map1.insert('e', 1); /// map1.insert('f', 2); /// /// let map2 = btree_map! { /// 'a': 0, /// ..'b'..='e': 1, /// 'f': 2 /// }; /// /// assert_eq!(map1, map2); ///``` pub use velcro_macros::btree_map; /// An initializer for `BTreeMap` that works the same as `btree_map!` except that /// values can be of any type that can be converted into the collection's item /// type via an `Into` implementation. /// /// The type of the item must be known at compile time, and usually this means an /// explicit type annotation is required. /// /// # Usage /// /// ```rust /// # use std::collections::BTreeMap; /// use velcro::btree_map_from; /// /// #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] /// struct Foo(u64); /// /// impl From<u64> for Foo { /// fn from(other: u64) -> Self { /// Foo(other) /// } /// } /// /// let mut map1 = BTreeMap::new(); /// map1.insert('a', Foo(0)); /// map1.insert('b', Foo(1)); /// map1.insert('c', Foo(1)); /// map1.insert('d', Foo(1)); /// map1.insert('e', Foo(1)); /// map1.insert('f', Foo(2)); /// /// let map2: BTreeMap<char, Foo> = btree_map_from! { /// 'a': 0, /// ..'b'..='e': 1, /// 'f': 2 /// }; /// /// assert_eq!(map1, map2); ///``` pub use velcro_macros::btree_map_from; /// Creates an iterator, over the given values. Other collections and iterators /// may also be interspersed, or "spread", using the `..` operator. /// /// # Usage /// /// ```rust /// use velcro::iter; /// let vec = vec![0, 1, 2, 3]; /// /// assert_eq!(iter![..vec, 4, 5, 6].collect::<Vec<_>>(), vec![0, 1, 2, 3, 4, 5, 6]); /// /// for x in iter![0, 1, ..2..=5, 6] { /// println!("x = {}", x); /// } /// /// assert_eq!(iter![0, 1, ..2..=5, 6].collect::<Vec<_>>(), vec![0, 1, 2, 3, 4, 5, 6]); ///``` pub use velcro_macros::iter; /// Creates an iterator, over the given values. Works the same as `iter` except that values /// may be any type that can be converted to the iterator item type via an `Into` /// implementation. /// /// # Usage /// /// ```rust /// use velcro::iter; /// /// #[derive(Debug, PartialEq)] /// struct Foo(u64); /// /// impl From<u64> for Foo { /// fn from(other: u64) -> Self { /// Foo(other) /// } /// } /// /// assert_eq!(iter![0, 1, ..2..=5, 6].collect::<Vec<_>>(), vec![0, 1, 2, 3, 4, 5, 6]); ///``` pub use velcro_macros::iter_from;