Skip to content

Commit 71c8abb

Browse files
authored
Shrink code produced by smallvec![]. (#385)
Currently `smallvec![]` expands to this: ``` { let count = 0usize; #[allow(unused_mut)] let mut vec = ::smallvec::SmallVec::new(); if count <= vec.capacity() { vec } else { ::smallvec::SmallVec::from_vec(::alloc::vec::Vec::new()) } }; ``` This commit adds a rule to the `smallvec!` macro for the zero-length case so it instead expands to this: ``` ::smallvec::SmallVec::new() ``` The `std::vec!` macro already has a similar special case. This commit also improves the non-zero case. - It removes the `#[allow(unused_mut)]`, which was only needed for the zero-length case. - It changes the `*` repetitions to `+`. (Again, like `std::vec`.)
1 parent 4fe4068 commit 71c8abb

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,18 +2140,20 @@ impl<T: Clone, const N: usize> Clone for IntoIter<T, N> {
21402140
macro_rules! smallvec {
21412141
// count helper: transform any expression into 1
21422142
(@one $x:expr) => (1usize);
2143+
() => (
2144+
$crate::SmallVec::new()
2145+
);
21432146
($elem:expr; $n:expr) => ({
21442147
$crate::SmallVec::from_elem($elem, $n)
21452148
});
2146-
($($x:expr),*$(,)?) => ({
2147-
let count = 0usize $(+ $crate::smallvec!(@one $x))*;
2148-
#[allow(unused_mut)]
2149+
($($x:expr),+$(,)?) => ({
2150+
let count = 0usize $(+ $crate::smallvec!(@one $x))+;
21492151
let mut vec = $crate::SmallVec::new();
21502152
if count <= vec.capacity() {
21512153
$(vec.push($x);)*
21522154
vec
21532155
} else {
2154-
$crate::SmallVec::from_vec($crate::alloc::vec![$($x,)*])
2156+
$crate::SmallVec::from_vec($crate::alloc::vec![$($x,)+])
21552157
}
21562158
});
21572159
}

0 commit comments

Comments
 (0)