Skip to content

Commit 28bbdca

Browse files
committed
Implement SmallVec::split_off
1 parent 735b206 commit 28bbdca

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/lib.rs

+42
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,48 @@ impl<T, const N: usize> SmallVec<T, N> {
880880
self.len.on_heap(Self::is_zst())
881881
}
882882

883+
/// Splits the collection into two at the given index.
884+
///
885+
/// Returns a newly allocated vector containing the elements in the range
886+
/// `[at, len)`. After the call, the original vector will be left containing
887+
/// the elements `[0, at)` with its previous capacity unchanged.
888+
///
889+
/// - If you want to take ownership of the entire contents and capacity of
890+
/// the vector, see [`mem::take`] or [`mem::replace`].
891+
/// - If you don't need the returned vector at all, see [`SmallVec::truncate`].
892+
/// - If you want to take ownership of an arbitrary subslice, or you don't
893+
/// necessarily want to store the removed items in a vector, see [`SmallVec::drain`].
894+
///
895+
/// # Panics
896+
///
897+
/// Panics if `at > len`.
898+
///
899+
/// # Examples
900+
///
901+
/// ```
902+
/// let mut vec = vec![1, 2, 3];
903+
/// let vec2 = vec.split_off(1);
904+
/// assert_eq!(vec, [1]);
905+
/// assert_eq!(vec2, [2, 3]);
906+
/// ```
907+
#[inline]
908+
pub fn split_off(&mut self, at: usize) -> Self {
909+
let len = self.len();
910+
assert!(at <= len);
911+
912+
let other_len = len - at;
913+
let mut other = Self::with_capacity(other_len);
914+
915+
// Unsafely `set_len` and copy items to `other`.
916+
unsafe {
917+
self.set_len(at);
918+
other.set_len(other_len);
919+
920+
core::ptr::copy_nonoverlapping(self.as_ptr().add(at), other.as_mut_ptr(), other.len());
921+
}
922+
other
923+
}
924+
883925
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, N>
884926
where
885927
R: core::ops::RangeBounds<usize>,

src/tests.rs

+34
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,40 @@ fn test_truncate_references() {
280280
);
281281
}
282282

283+
#[test]
284+
fn test_split_off() {
285+
let mut vec: SmallVec<u32, 4> = smallvec![1, 2, 3, 4, 5, 6];
286+
let orig_ptr = vec.as_ptr();
287+
let orig_capacity = vec.capacity();
288+
289+
let split_off = vec.split_off(4);
290+
assert_eq!(&vec[..], &[1, 2, 3, 4]);
291+
assert_eq!(&split_off[..], &[5, 6]);
292+
assert_eq!(vec.capacity(), orig_capacity);
293+
assert_eq!(vec.as_ptr(), orig_ptr);
294+
}
295+
296+
#[test]
297+
fn test_split_off_take_all() {
298+
// Allocate enough capacity that we can tell whether the split-off vector's
299+
// capacity is based on its size, or (incorrectly) on the original capacity.
300+
let mut vec = SmallVec::<u32, 4>::with_capacity(1000);
301+
vec.extend([1, 2, 3, 4, 5, 6]);
302+
let orig_ptr = vec.as_ptr();
303+
let orig_capacity = vec.capacity();
304+
305+
let split_off = vec.split_off(0);
306+
assert_eq!(&vec[..], &[]);
307+
assert_eq!(&split_off[..], &[1, 2, 3, 4, 5, 6]);
308+
assert_eq!(vec.capacity(), orig_capacity);
309+
assert_eq!(vec.as_ptr(), orig_ptr);
310+
311+
// The split-off vector should be newly-allocated, and should not have
312+
// stolen the original vector's allocation.
313+
assert!(split_off.capacity() < orig_capacity);
314+
assert_ne!(split_off.as_ptr(), orig_ptr);
315+
}
316+
283317
#[test]
284318
fn test_insert_many() {
285319
let mut v: SmallVec<u8, 8> = SmallVec::new();

0 commit comments

Comments
 (0)