Skip to content

Commit d682405

Browse files
nnethercotembrubeck
authored andcommitted
Shrink code produced by smallvec![].
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 7c4d350 commit d682405

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
@@ -184,18 +184,20 @@ use core::mem::ManuallyDrop;
184184
macro_rules! smallvec {
185185
// count helper: transform any expression into 1
186186
(@one $x:expr) => (1usize);
187+
() => (
188+
$crate::SmallVec::new()
189+
);
187190
($elem:expr; $n:expr) => ({
188191
$crate::SmallVec::from_elem($elem, $n)
189192
});
190-
($($x:expr),*$(,)*) => ({
191-
let count = 0usize $(+ $crate::smallvec!(@one $x))*;
192-
#[allow(unused_mut)]
193+
($($x:expr),+$(,)?) => ({
194+
let count = 0usize $(+ $crate::smallvec!(@one $x))+;
193195
let mut vec = $crate::SmallVec::new();
194196
if count <= vec.inline_size() {
195197
$(vec.push($x);)*
196198
vec
197199
} else {
198-
$crate::SmallVec::from_vec($crate::alloc::vec![$($x,)*])
200+
$crate::SmallVec::from_vec($crate::alloc::vec![$($x,)+])
199201
}
200202
});
201203
}

0 commit comments

Comments
 (0)