Skip to content

Commit

Permalink
Auto merge of #45 - mbrubeck:into-vec, r=emilio
Browse files Browse the repository at this point in the history
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 -->
  • Loading branch information
bors-servo authored Mar 22, 2017
2 parents 65fead9 + 2d78e85 commit f9269da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smallvec"
version = "0.3.1"
version = "0.3.2"
authors = ["Simon Sapin <[email protected]>"]
license = "MPL-2.0"
repository = "https://github.com/servo/rust-smallvec"
Expand Down
22 changes: 22 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]);
}
}

0 comments on commit f9269da

Please sign in to comment.