mirror of
https://github.com/openharmony/third_party_rust_rust-smallvec.git
synced 2026-07-21 01:55:25 -04:00
Add SmallVec::into_vec
This commit is contained in:
@@ -482,6 +482,19 @@ impl<A: Array> SmallVec<A> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a SmallVec to a Vec, without reallocating if the SmallVec has already spilled onto
|
||||
/// the heap.
|
||||
pub fn into_vec(self) -> Vec<A::Item> {
|
||||
match self.data {
|
||||
Inline { .. } => self.into_iter().collect(),
|
||||
Heap { ptr, capacity } => unsafe {
|
||||
let v = Vec::from_raw_parts(ptr, self.len, capacity);
|
||||
mem::forget(self);
|
||||
v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Array> SmallVec<A> where A::Item: Copy {
|
||||
@@ -1341,4 +1354,13 @@ pub mod tests {
|
||||
vec.shrink_to_fit();
|
||||
assert!(!vec.spilled(), "shrink_to_fit will un-spill if possible");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_into_vec() {
|
||||
let vec = SmallVec::<[u8; 2]>::from_iter(0..2);
|
||||
assert_eq!(vec.into_vec(), vec![0, 1]);
|
||||
|
||||
let vec = SmallVec::<[u8; 2]>::from_iter(0..3);
|
||||
assert_eq!(vec.into_vec(), vec![0, 1, 2]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user