Skip to content

Commit 731c0e9

Browse files
author
bors-servo
authored
Auto merge of #190 - L0uisc:into-boxed-slice, r=mbrubeck
Implement into_boxed_slice() and doc-comment it. Implements `into_boxed_slice()` as per suggestion in #184. I didn't write tests, since all I did was to call an existing method of `SmallVec`, `into_vec()` to create a `Vec` from it and then call a method of `alloc::vec::Vec`, `into_boxed_slice()` on the resulting `Vec`. Thus, nothing in my code needs testing, since it is trivially offloading to other methods. I didn't, however, find a test exercising `into_vec()`, however. That is maybe something I can do? If so, can I get some pointers to where I should look for example tests and where my tests should live? EDIT: Never mind, I forgot that unit tests are in the same file, so I scrutinized smallvec_ops.rs to search for a test for `into_vec()` and couldn't find it.
2 parents c1870ea + 8b72dba commit 731c0e9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern crate alloc;
3737
#[cfg(any(test, feature = "write"))]
3838
extern crate std;
3939

40+
use alloc::boxed::Box;
4041
use alloc::vec::Vec;
4142
use core::borrow::{Borrow, BorrowMut};
4243
use core::cmp;
@@ -893,6 +894,14 @@ impl<A: Array> SmallVec<A> {
893894
}
894895
}
895896

897+
/// Converts a `SmallVec` into a `Box<[T]>` without reallocating if the `SmallVec` has already spilled
898+
/// onto the heap.
899+
///
900+
/// Note that this will drop any excess capacity.
901+
pub fn into_boxed_slice(self) -> Box<[A::Item]> {
902+
self.into_vec().into_boxed_slice()
903+
}
904+
896905
/// Convert the SmallVec into an `A` if possible. Otherwise return `Err(Self)`.
897906
///
898907
/// This method returns `Err(Self)` if the SmallVec is too short (and the `A` contains uninitialized elements),

0 commit comments

Comments
 (0)