From b729a381f8f926786dbe4d6a92bd50c6566eb0a1 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 31 May 2016 16:29:53 +0200 Subject: [PATCH] rewrite map macros not to use an intermediate allocation --- util/src/common.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/util/src/common.rs b/util/src/common.rs index a4ba41f82..7ed660f29 100644 --- a/util/src/common.rs +++ b/util/src/common.rs @@ -26,30 +26,46 @@ pub use sha3::*; #[macro_export] macro_rules! hash_map { - ( $( $x:expr => $y:expr ),* ) => { - vec![ $( ($x, $y) ),* ].into_iter().collect::>() - } + ( $( $x:expr => $y:expr ),* ) => {{ + let mut x = HashMap::new(); + $( + x.insert($x, $y); + )* + x + }} } #[macro_export] macro_rules! hash_mapx { - ( $( $x:expr => $y:expr ),* ) => { - vec![ $( ( From::from($x), From::from($y) ) ),* ].into_iter().collect::>() - } + ( $( $x:expr => $y:expr ),* ) => {{ + let mut x = HashMap::new(); + $( + x.insert($x.into(), $y.into()); + )* + x + }} } #[macro_export] macro_rules! map { - ( $( $x:expr => $y:expr ),* ) => { - vec![ $( ($x, $y) ),* ].into_iter().collect::>() - } + ( $( $x:expr => $y:expr ),* ) => {{ + let mut x = BTreeMap::new(); + $( + x.insert($x, $y); + )* + x + }} } #[macro_export] macro_rules! mapx { - ( $( $x:expr => $y:expr ),* ) => { - vec![ $( ( From::from($x), From::from($y) ) ),* ].into_iter().collect::>() - } + ( $( $x:expr => $y:expr ),* ) => {{ + let mut x = BTreeMap::new(); + $( + x.insert($x.into(), $y.into()); + )* + x + }} } #[macro_export]