Skip to content

Commit 6e435c4

Browse files
committed
Make sure smallvec! is usable without being in scope
Macro recursion should use a `$crate` prefix to reliably find itself.
1 parent bb24de7 commit 6e435c4

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ macro_rules! smallvec {
145145
$crate::SmallVec::from_elem($elem, $n)
146146
});
147147
($($x:expr),*$(,)*) => ({
148-
let count = 0usize $(+ smallvec!(@one $x))*;
148+
let count = 0usize $(+ $crate::smallvec!(@one $x))*;
149149
let mut vec = $crate::SmallVec::new();
150150
if count <= vec.inline_size() {
151151
$(vec.push($x);)*

tests/macro.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// This file tests `smallvec!` without actually having the macro in scope.
2+
/// This forces any recursion to use a `$crate` prefix to reliably find itself.
3+
4+
#[test]
5+
fn smallvec() {
6+
let mut vec: smallvec::SmallVec<[i32; 2]>;
7+
8+
macro_rules! check {
9+
($init:tt) => {
10+
vec = smallvec::smallvec! $init;
11+
assert_eq!(*vec, *vec! $init);
12+
}
13+
}
14+
15+
check!([0; 0]);
16+
check!([1; 1]);
17+
check!([2; 2]);
18+
check!([3; 3]);
19+
20+
check!([]);
21+
check!([1]);
22+
check!([1, 2]);
23+
check!([1, 2, 3]);
24+
}
25+

0 commit comments

Comments
 (0)