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

Add vec!-like smallvec! macro #90

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(test)]

#[macro_use]
extern crate smallvec;
extern crate test;

Expand Down Expand Up @@ -109,3 +110,23 @@ fn bench_pushpop(b: &mut Bencher) {
vec
});
}

#[bench]
fn bench_macro_from_elem(b: &mut Bencher) {
b.iter(|| {
let vec: SmallVec<[u64; 16]> = smallvec![42; 100];
vec
});
}

#[bench]
fn bench_macro_from_list(b: &mut Bencher) {
b.iter(|| {
let vec: SmallVec<[u64; 16]> = smallvec![
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000,
0x80000, 0x100000
];
vec
});
}
62 changes: 62 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,55 @@ use std::marker::PhantomData;

use SmallVecData::{Inline, Heap};

/// Creates a [`SmallVec`] containing the arguments.
///
/// `smallvec!` allows `SmallVec`s to be defined with the same syntax as array expressions.
/// There are two forms of this macro:
///
/// - Create a [`SmallVec`] containing a given list of elements:
///
/// ```
/// # #[macro_use] extern crate smallvec;
/// # use smallvec::SmallVec;
/// # fn main() {
/// let v: SmallVec<[_; 128]> = smallvec![1, 2, 3];
/// assert_eq!(v[0], 1);
/// assert_eq!(v[1], 2);
/// assert_eq!(v[2], 3);
/// # }
/// ```
///
/// - Create a [`SmallVec`] from a given element and size:
///
/// ```
/// # #[macro_use] extern crate smallvec;
/// # use smallvec::SmallVec;
/// # fn main() {
/// let v: SmallVec<[_; 0x8000]> = smallvec![1; 3];
/// assert_eq!(v, SmallVec::from_buf([1, 1, 1]));
/// # }
/// ```
///
/// Note that unlike array expressions this syntax supports all elements
/// which implement [`Clone`] and the number of elements doesn't have to be
/// a constant.
///
/// This will use `clone` to duplicate an expression, so one should be careful
/// using this with types having a nonstandard `Clone` implementation. For
/// example, `smallvec![Rc::new(1); 5]` will create a vector of five references
/// to the same boxed integer value, not five references pointing to independently
/// boxed integers.

#[macro_export]
macro_rules! smallvec {
($elem:expr; $n:expr) => ({
SmallVec::from_elem($elem, $n)
});
($($x:expr),*) => ({
SmallVec::from_slice(&[$($x),*])
});
}

/// Common operations implemented by both `Vec` and `SmallVec`.
///
/// This can be used to write generic code that works with both `Vec` and `SmallVec`.
Expand Down Expand Up @@ -747,6 +796,19 @@ impl<A: Array> SmallVec<A> where A::Item: Clone {
self.truncate(len);
}
}

/// Creates a `SmallVec` with `n` copies of `elem`.
/// ```
/// use smallvec::SmallVec;
///
/// let v = SmallVec::<[char; 128]>::from_elem('d', 2);
/// assert_eq!(v, SmallVec::from_buf(['d', 'd']));
/// ```
pub fn from_elem(elem: A::Item, n: usize) -> Self {
let mut v = SmallVec::with_capacity(n);
v.insert_many(0, (0..n).map(|_| elem.clone()));
v
}
}

impl<A: Array> ops::Deref for SmallVec<A> {
Expand Down