rewrite map macros not to use an intermediate allocation

This commit is contained in:
Robert Habermeier 2016-05-31 16:29:53 +02:00
parent b036f1de98
commit b729a381f8

View File

@ -26,30 +26,46 @@ pub use sha3::*;
#[macro_export] #[macro_export]
macro_rules! hash_map { macro_rules! hash_map {
( $( $x:expr => $y:expr ),* ) => { ( $( $x:expr => $y:expr ),* ) => {{
vec![ $( ($x, $y) ),* ].into_iter().collect::<HashMap<_, _>>() let mut x = HashMap::new();
} $(
x.insert($x, $y);
)*
x
}}
} }
#[macro_export] #[macro_export]
macro_rules! hash_mapx { macro_rules! hash_mapx {
( $( $x:expr => $y:expr ),* ) => { ( $( $x:expr => $y:expr ),* ) => {{
vec![ $( ( From::from($x), From::from($y) ) ),* ].into_iter().collect::<HashMap<_, _>>() let mut x = HashMap::new();
} $(
x.insert($x.into(), $y.into());
)*
x
}}
} }
#[macro_export] #[macro_export]
macro_rules! map { macro_rules! map {
( $( $x:expr => $y:expr ),* ) => { ( $( $x:expr => $y:expr ),* ) => {{
vec![ $( ($x, $y) ),* ].into_iter().collect::<BTreeMap<_, _>>() let mut x = BTreeMap::new();
} $(
x.insert($x, $y);
)*
x
}}
} }
#[macro_export] #[macro_export]
macro_rules! mapx { macro_rules! mapx {
( $( $x:expr => $y:expr ),* ) => { ( $( $x:expr => $y:expr ),* ) => {{
vec![ $( ( From::from($x), From::from($y) ) ),* ].into_iter().collect::<BTreeMap<_, _>>() let mut x = BTreeMap::new();
} $(
x.insert($x.into(), $y.into());
)*
x
}}
} }
#[macro_export] #[macro_export]