Add a with_capacity constructor

Closes #62.
This commit is contained in:
Matt Brubeck
2017-09-19 12:20:08 -07:00
parent 09316926d5
commit 1d0ea3fdfe
+33
View File
@@ -273,6 +273,26 @@ impl<A: Array> SmallVec<A> {
}
}
/// Construct an empty vector with enough capacity pre-allocated to store at least `n`
/// elements.
///
/// Will create a heap allocation only if `n` is larger than the inline capacity.
///
/// ```
/// # use smallvec::SmallVec;
///
/// let v: SmallVec<[u8; 3]> = SmallVec::with_capacity(100);
///
/// assert!(v.is_empty());
/// assert!(v.capacity() >= 100);
/// ```
#[inline]
pub fn with_capacity(n: usize) -> Self {
let mut v = SmallVec::new();
v.reserve_exact(n);
v
}
/// Construct a new `SmallVec` from a `Vec<A::Item>` without copying
/// elements.
///
@@ -1156,6 +1176,19 @@ pub mod tests {
assert!(Some(SmallVec::<[&u32; 2]>::new()).is_some());
}
#[test]
fn test_with_capacity() {
let v: SmallVec<[u8; 3]> = SmallVec::with_capacity(1);
assert!(v.is_empty());
assert!(!v.spilled());
assert_eq!(v.capacity(), 3);
let v: SmallVec<[u8; 3]> = SmallVec::with_capacity(10);
assert!(v.is_empty());
assert!(v.spilled());
assert_eq!(v.capacity(), 10);
}
#[test]
fn drain() {
let mut v: SmallVec<[u8; 2]> = SmallVec::new();