1
Fork 0

improve the performance of the vec![] macro

Closes #17865
This commit is contained in:
Daniel Micay 2014-10-10 06:19:40 -04:00
parent 310f2deb99
commit 02d976a7f9
5 changed files with 13 additions and 14 deletions

View file

@ -2331,7 +2331,7 @@ mod tests {
fn test_into_vec() { fn test_into_vec() {
let xs = box [1u, 2, 3]; let xs = box [1u, 2, 3];
let ys = xs.into_vec(); let ys = xs.into_vec();
assert_eq!(ys.as_slice(), [1u, 2, 3]); assert_eq!(ys.as_slice(), [1u, 2, 3].as_slice());
} }
} }

View file

@ -2650,7 +2650,7 @@ mod tests {
fn test_into_boxed_slice() { fn test_into_boxed_slice() {
let xs = vec![1u, 2, 3]; let xs = vec![1u, 2, 3];
let ys = xs.into_boxed_slice(); let ys = xs.into_boxed_slice();
assert_eq!(ys.as_slice(), [1u, 2, 3]); assert_eq!(ys.as_slice(), [1u, 2, 3].as_slice());
} }
#[bench] #[bench]

View file

@ -18,7 +18,6 @@
//! listener (socket server) implements the `Listener` and `Acceptor` traits. //! listener (socket server) implements the `Listener` and `Acceptor` traits.
use clone::Clone; use clone::Clone;
use collections::MutableSeq;
use io::IoResult; use io::IoResult;
use iter::Iterator; use iter::Iterator;
use slice::ImmutableSlice; use slice::ImmutableSlice;

View file

@ -272,7 +272,9 @@ mod std {
// The test runner calls ::std::os::args() but really wants realstd // The test runner calls ::std::os::args() but really wants realstd
#[cfg(test)] pub use realstd::os as os; #[cfg(test)] pub use realstd::os as os;
// The test runner requires std::slice::Vector, so re-export std::slice just for it. // The test runner requires std::slice::Vector, so re-export std::slice just for it.
#[cfg(test)] pub use slice; //
// It is also used in vec![]
pub use slice;
pub use collections; // vec!() uses MutableSeq pub use boxed; // used for vec![]
} }

View file

@ -323,16 +323,14 @@ macro_rules! try(
/// Create a `std::vec::Vec` containing the arguments. /// Create a `std::vec::Vec` containing the arguments.
#[macro_export] #[macro_export]
macro_rules! vec( macro_rules! vec[
($($e:expr),*) => ({ ($($x:expr),*) => ({
// leading _ to allow empty construction without a warning. use std::slice::BoxedSlice;
let mut _temp = ::std::vec::Vec::new(); let xs: ::std::boxed::Box<[_]> = box [$($x),*];
$(_temp.push($e);)* xs.into_vec()
_temp
}); });
($($e:expr),+,) => (vec!($($e),+)) ($($x:expr,)*) => (vec![$($x),*])
) ]
/// A macro to select an event from a number of receivers. /// A macro to select an event from a number of receivers.
/// ///