Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate better code for "large" vec![] macro invocations #17857

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),*) => ({
Expand All @@ -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.
///
Expand Down