Skip to content

Commit f9269da

Browse files
author
bors-servo
authored
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 -->
2 parents 65fead9 + 2d78e85 commit f9269da

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smallvec"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["Simon Sapin <[email protected]>"]
55
license = "MPL-2.0"
66
repository = "https://github.com/servo/rust-smallvec"

lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,19 @@ impl<A: Array> SmallVec<A> {
482482
}
483483
}
484484
}
485+
486+
/// Convert a SmallVec to a Vec, without reallocating if the SmallVec has already spilled onto
487+
/// the heap.
488+
pub fn into_vec(self) -> Vec<A::Item> {
489+
match self.data {
490+
Inline { .. } => self.into_iter().collect(),
491+
Heap { ptr, capacity } => unsafe {
492+
let v = Vec::from_raw_parts(ptr, self.len, capacity);
493+
mem::forget(self);
494+
v
495+
}
496+
}
497+
}
485498
}
486499

487500
impl<A: Array> SmallVec<A> where A::Item: Copy {
@@ -1341,4 +1354,13 @@ pub mod tests {
13411354
vec.shrink_to_fit();
13421355
assert!(!vec.spilled(), "shrink_to_fit will un-spill if possible");
13431356
}
1357+
1358+
#[test]
1359+
fn test_into_vec() {
1360+
let vec = SmallVec::<[u8; 2]>::from_iter(0..2);
1361+
assert_eq!(vec.into_vec(), vec![0, 1]);
1362+
1363+
let vec = SmallVec::<[u8; 2]>::from_iter(0..3);
1364+
assert_eq!(vec.into_vec(), vec![0, 1, 2]);
1365+
}
13441366
}

0 commit comments

Comments
 (0)