Auto merge of #45 - mbrubeck:into-vec, r=emilio

Add SmallVec::into_vec

Converts a SmallVec to a Vec, without reallocating if the SmallVec has already spilled onto the heap.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/45)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo
2017-03-22 14:39:46 -07:00
committed by GitHub
2 changed files with 23 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "smallvec"
version = "0.3.1"
version = "0.3.2"
authors = ["Simon Sapin <simon.sapin@exyr.org>"]
license = "MPL-2.0"
repository = "https://github.com/servo/rust-smallvec"
+22
View File
@@ -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]);
}
}