diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index fa356432a673..3ca46f8f3e13 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -321,7 +321,22 @@ macro_rules! try( ($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) }) ) +/// A macro to count the number of expressions passed to it +#[macro_export] +macro_rules! count( + ($x:expr, $($xs:expr),+) => { + 1 + count!($($xs),+) + }; + ($x:expr) => { + 1u + }; + ($($x:expr),*) => { + 0u + }; +) + /// Create a `std::vec::Vec` containing the arguments. +#[cfg(stage0)] #[macro_export] macro_rules! vec( ($($e:expr),*) => ({ @@ -333,6 +348,18 @@ macro_rules! vec( ($($e:expr),+,) => (vec!($($e),+)) ) +#[not(cfg(stage0))] +#[macro_export] +macro_rules! vec( + ($($e:expr),*) => ({ + // leading _ to allow empty construction without a warning. + let mut _temp = ::std::vec::Vec::with_capacity(count!($($e),*)); + $(_temp.push($e);)* + _temp + }); + ($($e:expr),+,) => (vec!($($e),+)) +) + /// A macro to select an event from a number of receivers. ///