mirror of
https://github.com/openharmony/third_party_rust_tinyvec.git
synced 2026-07-19 14:23:33 -04:00
Merge pull request #37 from mental32/26-fix-tinyvec-capacity
Account for heap allocated capacities.
This commit is contained in:
+6
-2
@@ -170,11 +170,15 @@ impl<A: Array> TinyVec<A> {
|
||||
|
||||
/// The capacity of the `TinyVec`.
|
||||
///
|
||||
/// This is fixed based on the array type.
|
||||
/// When not heap allocated this is fixed based on the array type.
|
||||
/// Otherwise its the result of the underlying Vec::capacity.
|
||||
#[inline(always)]
|
||||
#[must_use]
|
||||
pub fn capacity(&self) -> usize {
|
||||
A::CAPACITY
|
||||
match self {
|
||||
TinyVec::Inline(_) => A::CAPACITY,
|
||||
TinyVec::Heap(v) => v.capacity(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes all elements from the vec.
|
||||
|
||||
@@ -21,6 +21,15 @@ fn TinyVec_swap_remove() {
|
||||
assert_eq!(&tv[..], &[][..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn TinyVec_capacity() {
|
||||
let mut tv: TinyVec<[i32; 1]> = Default::default();
|
||||
assert_eq!(tv.capacity(), 1);
|
||||
tv.move_to_the_heap();
|
||||
tv.extend_from_slice(&[1, 2, 3, 4]);
|
||||
assert_eq!(tv.capacity(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn TinyVec_drain() {
|
||||
let mut tv: TinyVec<[i32; 10]> = Default::default();
|
||||
|
||||
Reference in New Issue
Block a user