mirror of
https://github.com/openharmony/third_party_rust_rust-smallvec.git
synced 2026-07-21 01:55:25 -04:00
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user