tiny_vec/lib.rs
1/* Copyright (C) 2025 Saúl Valdelvira
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
14
15//! Tiny Vec
16//!
17//! A dynamic array that can store a small amount of elements on the stack.
18//!
19//! This struct provides a vec-like API, but performs small-vector optimization.
20//! This means that a `TinyVec<T, N>` stores up to N elements on the stack.
21//! If the vector grows bigger than that, it moves the contents to the heap.
22//!
23//! # Example
24//! ```
25//! use tiny_vec::TinyVec;
26//!
27//! let mut tv = TinyVec::<u8, 16>::new();
28//!
29//! for n in 0..16 {
30//! tv.push(n);
31//! }
32//!
33//! // Up to this point, no heap allocations are needed.
34//! // All the elements are stored on the stack.
35//!
36//! tv.push(123); // This moves the vector to the heap
37//!
38//! assert_eq!(&tv[..], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
39//! 10, 11, 12, 13, 14, 15, 123])
40//! ```
41//!
42//! # Memory layout
43//! For a TinyVec<T, N>
44//!
45//! On the stack (length <= N)
46//! - [T; N] : Data
47//! - usize : Length
48//!
49//! On the heap (length > N)
50//! - T* : Data
51//! - usize : Capacity
52//! - usize : Length
53//!
54//! If N is equal to `sizeof (T*, usize) / sizeof T`, the
55//! TinyVec is the same size as a regular vector \
56//! NOTE: The [n_elements_for_stack] function returns the maximun
57//! number of elements for a type, such that it doesn't waste extra
58//! space when moved to the heap
59
60#![allow(incomplete_features)]
61#![cfg_attr(feature = "use-nightly-features", feature(min_specialization, slice_swap_unchecked, generic_const_exprs))]
62#![cfg_attr(feature = "use-nightly-features", feature(extend_one, extend_one_unchecked))]
63#![cfg_attr(feature = "use-nightly-features", feature(iter_advance_by))]
64
65#![no_std]
66
67extern crate alloc;
68use alloc::vec::Vec;
69use alloc::boxed::Box;
70use drain::Drain;
71use extract_if::ExtractIf;
72
73use core::marker::PhantomData;
74use core::mem::{self, ManuallyDrop, MaybeUninit};
75use core::ops::{Range, Bound, Deref, DerefMut, RangeBounds};
76use core::ptr::NonNull;
77use core::{fmt, ptr};
78use core::slice;
79
80mod raw;
81use raw::RawVec;
82
83pub mod iter;
84pub mod drain;
85pub mod extract_if;
86
87union TinyVecInner<T, const N: usize> {
88 stack: ManuallyDrop<[MaybeUninit<T>; N]>,
89 raw: RawVec<T>,
90}
91
92impl<T, const N: usize> TinyVecInner<T, N> {
93
94 #[inline(always)]
95 const unsafe fn as_ptr_stack(&self) -> *const T {
96 unsafe { &raw const self.stack as *const T }
97 }
98
99 #[inline(always)]
100 const unsafe fn as_ptr_stack_mut(&mut self) -> *mut T {
101 unsafe { &raw mut self.stack as *mut T }
102 }
103
104 #[inline(always)]
105 const unsafe fn as_ptr_heap(&self) -> *const T {
106 unsafe { self.raw.ptr.as_ptr() }
107 }
108
109 #[inline(always)]
110 const unsafe fn as_ptr_heap_mut(&mut self) -> *mut T {
111 unsafe { self.raw.ptr.as_ptr() }
112 }
113}
114
115#[repr(transparent)]
116struct Length(usize);
117
118impl Length {
119 #[inline(always)]
120 const fn new_stack(len: usize) -> Self {
121 Self(len << 1)
122 }
123
124 #[inline(always)]
125 const fn new_heap(len: usize) -> Self {
126 Self(len << 1 | 0b1)
127 }
128
129 #[inline(always)]
130 const fn is_stack(&self) -> bool {
131 (self.0 & 0b1) == 0
132 }
133
134 #[inline(always)]
135 const fn set_heap(&mut self) {
136 self.0 |= 0b1;
137 }
138
139 #[inline(always)]
140 const fn set_stack(&mut self) {
141 self.0 &= 0b0;
142 }
143
144 #[inline(always)]
145 const fn set(&mut self, n: usize) {
146 self.0 &= 0b1;
147 self.0 |= n << 1;
148 }
149
150 #[inline(always)]
151 const fn get(&self) -> usize {
152 self.0 >> 1
153 }
154
155 #[inline(always)]
156 const fn add(&mut self, n: usize) {
157 self.0 += n << 1;
158 }
159
160 #[inline(always)]
161 const fn sub(&mut self, n: usize) {
162 self.0 -= n << 1;
163 }
164}
165
166/// Macro to create [TinyVec]s
167///
168/// # Example
169/// ```
170/// use tiny_vec::{TinyVec, tinyvec};
171///
172/// // Create a TinyVec with a list of elements
173/// let v: TinyVec<_, 10> = tinyvec![1, 2, 3, 4];
174/// assert_eq!(&v[0..4], &[1, 2, 3, 4]);
175///
176/// // Create a TinyVec with 100 zeroes
177/// let v: TinyVec<_, 10> = tinyvec![0; 100];
178/// assert_eq!(v[20], 0);
179/// ```
180#[macro_export]
181macro_rules! tinyvec {
182 ($elem:expr; $n:expr) => ({
183 let mut tv = $crate::TinyVec::new();
184 tv.resize($n, $elem);
185 tv
186 });
187 ($($x:expr),*$(,)?) => ({
188 $crate::TinyVec::from(&[ $( $x ,)*])
189 });
190}
191
192/// The maximun number of elements that can be stored in the stack
193/// for the vector, without incrementing it's size
194///
195/// This means, that [`n_elements_for_stack`] for T returns the max
196/// number of elements, so that when switching to a heap allocated
197/// buffer, no stack size is wasted
198///
199/// # Examples
200/// ```
201/// use tiny_vec::n_elements_for_stack;
202///
203/// assert_eq!(n_elements_for_stack::<u8>(), 16);
204/// assert_eq!(n_elements_for_stack::<u16>(), 8);
205/// assert_eq!(n_elements_for_stack::<i32>(), 4);
206/// ```
207pub const fn n_elements_for_stack<T>() -> usize {
208 n_elements_for_bytes::<T>(mem::size_of::<RawVec<T>>())
209}
210
211/// The maximun number of elements of type T, that can be stored on
212/// the given byte size
213///
214/// # Examples
215/// ```
216/// use tiny_vec::n_elements_for_bytes;
217///
218/// assert_eq!(n_elements_for_bytes::<u8>(2), 2);
219/// assert_eq!(n_elements_for_bytes::<u16>(4), 2);
220/// assert_eq!(n_elements_for_bytes::<i32>(17), 4);
221/// ```
222pub const fn n_elements_for_bytes<T>(n: usize) -> usize {
223 n / mem::size_of::<T>()
224}
225
226fn slice_range<R>(range: R, len: usize) -> Range<usize>
227where
228 R: RangeBounds<usize>
229{
230 let start = match range.start_bound() {
231 Bound::Included(n) => *n,
232 Bound::Excluded(n) => *n + 1,
233 Bound::Unbounded => 0,
234 };
235
236 let end = match range.end_bound() {
237 Bound::Included(n) => *n + 1,
238 Bound::Excluded(n) => *n,
239 Bound::Unbounded => len,
240 };
241
242 assert!(start <= end);
243 assert!(end <= len);
244
245 Range { start, end }
246}
247
248/// A dynamic array that can store a small amount of elements on the stack.
249pub struct TinyVec<T,
250 #[cfg(not(feature = "use-nightly-features"))]
251 const N: usize,
252
253 #[cfg(feature = "use-nightly-features")]
254 const N: usize = { n_elements_for_stack::<T>() },
255> {
256 inner: TinyVecInner<T, N>,
257 len: Length,
258}
259
260impl<T, const N: usize> TinyVec<T, N> {
261
262 unsafe fn switch_to_heap(&mut self, n: usize, exact: bool) {
263 debug_assert!(self.lives_on_stack());
264
265 let mut vec = RawVec::new();
266 if exact {
267 vec.expand_if_needed_exact(0, self.len.get() + n);
268 } else {
269 vec.expand_if_needed(0, self.len.get() + n);
270 }
271 unsafe {
272 let src = self.inner.as_ptr_stack();
273 let dst = vec.ptr.as_ptr();
274 ptr::copy_nonoverlapping(src, dst, self.len());
275 self.inner.raw = vec;
276 }
277 self.len.set_heap();
278 }
279
280 unsafe fn switch_to_stack(&mut self) {
281 debug_assert!(!self.lives_on_stack());
282
283 let mut rv = unsafe { self.inner.raw };
284
285 let stack = [ const { MaybeUninit::uninit() }; N ];
286
287 unsafe {
288 let src = rv.ptr.as_ptr();
289 let dst = stack.as_ptr() as *mut T;
290 ptr::copy_nonoverlapping(src,dst,self.len());
291 rv.destroy();
292 }
293
294 self.inner.stack = ManuallyDrop::new(stack);
295 self.len.set_stack();
296 }
297
298 const unsafe fn split_at_spare_mut_with_len(&mut self) -> (&mut [T], &mut [MaybeUninit<T>], &mut Length) {
299 unsafe {
300 let len = self.len();
301 let ptr = self.as_mut_ptr();
302
303 let spare_ptr = ptr.add(len).cast::<MaybeUninit<T>>();
304 let spare_len = self.capacity() - len;
305
306 let slice = slice::from_raw_parts_mut(ptr, len);
307 let spare_slice = slice::from_raw_parts_mut(spare_ptr, spare_len);
308
309 (slice, spare_slice, &mut self.len)
310 }
311 }
312
313}
314
315impl<T, const N: usize> TinyVec<T, N> {
316
317 /// Creates a new [TinyVec]
318 pub const fn new() -> Self {
319 let stack = [ const { MaybeUninit::uninit() }; N ];
320 Self {
321 inner: TinyVecInner { stack: ManuallyDrop::new(stack) },
322 len: Length::new_stack(0),
323 }
324 }
325
326 /// Creates a new [TinyVec] with the specified initial capacity
327 pub fn with_capacity(cap: usize) -> Self {
328 let mut len = Length(0);
329 let inner = if cap <= N {
330 let s = [const { MaybeUninit::uninit() }; N];
331 TinyVecInner {
332 stack: ManuallyDrop::new(s)
333 }
334 } else {
335 len.set_heap();
336 TinyVecInner {
337 raw: RawVec::with_capacity(cap)
338 }
339 };
340
341 Self { inner, len }
342 }
343
344 /// Creates a new [TinyVec] from the given array
345 ///
346 /// # Example
347 /// ```
348 /// use tiny_vec::TinyVec;
349 ///
350 /// let tv = TinyVec::<i32, 10>::from_array([1, 2, 3, 4]);
351 ///
352 /// assert_eq!(tv.capacity(), 10);
353 /// assert!(tv.lives_on_stack());
354 /// ```
355 pub fn from_array<const M: usize>(arr: [T; M]) -> Self {
356 let arr = ManuallyDrop::new(arr);
357 let mut tv = Self::with_capacity(M);
358
359 let src = arr.as_ptr();
360 let dst = tv.as_mut_ptr();
361
362 unsafe {
363 ptr::copy(src, dst, M);
364 tv.set_len(M);
365 }
366
367 tv
368 }
369
370 /// Like [from_array](Self::from_array), but the array's length
371 /// and the TinyVec's N are equal, so we can call it on const functions.
372 ///
373 /// # Example
374 /// ```
375 /// use tiny_vec::TinyVec;
376 ///
377 /// let tv = TinyVec::from_array_eq_size([1, 2, 3, 4]);
378 ///
379 /// assert_eq!(tv.capacity(), 4);
380 /// assert!(tv.lives_on_stack());
381 /// ```
382 pub const fn from_array_eq_size(arr: [T; N]) -> Self {
383 let mut tv = Self::new();
384
385 let src = arr.as_ptr();
386 let dst = tv.as_mut_ptr();
387
388 unsafe {
389 ptr::copy(src, dst, N);
390 tv.set_len(N);
391 }
392
393 mem::forget(arr);
394 tv
395 }
396
397 /// Creates a new [TinyVec] from the given [Vec]
398 ///
399 /// The returned TinyVec will have no extra capacity.
400 /// This means that it won't reuse the Vec's buffer,
401 /// and won't allocate more that vec.len() elements.
402 ///
403 /// If the vector has less than N elements, they'll
404 /// be stored in the stack.
405 ///
406 /// If you want to reuse the Vec's buffer, use the
407 /// [from_vec_reuse_buffer](Self::from_vec_reuse_buffer) function
408 ///
409 /// # Example
410 /// ```
411 /// use tiny_vec::TinyVec;
412 ///
413 /// let vec = vec![1, 2, 3, 4, 5];
414 ///
415 /// let tv = TinyVec::<i32, 10>::from_vec(vec);
416 ///
417 /// /* vec fits on the stack, so it won't heap-allocate the TinyVec */
418 /// assert!(tv.lives_on_stack());
419 /// ```
420 pub fn from_vec(mut vec: Vec<T>) -> Self {
421 let mut tv = Self::with_capacity(vec.len());
422 let dst = tv.as_mut_ptr();
423 let src = vec.as_ptr();
424 unsafe {
425 ptr::copy(src, dst, vec.len());
426 vec.set_len(0);
427 }
428 tv
429 }
430
431 /// Like [from_vec](Self::from_vec), but it reuses the
432 /// [Vec]'s buffer.
433 ///
434 /// The returned TinyVec will have no extra capacity.
435 /// This means that it won't reuse the Vec's buffer,
436 /// and won't allocate more that vec.len() elements.
437 ///
438 /// For a version that creates a TinyVec with the mininum
439 /// capacity for this vec, check [from_vec](Self::from_vec)
440 ///
441 /// # Example
442 /// ```
443 /// use tiny_vec::TinyVec;
444 ///
445 /// let vec = vec![1, 2, 3, 4, 5];
446 ///
447 /// let tv = TinyVec::<i32, 10>::from_vec_reuse_buffer(vec);
448 ///
449 /// /* This version of from_vec, will use the same buffer vec used */
450 /// assert!(!tv.lives_on_stack());
451 /// ```
452 pub fn from_vec_reuse_buffer(vec: Vec<T>) -> Self {
453 let mut vec = ManuallyDrop::new(vec);
454
455 let ptr = vec.as_mut_ptr();
456 let ptr = unsafe { NonNull::new_unchecked(ptr) };
457
458 let len = Length::new_heap(vec.len());
459 let cap = vec.capacity();
460 let raw = RawVec { cap, ptr };
461
462 let inner = TinyVecInner { raw };
463 Self { inner, len }
464 }
465
466 /// Creates a TinyVec from a boxed slice of T
467 pub fn from_boxed_slice(boxed: Box<[T]>) -> Self {
468 let len = boxed.len();
469 let ptr = Box::into_raw(boxed);
470 let ptr = unsafe { NonNull::new_unchecked(ptr as *mut T) };
471
472 let raw = RawVec { ptr, cap: len };
473
474 Self {
475 inner: TinyVecInner { raw },
476 len: Length::new_heap(len)
477 }
478 }
479
480 /// Builds a TinyVec from a TinyVec with a different capacity generic parameter
481 ///
482 /// # Example
483 /// ```
484 /// use tiny_vec::TinyVec;
485 ///
486 /// let v1 = TinyVec::<i32, 10>::from(&[1, 2, 3, 4]);
487 ///
488 /// let v2 = TinyVec::<i32, 7>::from_tiny_vec(v1.clone());
489 /// assert!(v2.lives_on_stack());
490 ///
491 /// let v3 = TinyVec::<i32, 2>::from_tiny_vec(v1);
492 /// /* v3 must be heap-allocated, since it can only store 2 elements
493 /// on the stack, and v1 has 3*/
494 /// assert!(!v3.lives_on_stack());
495 ///
496 /// ```
497 pub fn from_tiny_vec<const M: usize>(mut vec: TinyVec<T, M>) -> Self {
498 let len = vec.len();
499 if len > N && len > M {
500 /* If the buffer must be on the heap on both src and dest,
501 * just copy the RawVec from vec to Self */
502 let tv = Self {
503 len: Length::new_heap(len),
504 inner: TinyVecInner {
505 raw: unsafe { vec.inner.raw }
506 }
507 };
508 mem::forget(vec);
509 return tv
510 }
511
512 let mut tv = Self::with_capacity(len);
513 let src = vec.as_ptr();
514 let dst = tv.as_mut_ptr();
515 unsafe {
516 /* SAFETY: src points to vec, and dst to tv. They are two different
517 * objects, so their buffers can't overap */
518 ptr::copy_nonoverlapping(src, dst, len);
519 vec.set_len(0);
520 tv.set_len(len);
521 }
522 tv
523 }
524
525 /// Creates a new [TinyVec] from the given slice.
526 ///
527 /// This function clones the elements in the slice.
528 /// If the type T is [Copy], the [from_slice_copied](Self::from_slice_copied)
529 /// function is a more optimized alternative
530 pub fn from_slice(slice: &[T]) -> Self
531 where
532 T: Clone
533 {
534 let mut v = Self::with_capacity(slice.len());
535 v.extend_from_slice_impl(slice);
536 v
537 }
538
539 /// Creates a new [TinyVec] from the given slice.
540 ///
541 /// This function copies the slice into the buffer, which
542 /// is faster that calling [clone](Clone::clone).
543 /// That's why it requires T to implement [Copy].
544 ///
545 /// For a cloning alternative, use [from_slice](Self::from_slice)
546 pub fn from_slice_copied(slice: &[T]) -> Self
547 where
548 T: Copy
549 {
550 let mut v = Self::with_capacity(slice.len());
551 v.extend_from_slice_copied(slice);
552 v
553 }
554
555 /// Returns the number of elements inside this vec
556 #[inline]
557 pub const fn len(&self) -> usize { self.len.get() }
558
559 /// Returns true if the vector is empty
560 #[inline]
561 pub const fn is_empty(&self) -> bool { self.len.get() == 0 }
562
563 /// Returns the allocated capacity for this vector
564 #[inline]
565 pub const fn capacity(&self) -> usize {
566 if self.len.is_stack() {
567 N
568 } else {
569 unsafe { self.inner.raw.cap }
570 }
571 }
572
573 /// Returns true if the vector is currently using stack memory.
574 ///
575 /// This means that [Self::len] <= `N`
576 ///
577 /// # Example
578 /// ```
579 /// use tiny_vec::TinyVec;
580 ///
581 /// let mut vec = TinyVec::<i32, 5>::new();
582 ///
583 /// for n in 0..5 {
584 /// vec.push(n)
585 /// }
586 ///
587 /// assert!(vec.lives_on_stack());
588 /// vec.push(6);
589 /// assert!(!vec.lives_on_stack());
590 /// ```
591 #[inline]
592 pub const fn lives_on_stack(&self) -> bool { self.len.is_stack() }
593
594 /// Gets a const pointer to the vec's buffer
595 #[inline]
596 pub const fn as_ptr(&self) -> *const T {
597 unsafe {
598 if self.len.is_stack() {
599 self.inner.as_ptr_stack()
600 } else {
601 self.inner.as_ptr_heap()
602 }
603 }
604 }
605
606 /// Gets a mutable pointer to the vec's buffer
607 #[inline]
608 pub const fn as_mut_ptr(&mut self) -> *mut T {
609 unsafe {
610 if self.len.is_stack() {
611 self.inner.as_ptr_stack_mut()
612 } else {
613 self.inner.as_ptr_heap_mut()
614 }
615 }
616 }
617
618 /// Gets a mutable pointer to the vec's buffer as a [NonNull]
619 #[inline]
620 pub const fn as_non_null(&mut self) -> NonNull<T> {
621 debug_assert!(!self.as_mut_ptr().is_null());
622 unsafe {
623 /* SAFETY: as_mut_ptr should never return a null ptr */
624 NonNull::new_unchecked(self.as_mut_ptr())
625 }
626 }
627
628 /// Gets a slice of the whole vector
629 #[inline]
630 pub const fn as_slice(&self) -> &[T] {
631 unsafe {
632 slice::from_raw_parts(self.as_ptr(), self.len.get())
633 }
634 }
635
636 /// Gets a mutable slice of the whole vector
637 #[inline]
638 pub const fn as_mut_slice(&mut self) -> &mut [T] {
639 unsafe {
640 slice::from_raw_parts_mut(self.as_mut_ptr(), self.len.get())
641 }
642 }
643
644 /// Reserves space for, at least, n elements
645 ///
646 /// # Example
647 /// ```
648 /// use tiny_vec::TinyVec;
649 ///
650 /// let mut vec = TinyVec::<i32, 5>::new();
651 ///
652 /// assert_eq!(vec.capacity(), 5);
653 /// assert!(vec.lives_on_stack());
654 /// vec.reserve(10);
655 /// assert!(vec.capacity() >= 10);
656 /// assert!(!vec.lives_on_stack());
657 /// ```
658 pub fn reserve(&mut self, n: usize) {
659 if self.len.is_stack() {
660 if self.len.get() + n > N {
661 unsafe { self.switch_to_heap(n, false); }
662 }
663 } else {
664 unsafe {
665 self.inner.raw.expand_if_needed(self.len.get(), n);
666 }
667 }
668 }
669
670 /// Reserves space for n more elements, but unline
671 /// [reserve](Self::reserve), this function doesn't over-allocate.
672 ///
673 /// # Example
674 /// ```
675 /// use tiny_vec::TinyVec;
676 ///
677 /// let mut vec = TinyVec::<i32, 5>::new();
678 ///
679 /// assert_eq!(vec.capacity(), 5);
680 /// assert!(vec.lives_on_stack());
681 /// vec.reserve_exact(10);
682 /// assert_eq!(vec.capacity(), 10);
683 /// assert!(!vec.lives_on_stack());
684 /// ```
685 pub fn reserve_exact(&mut self, n: usize) {
686 if self.len.is_stack() {
687 if self.len.get() + n > N {
688 unsafe { self.switch_to_heap(n, true); }
689 }
690 } else {
691 let vec = unsafe { &mut self.inner.raw };
692 let len = self.len.get();
693 let new_cap = vec.cap.max(len + n);
694 vec.expand_if_needed_exact(len, new_cap);
695 }
696 }
697
698 /// Appends an element to the back of the vector
699 /// This operation is O(1), if no resize takes place.
700 /// If the buffer needs to be resized, it has an O(n)
701 /// time complexity.
702 ///
703 /// See also: [push_within_capacity](Self::push_within_capacity)
704 ///
705 /// # Example
706 /// ```
707 /// use tiny_vec::TinyVec;
708 ///
709 /// let mut vec = TinyVec::<i32, 5>::from(&[1, 2, 3, 4]);
710 ///
711 /// vec.push(5); // No resize. O(1)
712 /// vec.push(6); // Resize, must realloc. O(n)
713 ///
714 /// assert_eq!(vec.as_slice(), &[1, 2, 3, 4, 5, 6]);
715 /// ```
716 #[inline]
717 pub fn push(&mut self, elem: T) {
718 self.reserve(1);
719 unsafe { self.push_unchecked(elem); }
720 }
721
722 /// Appends an element to the back of the vector without
723 /// checking for space.
724 ///
725 /// # Safety
726 /// The caller must ensure that there's enought capacity
727 /// for this element.
728 /// This means that [Self::len] < [Self::capacity]
729 ///
730 /// # Example
731 /// ```
732 /// use tiny_vec::TinyVec;
733 ///
734 /// let mut vec = TinyVec::<i32, 10>::with_capacity(10);
735 ///
736 /// // We've allocated a TinyVec with an initial capacity of 10.
737 /// // We can skip the bounds checking, since there will be room
738 /// // for all the elements on the iterator
739 /// for n in 0..10 {
740 /// unsafe { vec.push_unchecked(n); }
741 /// }
742 /// assert_eq!(vec.as_slice(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
743 /// ```
744 pub unsafe fn push_unchecked(&mut self, elem: T) {
745 unsafe {
746 let dst = self.as_mut_ptr().add(self.len.get());
747 dst.write(elem);
748 }
749 self.len.add(1);
750 }
751
752 /// Try to push an element inside the vector, only if
753 /// there's room for it. If the push would've caused a
754 /// reallocation of the buffer, returns the value wrapped
755 /// on an [Err] variant.
756 ///
757 /// This operation has O(1) time complexity in all cases.
758 ///
759 /// # Example
760 /// ```
761 /// use tiny_vec::TinyVec;
762 ///
763 /// let mut vec = TinyVec::<i32, 5>::from(&[1, 2, 3, 4]);
764 ///
765 /// assert!(vec.push_within_capacity(5).is_ok());
766 ///
767 /// // No room left, the value is returned
768 /// assert_eq!(vec.push_within_capacity(6), Err(6));
769 /// ```
770 pub fn push_within_capacity(&mut self, val: T) -> Result<(),T> {
771 if self.len.get() < self.capacity() {
772 unsafe { self.push_unchecked(val); }
773 Ok(())
774 } else {
775 Err(val)
776 }
777 }
778 /// Removes the last element of this vector (if present)
779 pub const fn pop(&mut self) -> Option<T> {
780 if self.len.get() == 0 {
781 None
782 } else {
783 self.len.sub(1);
784 let val = unsafe {
785 self.as_ptr()
786 .add(self.len.get())
787 .read()
788 };
789 Some(val)
790 }
791 }
792
793 /// Inserts an element in the given index position
794 ///
795 /// This operation has a worst case time complexity of O(n),
796 /// since it needs to move elements on range [index, len) one
797 /// position to the right.
798 ///
799 /// # Example
800 /// ```
801 /// use tiny_vec::TinyVec;
802 ///
803 /// let mut vec = TinyVec::<i32, 10>::from(&[1, 2, 3, 4]);
804 ///
805 /// vec.insert(2, -1);
806 /// assert_eq!(vec.as_slice(), &[1, 2, -1, 3, 4]);
807 ///
808 /// // An insert on vec.len() behaves like a push
809 /// vec.insert(vec.len(), 5);
810 /// assert_eq!(vec.as_slice(), &[1, 2, -1, 3, 4, 5]);
811 /// ```
812 pub fn insert(&mut self, index: usize, elem: T) -> Result<(),T> {
813 if index > self.len.get() {
814 return Err(elem)
815 }
816 unsafe { self.insert_unckecked(index, elem); }
817 Ok(())
818 }
819
820 /// Like [insert](Self::insert), but without bounds checking
821 ///
822 /// # Safety
823 /// The index should be <= self.len
824 pub unsafe fn insert_unckecked(&mut self, index: usize, elem: T) {
825 self.reserve(1);
826 unsafe {
827 let ptr = self.as_mut_ptr();
828 ptr::copy(
829 ptr.add(index),
830 ptr.add(index + 1),
831 self.len.get() - index,
832 );
833 ptr.add(index).write(elem);
834 }
835 self.len.add(1);
836 }
837
838 /// Inserts all the elements of the given slice into the
839 /// vector, at the given index
840 ///
841 /// This function clones the elements in the slice.
842 ///
843 /// If the type T is [Copy], the [insert_slice_copied]
844 /// function is a more optimized alternative
845 ///
846 /// # Errors
847 /// If the index is out of bounds, returns the slice as an [Err] variant
848 ///
849 /// # Example
850 /// ```
851 /// use tiny_vec::TinyVec;
852 ///
853 /// let mut vec = TinyVec::from(["abc".to_string(), "ghi".to_string()]);
854 /// vec.insert_slice(1, &[
855 /// "__".to_string(),
856 /// "def".to_string(),
857 /// "__".to_string(),
858 /// ]);
859 ///
860 /// assert_eq!(vec.as_slice(), &["abc", "__", "def", "__", "ghi"]);
861 /// ```
862 /// [insert_slice_copied]: Self::insert_slice_copied
863 #[inline]
864 pub fn insert_slice<'a>(&mut self, index: usize, elems: &'a [T]) -> Result<(), &'a [T]>
865 where
866 T: Clone
867 {
868 self.insert_slice_impl(index, elems)
869 }
870
871 /// Inserts all the elements of the given slice into the
872 /// vector, at the given index
873 ///
874 /// This function copies the slice into the buffer, which
875 /// is faster that calling [clone]
876 /// That's why it requires T to implement [Copy].
877 ///
878 /// For a cloning alternative, use [insert_slice]
879 ///
880 /// # Example
881 /// ```
882 /// use tiny_vec::TinyVec;
883 ///
884 /// let mut vec = TinyVec::from([1, 2, 3, 4]);
885 /// vec.insert_slice_copied(2, &[-1, -2, -3]);
886 /// assert_eq!(vec.as_slice(), &[1, 2, -1, -2, -3, 3, 4]);
887 /// ```
888 /// [clone]: Clone::clone
889 /// [insert_slice]: Self::insert_slice
890 pub fn insert_slice_copied<'a>(&mut self, index: usize, elems: &'a [T]) -> Result<(), &'a [T]>
891 where
892 T: Copy
893 {
894 if index > self.len() {
895 return Err(elems)
896 }
897
898 let len = elems.len();
899 self.reserve(len);
900 unsafe {
901 let ptr = self.as_mut_ptr();
902 ptr::copy(
903 ptr.add(index),
904 ptr.add(index + len),
905 self.len.get() - index,
906 );
907 ptr::copy_nonoverlapping(
908 elems.as_ptr(),
909 ptr.add(index),
910 len
911 );
912 }
913 self.len.add(len);
914
915 Ok(())
916 }
917
918 /// Inserts all the elements on the given iterator at the given index
919 ///
920 /// # Errors
921 /// If the index is out of bounds, returns the passed iterator, wrapped
922 /// on an [Err] variant.
923 ///
924 /// # Example
925 /// ```
926 /// use tiny_vec::TinyVec;
927 ///
928 /// let mut vec = TinyVec::from([1, 2, 3, 4]);
929 ///
930 /// vec.insert_iter(2, (-3..-1));
931 /// assert_eq!(vec.as_slice(), &[1, 2, -3, -2, 3, 4]);
932 /// ```
933 pub fn insert_iter<I>(&mut self, index: usize, it: I) -> Result<(), I>
934 where
935 I: IntoIterator<Item = T>,
936 <I as IntoIterator>::IntoIter: ExactSizeIterator,
937 {
938 if index > self.len() {
939 return Err(it);
940 }
941
942 let it = it.into_iter();
943 let len = it.len();
944 self.reserve(len);
945 unsafe {
946 let ptr = self.as_mut_ptr();
947 ptr::copy(
948 ptr.add(index),
949 ptr.add(index + len),
950 self.len.get() - index,
951 );
952 let mut ptr = ptr.add(index);
953 for elem in it {
954 ptr.write(elem);
955 ptr = ptr.add(1);
956 self.len.add(1);
957 }
958 }
959 Ok(())
960 }
961
962 /// Resizes the vector, cloning elem to fill any possible new slots
963 ///
964 /// If new_len < self.len, behaves like [truncate](Self::truncate)
965 ///
966 /// # Example
967 /// ```
968 /// use tiny_vec::TinyVec;
969 ///
970 /// let mut vec = TinyVec::<i32, 5>::new();
971 ///
972 /// vec.resize(5, 0);
973 /// assert_eq!(vec.len(), 5);
974 /// assert_eq!(vec.as_slice(), &[0, 0, 0, 0, 0]);
975 /// ```
976 pub fn resize(&mut self, new_len: usize, elem: T)
977 where
978 T: Clone
979 {
980 if new_len < self.len() {
981 self.truncate(new_len);
982 } else {
983 let n = new_len - self.len();
984 self.reserve(n);
985
986 unsafe {
987 let mut ptr = self.as_mut_ptr().add(self.len());
988 let len = &mut self.len;
989
990 for _ in 1..n {
991 ptr::write(ptr, elem.clone());
992 ptr = ptr.add(1);
993 len.add(1);
994 }
995
996 if n > 0 {
997 ptr::write(ptr, elem);
998 len.add(1);
999 }
1000 }
1001 }
1002 }
1003
1004 /// Resizes the vector, using the given generator closure
1005 /// to fill any possible new slots
1006 ///
1007 /// If new_len < self.len, behaves like [truncate](Self::truncate)
1008 ///
1009 /// # Example
1010 /// ```
1011 /// use tiny_vec::TinyVec;
1012 ///
1013 /// let mut v = TinyVec::<i32, 10>::new();
1014 ///
1015 /// let mut n = 0;
1016 /// v.resize_with(5, || {
1017 /// n += 1;
1018 /// n
1019 /// });
1020 ///
1021 /// assert_eq!(v.len(), 5);
1022 /// assert_eq!(v.as_slice(), &[1, 2, 3, 4, 5]);
1023 /// ```
1024 pub fn resize_with<F>(&mut self, cap: usize, mut f: F)
1025 where
1026 F: FnMut() -> T
1027 {
1028 if cap < self.len() {
1029 self.truncate(cap);
1030 } else {
1031 let n = cap - self.len();
1032 self.reserve(n);
1033
1034 unsafe {
1035 let mut ptr = self.as_mut_ptr().add(self.len());
1036 let len = &mut self.len;
1037
1038 for _ in 0..n {
1039 ptr::write(ptr, f());
1040 ptr = ptr.add(1);
1041 len.add(1);
1042 }
1043 }
1044 }
1045 }
1046
1047 /// Resizes the vector, initializing the new memory to 0
1048 ///
1049 /// # Safety
1050 /// The caller must ensure that an all-zero byte representation
1051 /// is valid for T.
1052 ///
1053 /// If [mem::zeroed] is valid for T, this function is valid too.
1054 ///
1055 /// # Example
1056 /// ```
1057 /// use tiny_vec::TinyVec;
1058 ///
1059 /// let mut v = TinyVec::<_, 10>::from(&[1, 2, 3]);
1060 ///
1061 /// /* SAFETY: i32 can be initialized to 0b0 */
1062 /// unsafe { v.resize_zeroed(8); }
1063 ///
1064 /// /* The above is the same as
1065 /// v.resize_with(8, || unsafe { core::mem::zeroed() }); */
1066 ///
1067 /// assert_eq!(v.len(), 8);
1068 /// assert_eq!(v.as_slice(), &[1, 2, 3, 0, 0, 0, 0, 0]);
1069 /// ```
1070 pub unsafe fn resize_zeroed(&mut self, cap: usize) {
1071 if cap < self.len() {
1072 self.truncate(cap);
1073 } else {
1074 let n = cap - self.len();
1075 self.reserve(n);
1076 unsafe {
1077 let ptr = self.as_mut_ptr().add(self.len());
1078 ptr.write_bytes(0, n);
1079 }
1080 self.len.add(n);
1081 }
1082 }
1083
1084 /// Like [remove](Self::remove), but without bounds checking
1085 ///
1086 /// # Safety
1087 /// index must be within bounds (less than self.len)
1088 pub const unsafe fn remove_unchecked(&mut self, index: usize) -> T {
1089 debug_assert!(index < self.len());
1090
1091 unsafe {
1092 self.len.sub(1);
1093 let result = self.as_mut_ptr().add(index).read();
1094 ptr::copy(
1095 self.as_ptr().add(index + 1),
1096 self.as_mut_ptr().add(index),
1097 self.len.get() - index,
1098 );
1099 result
1100 }
1101 }
1102
1103 /// Removes the element at the given index.
1104 /// If the index is out of bounds, returns [None]
1105 #[inline]
1106 pub const fn remove(&mut self, index: usize) -> Option<T> {
1107 if index >= self.len.get() { return None }
1108 /* SAFETY: We've just checked that index is < self.len */
1109 Some(unsafe { self.remove_unchecked(index) })
1110 }
1111
1112 /// Swaps the elements on index a and b
1113 ///
1114 /// # Errors
1115 /// If an index is out of bounds for [0, len),
1116 /// returns that index inside an [Err] variant.
1117 pub const fn swap_checked(&mut self, a: usize, b: usize) -> Result<(),usize> {
1118 if a >= self.len.get() {
1119 return Err(a)
1120 };
1121 if b >= self.len.get() {
1122 return Err(b)
1123 };
1124 /* SAFETY: a and b are in bounds */
1125 unsafe { self.swap_unchecked(a, b); }
1126 Ok(())
1127 }
1128 /// Swaps the elements on index a and b, without checking bounds
1129 ///
1130 /// # Safety
1131 /// The caller must ensure that both `a` and `b` are in bounds [0, len)
1132 /// For a checked version of this function, check [swap_checked](Self::swap_checked)
1133 #[cfg(not(feature = "use-nightly-features"))]
1134 pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
1135 unsafe {
1136 let ptr = self.as_mut_ptr();
1137 let ap = ptr.add(a);
1138 let bp = ptr.add(b);
1139 ptr::swap(ap, bp);
1140 }
1141 }
1142
1143 #[cfg(feature = "use-nightly-features")]
1144 #[inline(always)]
1145 const unsafe fn swap_unchecked(&mut self, a: usize, b: usize) {
1146 unsafe { self.as_mut_slice().swap_unchecked(a, b); }
1147 }
1148
1149 /// Removes the element at the given index by swaping it with the last one
1150 pub const fn swap_remove(&mut self, index: usize) -> Option<T> {
1151 if index >= self.len.get() {
1152 None
1153 } else if index == self.len.get() - 1 {
1154 self.pop()
1155 } else {
1156 /* SAFETY: index in < self.len */
1157 unsafe { self.swap_unchecked(index, self.len.get() - 1) }
1158 self.pop()
1159 }
1160 }
1161
1162 /// Shrinks the capacity of the vector to fit exactly it's length
1163 ///
1164 /// If the vector lives on the heap, but it's length fits inside the
1165 /// stack-allocated buffer `self.len <= N`, it deallocates the heap
1166 /// buffer and moves the contents to the stack.
1167 ///
1168 /// If you need a function that doesn't move the buffer to the stack,
1169 /// use the [shrink_to_fit_heap_only](Self::shrink_to_fit_heap_only) function.
1170 pub fn shrink_to_fit(&mut self) {
1171 if self.len.is_stack() { return }
1172
1173 /* SAFETY: It's safe to assume that we are on the heap,
1174 * because of the check above */
1175 if self.len.get() <= N {
1176 unsafe { self.switch_to_stack(); }
1177 } else {
1178 unsafe { self.inner.raw.shrink_to_fit(self.len.get()); };
1179 }
1180 }
1181
1182 /// Moves this `TinyVec` to the heap
1183 pub fn move_to_heap(&mut self) {
1184 if self.lives_on_stack() {
1185 unsafe { self.switch_to_heap(0, false) };
1186 }
1187 }
1188
1189 /// Moves this `TinyVec` to the heap, without allocating more
1190 /// than `self.len` elements
1191 pub fn move_to_heap_exact(&mut self) {
1192 if self.lives_on_stack() {
1193 unsafe { self.switch_to_heap(0, true) };
1194 }
1195 }
1196
1197 /// Shrinks the capacity of the vector to fit exactly it's length.
1198 ///
1199 /// Unlike [shrink_to_fit](Self::shrink_to_fit), this function doesn't
1200 /// move the buffer to the stack, even if the length of `self`, could
1201 /// fit on the stack space.
1202 pub fn shrink_to_fit_heap_only(&mut self) {
1203 if !self.len.is_stack() {
1204 unsafe { self.inner.raw.shrink_to_fit(self.len.get()); };
1205 }
1206 }
1207
1208 /// Clears all the elements of this vector
1209 ///
1210 /// # Example
1211 /// ```
1212 /// use tiny_vec::{TinyVec, tinyvec};
1213 ///
1214 /// let mut vec: TinyVec<_, 5> = tinyvec![1, 2, 3, 4, 5];
1215 /// vec.clear();
1216 ///
1217 /// assert!(vec.is_empty());
1218 /// assert_eq!(vec.as_slice(), &[]);
1219 /// ```
1220 pub fn clear(&mut self) {
1221 let ptr = self.as_mut_slice() as *mut [T];
1222 unsafe {
1223 self.len.set(0);
1224 ptr::drop_in_place(ptr);
1225 }
1226 }
1227
1228 /// Sets the length of the vector.
1229 ///
1230 /// # Safety
1231 /// The caller must ensure that changing the length doesn't
1232 /// leave the vector in an inconsistent state. This is:
1233 ///
1234 /// - If you reduce de length, you may leak memory, if the type
1235 /// stored need to be droped
1236 /// - If you extend the length, you may access uninitialized memory
1237 #[inline]
1238 pub const unsafe fn set_len(&mut self, len: usize) {
1239 self.len.set(len);
1240 }
1241
1242 /// Updates the length of the vector using the given closure.
1243 ///
1244 /// This is just the same as getting the len using [Self::len], and
1245 /// then using [Self::set_len].
1246 ///
1247 /// *This is a low level api*. Use it only if you know what you're doing.
1248 ///
1249 /// # Safety
1250 /// Just like [Self::set_len], you need to make sure that changing the
1251 /// vector length doesn't leave the vector in an inconsistent state, or
1252 /// leaks memory.
1253 ///
1254 /// # Example
1255 /// ```
1256 /// use tiny_vec::TinyVec;
1257 ///
1258 /// let mut vec = TinyVec::<i32, 10>::with_capacity(10);
1259 ///
1260 /// unsafe {
1261 /// let mut dst = vec.as_mut_ptr();
1262 /// let src = &[1, 2, 3, 4] as *const i32;
1263 /// core::ptr::copy(src, dst, 4);
1264 /// vec.update_len(|len| *len += 4);
1265 /// // Same as:
1266 /// // let len = vec.len();
1267 /// // len.set_len(len + 4);
1268 /// }
1269 /// ```
1270 pub unsafe fn update_len<F>(&mut self, mut f: F)
1271 where
1272 F: FnMut(&mut usize)
1273 {
1274 let mut len = self.len.get();
1275 f(&mut len);
1276 self.len.set(len);
1277 }
1278
1279 /// Reduces the length in the vector, dropping the elements
1280 /// in range [new_len, old_len)
1281 ///
1282 /// If the new_len is >= old_len, this function does nothing.
1283 ///
1284 /// # Example
1285 /// ```
1286 /// use tiny_vec::TinyVec;
1287 ///
1288 /// let mut vec = TinyVec::from([1, 2, 3, 4, 5]);
1289 /// vec.truncate(3);
1290 /// assert_eq!(vec.as_slice(), &[1, 2, 3]);
1291 /// ```
1292 pub fn truncate(&mut self, len: usize) {
1293 if len < self.len.get() {
1294 for e in &mut self[len..] {
1295 unsafe { ptr::drop_in_place(e) }
1296 }
1297 unsafe { self.set_len(len); }
1298 }
1299 }
1300
1301 /// Copies all the elements of the given slice into the vector
1302 ///
1303 /// # Example
1304 /// ```
1305 /// use tiny_vec::TinyVec;
1306 ///
1307 /// let mut vec = TinyVec::<String, 5>::new();
1308 /// vec.extend_from_slice(&[
1309 /// "abc".to_string(),
1310 /// "def".to_string(),
1311 /// "__".to_string(),
1312 /// ]);
1313 ///
1314 /// assert_eq!(vec.as_slice(), &["abc", "def", "__"]);
1315 /// ```
1316 /// [extend_from_slice_copied]: Self::extend_from_slice_copied
1317 #[inline]
1318 pub fn extend_from_slice(&mut self, s: &[T])
1319 where
1320 T: Clone
1321 {
1322 self.extend_from_slice_impl(s);
1323 }
1324
1325 /// Copies all the elements of the given slice into the vector
1326 ///
1327 /// This function copies the slice into the buffer, which
1328 /// is faster that calling [clone]
1329 /// That's why it requires T to implement [Copy].
1330 ///
1331 /// For a cloning alternative, use [extend_from_slice]
1332 ///
1333 /// # Example
1334 /// ```
1335 /// use tiny_vec::TinyVec;
1336 ///
1337 /// let mut vec = TinyVec::<i32, 5>::new();
1338 /// vec.extend_from_slice_copied(&[1, 2, 3, 4]);
1339 ///
1340 /// assert_eq!(vec.as_slice(), &[1, 2, 3, 4]);
1341 /// ```
1342 /// [clone]: Clone::clone
1343 /// [extend_from_slice]: Self::extend_from_slice
1344 pub fn extend_from_slice_copied(&mut self, s: &[T])
1345 where
1346 T: Copy
1347 {
1348 let len = s.len();
1349 let src = s.as_ptr();
1350
1351 self.reserve(len);
1352
1353 unsafe {
1354 ptr::copy(
1355 src,
1356 self.as_mut_ptr().add(self.len.get()),
1357 len
1358 )
1359 }
1360
1361 self.len.add(len);
1362 }
1363
1364 /// Copies the slice from the given range to the back
1365 /// of this vector.
1366 ///
1367 /// # Panics
1368 /// Panics if the range is invalid for [0, self.len)
1369 ///
1370 /// # Example
1371 /// ```
1372 /// use tiny_vec::TinyVec;
1373 ///
1374 /// let mut vec = TinyVec::from([1, 2, 3, 4, 5, 6, 7, 8]);
1375 ///
1376 /// vec.extend_from_within(3..=5);
1377 ///
1378 /// assert_eq!(vec, &[1, 2, 3, 4, 5, 6, 7, 8, 4, 5, 6]);
1379 /// ```
1380 #[inline]
1381 pub fn extend_from_within<R>(&mut self, range: R)
1382 where
1383 T: Clone,
1384 R: RangeBounds<usize>,
1385 {
1386 self.extend_from_within_impl(range);
1387 }
1388
1389 /// Like [extend_from_within](Self::extend_from_within),
1390 /// but optimized for [Copy] types
1391 pub fn extend_from_within_copied<R>(&mut self, range: R)
1392 where
1393 T: Copy,
1394 R: RangeBounds<usize>,
1395 {
1396 let len = self.len();
1397 let Range { start, end } = slice_range(range, len);
1398
1399 let new_len = end - start;
1400 self.reserve(new_len);
1401
1402 let ptr = self.as_mut_ptr();
1403 unsafe {
1404 let src = ptr.add(start);
1405 let dst = ptr.add(len);
1406 ptr::copy(src, dst, new_len);
1407 }
1408 self.len.add(new_len);
1409 }
1410
1411 /// Retains in this vector only the elements that match
1412 /// the given predicate
1413 ///
1414 /// This is the same as calling
1415 /// `self.extract_if(.., |e| !pred(e)).for_each(|e| drop(e))`
1416 ///
1417 /// # Example
1418 /// ```
1419 /// use tiny_vec::TinyVec;
1420 ///
1421 /// let mut vec = TinyVec::from([1, 2, 3, 4, 5, 6, 7, 8]);
1422 /// vec.retain(|&n| n % 2 == 0);
1423 /// assert_eq!(vec, &[2, 4, 6, 8]);
1424 /// ```
1425 pub fn retain<F>(&mut self, mut pred: F)
1426 where
1427 F: FnMut(&T) -> bool
1428 {
1429 self.retain_mut(|e| pred(e));
1430 }
1431
1432 /// Like [retain](Self::retain), but the predicate receives a
1433 /// mutable reference to the element.
1434 ///
1435 /// # Example
1436 /// ```
1437 /// use tiny_vec::TinyVec;
1438 ///
1439 /// let mut vec = TinyVec::from([1, 2, 3, 4, 5, 6, 7, 8]);
1440 /// vec.retain_mut(|n| {
1441 /// let is_even = *n % 2 == 0;
1442 /// *n *= 2;
1443 /// is_even
1444 /// });
1445 /// assert_eq!(vec, &[4, 8, 12, 16]);
1446 /// ```
1447 pub fn retain_mut<F>(&mut self, mut pred: F)
1448 where
1449 F: FnMut(&mut T) -> bool
1450 {
1451 /* TODO: We can probably optimize this */
1452 for e in self.extract_if(.., |e| !pred(e)) {
1453 drop(e)
1454 }
1455 }
1456
1457 /// Returns vector content as a slice of `T`, along with the remaining spare
1458 /// capacity of the vector as a slice of `MaybeUninit<T>`.
1459 ///
1460 /// The returned spare capacity slice can be used to fill the vector with data
1461 /// (e.g. by reading from a file) before marking the data as initialized using
1462 /// the [`set_len`], or [`update_len`] methods.
1463 ///
1464 /// [`set_len`]: TinyVec::set_len
1465 /// [`update_len`]: TinyVec::update_len
1466 ///
1467 /// Note that this is a low-level API, which should be used with care for
1468 /// optimization purposes. If you need to append data to a `Vec`
1469 /// you can use [`push`], [`extend`], [`extend_from_slice`],
1470 /// [`extend_from_within`], [`insert`], [`append`], [`resize`] or
1471 /// [`resize_with`], depending on your exact needs.
1472 ///
1473 /// [`push`]: TinyVec::push
1474 /// [`extend`]: TinyVec::extend
1475 /// [`extend_from_slice`]: TinyVec::extend_from_slice
1476 /// [`extend_from_within`]: TinyVec::extend_from_within
1477 /// [`insert`]: TinyVec::insert
1478 /// [`append`]: TinyVec::append
1479 /// [`resize`]: TinyVec::resize
1480 /// [`resize_with`]: TinyVec::resize_with
1481 ///
1482 /// # Examples
1483 ///
1484 /// ```
1485 /// use tiny_vec::TinyVec;
1486 ///
1487 /// let mut v = TinyVec::from([1, 1, 2]);
1488 ///
1489 /// // Reserve additional space big enough for 10 elements.
1490 /// v.reserve(10);
1491 ///
1492 /// let (init, uninit) = v.split_at_spare_mut();
1493 /// let sum = init.iter().copied().sum::<u32>();
1494 ///
1495 /// // Fill in the next 4 elements.
1496 /// uninit[0].write(sum);
1497 /// uninit[1].write(sum * 2);
1498 /// uninit[2].write(sum * 3);
1499 /// uninit[3].write(sum * 4);
1500 ///
1501 /// // Mark the 4 elements of the vector as being initialized.
1502 /// unsafe {
1503 /// let len = v.len();
1504 /// v.set_len(len + 4);
1505 /// }
1506 ///
1507 /// assert_eq!(&v, &[1, 1, 2, 4, 8, 12, 16]);
1508 /// ```
1509 pub const fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]) {
1510 let (init, uninit, _) = unsafe { self.split_at_spare_mut_with_len() };
1511 (init, uninit)
1512 }
1513
1514 /// Splits the collection into two at the given index.
1515 ///
1516 /// Returns a newly allocated vector containing the elements in the range
1517 /// `[at, len)`. After the call, the original vector will be left containing
1518 /// the elements `[0, at)` with its previous capacity unchanged.
1519 ///
1520 /// - If you want to take ownership of the entire contents and capacity of
1521 /// the vector, see [`mem::take`] or [`mem::replace`].
1522 /// - If you don't need the returned vector at all, see [`TinyVec::truncate`].
1523 /// - If you want to take ownership of an arbitrary subslice, or you don't
1524 /// necessarily want to store the removed items in a vector, see [`TinyVec::drain`].
1525 ///
1526 /// # Panics
1527 ///
1528 /// Panics if `at > len`.
1529 ///
1530 /// # Examples
1531 ///
1532 /// ```
1533 /// use tiny_vec::TinyVec;
1534 ///
1535 /// let mut vec = TinyVec::from(['a', 'b', 'c']);
1536 /// let vec2 = vec.split_off(1);
1537 /// assert_eq!(vec, ['a']);
1538 /// assert_eq!(vec2, ['b', 'c']);
1539 /// ```
1540 pub fn split_off(&mut self, at: usize) -> TinyVec<T , N> {
1541 if at >= self.len() {
1542 panic!("Index out of bounds");
1543 }
1544 let other_len = self.len() - at;
1545 let mut other = TinyVec::<T, N>::with_capacity(other_len);
1546
1547 unsafe {
1548 let src = self.as_ptr().add(at);
1549 let dst = other.as_mut_ptr();
1550 ptr::copy_nonoverlapping(src, dst, other_len);
1551 other.set_len(other_len);
1552 self.len.sub(other_len);
1553 }
1554 other
1555 }
1556
1557 /// Consumes and leaks the `TinyVec`, returning a mutable reference to the contents,
1558 /// `&'a mut [T]`.
1559 ///
1560 /// Note that the type `T` must outlive the chosen lifetime `'a`. If the type
1561 /// has only static references, or none at all, then this may be chosen to be
1562 /// `'static`.
1563 ///
1564 /// This method shrinks the buffer, and moves it to the heap in case it lived
1565 /// on the stack.
1566 ///
1567 /// This function is mainly useful for data that lives for the remainder of
1568 /// the program's life. Dropping the returned reference will cause a memory
1569 /// leak.
1570 ///
1571 /// # Examples
1572 ///
1573 /// ```
1574 /// let x = tiny_vec::TinyVec::from([1, 2, 3]);
1575 ///
1576 /// let static_ref: &'static mut [usize] = x.leak();
1577 /// static_ref[0] += 1;
1578 ///
1579 /// assert_eq!(static_ref, &[2, 2, 3]);
1580 /// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
1581 /// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
1582 /// # drop(unsafe { Box::from_raw(static_ref) });
1583 /// ```
1584 pub fn leak<'a>(self) -> &'a mut [T]
1585 where
1586 T: 'a
1587 {
1588 let mut slf = ManuallyDrop::new(self);
1589 unsafe {
1590 let len = slf.len();
1591 slf.shrink_to_fit_heap_only();
1592 slf.move_to_heap_exact();
1593 let ptr = slf.as_mut_ptr();
1594 slice::from_raw_parts_mut(ptr, len)
1595 }
1596 }
1597
1598 /// Moves all the elements of `other` into `self`, leaving `other` empty.
1599 ///
1600 /// # Panics
1601 ///
1602 /// Panics if the new capacity exceeds `isize::MAX` _bytes_.
1603 ///
1604 /// # Examples
1605 ///
1606 /// ```
1607 /// use tiny_vec::TinyVec;
1608 ///
1609 /// let mut vec = TinyVec::from([1, 2, 3]);
1610 /// let mut vec2 = TinyVec::from([4, 5, 6]);
1611 /// vec.append(&mut vec2);
1612 /// assert_eq!(vec, [1, 2, 3, 4, 5, 6]);
1613 /// assert_eq!(vec2, []);
1614 /// ```
1615 pub fn append<const M: usize>(&mut self, other: &mut TinyVec<T, M>) {
1616 unsafe {
1617 let other_len = other.len();
1618 self.reserve(other_len);
1619
1620 let src = other.as_slice().as_ptr();
1621 let dst = self.as_mut_ptr().add(self.len());
1622 ptr::copy(src, dst, other_len);
1623
1624 other.set_len(0);
1625 self.len.add(other_len);
1626 }
1627 }
1628
1629 /// Removes the subslice indicated by the given range from the vector,
1630 /// returning a double-ended iterator over the removed subslice.
1631 ///
1632 /// If the iterator is dropped before being fully consumed,
1633 /// it drops the remaining removed elements.
1634 ///
1635 /// # Panics
1636 ///
1637 /// Panics if the starting point is greater than the end point or if
1638 /// the end point is greater than the length of the vector.
1639 ///
1640 /// # Leaking
1641 ///
1642 /// If the returned iterator goes out of scope without being dropped (due to
1643 /// [`mem::forget`], for example), the vector may have lost and leaked
1644 /// elements arbitrarily, including elements outside the range.
1645 ///
1646 /// # Examples
1647 ///
1648 /// ```
1649 /// use tiny_vec::{tinyvec, TinyVec};
1650 /// let mut v: TinyVec<_, 10> = tinyvec![0, 1, 2, 3, 4, 5, 6];
1651 /// let mut drain = v.drain(2..=4);
1652 /// assert_eq!(drain.next(), Some(2));
1653 /// assert_eq!(drain.next(), Some(3));
1654 /// assert_eq!(drain.next(), Some(4));
1655 /// assert_eq!(drain.next(), None);
1656 /// drop(drain);
1657 ///
1658 /// assert_eq!(v, &[0, 1, 5, 6]);
1659 ///
1660 /// // A full range clears the vector, like `clear()` does
1661 /// v.drain(..);
1662 /// assert_eq!(v, &[]);
1663 /// ```
1664 pub fn drain<R>(&mut self, range: R) -> Drain<T, N>
1665 where
1666 R: RangeBounds<usize>
1667 {
1668
1669 let len = self.len();
1670 let Range { start, end } = slice_range(range, len);
1671
1672 unsafe {
1673 self.set_len(start);
1674
1675 Drain {
1676 vec: NonNull::new_unchecked(self as *mut _),
1677 remaining_start: start,
1678 remaining_len: end - start,
1679 tail_start: end,
1680 tail_len: len - end,
1681 _marker: PhantomData,
1682 }
1683 }
1684 }
1685
1686 /// Creates an iterator which uses a closure to determine if the element in the range should be removed.
1687 ///
1688 /// If the closure returns true, then the element is removed and yielded.
1689 /// If the closure returns false, the element will remain in the vector and will not be yielded
1690 /// by the iterator.
1691 ///
1692 /// Only elements that fall in the provided range are considered for extraction, but any elements
1693 /// after the range will still have to be moved if any element has been extracted.
1694 ///
1695 /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
1696 /// or the iteration short-circuits, then the remaining elements will be retained.
1697 ///
1698 /// Note that `extract_if` also lets you mutate the elements passed to the filter closure,
1699 /// regardless of whether you choose to keep or remove them.
1700 ///
1701 /// # Panics
1702 ///
1703 /// If `range` is out of bounds.
1704 ///
1705 /// # Examples
1706 ///
1707 /// Splitting an array into evens and odds, reusing the original allocation:
1708 ///
1709 /// ```
1710 /// use tiny_vec::TinyVec;
1711 /// let mut numbers = TinyVec::<i32, 10>::from(&[1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]);
1712 ///
1713 /// let evens = numbers.extract_if(.., |x| *x % 2 == 0).collect::<TinyVec<_, 8>>();
1714 /// let odds = numbers;
1715 ///
1716 /// assert_eq!(evens, &[2, 4, 6, 8, 14]);
1717 /// assert_eq!(odds, &[1, 3, 5, 9, 11, 13, 15]);
1718 /// ```
1719 ///
1720 /// Using the range argument to only process a part of the vector:
1721 ///
1722 /// ```
1723 /// use tiny_vec::TinyVec;
1724 /// let mut items = TinyVec::<i32, 10>::from(&[0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 1, 2]);
1725 /// let ones = items.extract_if(7.., |x| *x == 1).collect::<TinyVec<_, 4>>();
1726 /// assert_eq!(items, vec![0, 0, 0, 0, 0, 0, 0, 2, 2, 2]);
1727 /// assert_eq!(ones.len(), 3);
1728 /// ```
1729 pub fn extract_if<R, F>(&mut self, range: R, pred: F) -> ExtractIf<'_, T, N, F>
1730 where
1731 R: RangeBounds<usize>,
1732 F: FnMut(&mut T) -> bool,
1733 {
1734 let len = self.len();
1735 let Range { start, end } = slice_range(range, len);
1736
1737 unsafe { self.set_len(start) }
1738
1739 ExtractIf {
1740 original_len: len,
1741 deleted: 0,
1742 next: start,
1743 last: end,
1744 vec: self,
1745 pred,
1746 }
1747 }
1748
1749 /// Converts this [TinyVec] into a boxed slice
1750 ///
1751 /// # Example
1752 /// ```
1753 /// use tiny_vec::TinyVec;
1754 ///
1755 /// let mut v = TinyVec::<_, 10>::from(&[1, 2, 3, 4]);
1756 /// let b = v.into_boxed_slice();
1757 ///
1758 /// assert_eq!(&b[..], [1, 2, 3, 4]);
1759 /// ```
1760 pub fn into_boxed_slice(self) -> Box<[T]> {
1761 let mut slf = ManuallyDrop::new(self);
1762
1763 if slf.lives_on_stack() {
1764 unsafe { slf.switch_to_heap(0, true) };
1765 }
1766 debug_assert!(!slf.lives_on_stack());
1767
1768 let len = slf.len();
1769 slf.shrink_to_fit_heap_only();
1770 debug_assert_eq!(len, slf.capacity());
1771
1772 let ptr = slf.as_mut_ptr();
1773 unsafe {
1774 let slice = slice::from_raw_parts_mut(ptr, len);
1775 Box::from_raw(slice)
1776 }
1777 }
1778
1779 /// Converts this [TinyVec] into a standard [Vec]
1780 ///
1781 /// # Example
1782 /// ```
1783 /// use tiny_vec::TinyVec;
1784 ///
1785 /// let mut v = TinyVec::from([1, 2, 3, 4]);
1786 /// let b = v.into_vec();
1787 ///
1788 /// assert_eq!(&b[..], &[1, 2, 3, 4]);
1789 /// ```
1790 pub fn into_vec(self) -> Vec<T> {
1791 let mut vec = ManuallyDrop::new(self);
1792 vec.move_to_heap();
1793
1794 let ptr = vec.as_mut_ptr();
1795 let len = vec.len();
1796 let cap = vec.capacity();
1797
1798 unsafe { Vec::from_raw_parts(ptr, len, cap) }
1799 }
1800}
1801
1802impl<T, const N: usize> Extend<T> for TinyVec<T, N> {
1803 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
1804 let iter = iter.into_iter();
1805
1806 let (min, max) = iter.size_hint();
1807 let reserve = match max {
1808 Some(max) => max,
1809 None => min,
1810 };
1811
1812 if reserve > 0 {
1813 self.reserve(reserve);
1814 }
1815
1816 for elem in iter {
1817 unsafe { self.push_unchecked(elem); }
1818 }
1819 }
1820
1821 #[cfg(feature = "use-nightly-features")]
1822 fn extend_one(&mut self, item: T) {
1823 self.push(item);
1824 }
1825
1826 #[cfg(feature = "use-nightly-features")]
1827 fn extend_reserve(&mut self, additional: usize) {
1828 self.reserve(additional);
1829 }
1830
1831 #[cfg(feature = "use-nightly-features")]
1832 unsafe fn extend_one_unchecked(&mut self, item: T) {
1833 /* SAFETY: The caller guarantees that self.len < self.capacity */
1834 unsafe { self.push_unchecked(item); }
1835 }
1836}
1837
1838#[cfg(feature = "use-nightly-features")]
1839macro_rules! maybe_default {
1840 ($($t:tt)*) => {
1841 default $($t)*
1842 };
1843}
1844
1845#[cfg(not(feature = "use-nightly-features"))]
1846macro_rules! maybe_default {
1847 ($($t:tt)*) => {
1848 $($t)*
1849 };
1850}
1851
1852trait CopyOptimization<T> {
1853 fn extend_from_slice_impl(&mut self, s: &[T]);
1854 fn insert_slice_impl<'a>(&mut self, index: usize, elems: &'a [T]) -> Result<(), &'a [T]>;
1855 fn extend_from_within_impl<R>(&mut self, range: R)
1856 where
1857 R: RangeBounds<usize>;
1858}
1859
1860impl<T: Clone, const N: usize> CopyOptimization<T> for TinyVec<T, N> {
1861 maybe_default! {
1862 fn extend_from_slice_impl(&mut self, s: &[T]) {
1863 self.extend(s.iter().cloned());
1864 }
1865 }
1866
1867 maybe_default! {
1868 fn insert_slice_impl<'a>(&mut self, index: usize, elems: &'a [T]) -> Result<(), &'a [T]> {
1869 self.insert_iter(index, elems.iter().cloned()).map_err(|_| elems)
1870 }
1871 }
1872
1873 maybe_default! {
1874 fn extend_from_within_impl<R>(&mut self, range: R)
1875 where
1876 R: RangeBounds<usize>
1877 {
1878 let len = self.len();
1879 let Range { start, end } = slice_range(range, len);
1880
1881 self.reserve(end - start);
1882
1883 let (slice, spare, len) = unsafe { self.split_at_spare_mut_with_len() };
1884 let slice = &slice[start..end];
1885
1886 for (src, dst) in slice.iter().zip(spare.iter_mut()) {
1887 dst.write(src.clone());
1888 len.add(1);
1889 }
1890 }
1891 }
1892}
1893
1894#[cfg(feature = "use-nightly-features")]
1895impl<T: Copy, const N: usize> CopyOptimization<T> for TinyVec<T, N> {
1896
1897 #[inline]
1898 fn extend_from_slice_impl(&mut self, s: &[T]) {
1899 self.extend_from_slice_copied(s);
1900 }
1901
1902 #[inline]
1903 fn insert_slice_impl<'a>(&mut self, index: usize, elems: &'a [T]) -> Result<(), &'a [T]> {
1904 self.insert_slice_copied(index, elems)
1905 }
1906
1907 #[inline]
1908 fn extend_from_within_impl<R>(&mut self, range: R)
1909 where
1910 R: RangeBounds<usize>
1911 {
1912 self.extend_from_within_copied(range);
1913 }
1914
1915}
1916
1917impl<T, const N: usize> Default for TinyVec<T, N> {
1918 #[inline]
1919 fn default() -> Self {
1920 Self::new()
1921 }
1922}
1923
1924impl<T: fmt::Debug, const N: usize> fmt::Debug for TinyVec<T, N> {
1925 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1926 fmt::Debug::fmt(self.as_slice(), f)
1927 }
1928}
1929
1930impl<T: PartialEq, const N: usize, S> PartialEq<S> for TinyVec<T, N>
1931where
1932 S: AsRef<[T]>,
1933{
1934 fn eq(&self, other: &S) -> bool {
1935 self.as_slice() == other.as_ref()
1936 }
1937}
1938
1939impl<T: PartialEq, const N: usize> Eq for TinyVec<T, N> {}
1940
1941impl<T, const N: usize> Deref for TinyVec<T, N> {
1942 type Target = [T];
1943
1944 #[inline]
1945 fn deref(&self) -> &Self::Target {
1946 self.as_slice()
1947 }
1948}
1949
1950impl<T, const N: usize> DerefMut for TinyVec<T, N> {
1951 #[inline]
1952 fn deref_mut(&mut self) -> &mut Self::Target {
1953 self.as_mut_slice()
1954 }
1955}
1956
1957impl<T, const N: usize> Drop for TinyVec<T, N> {
1958 fn drop(&mut self) {
1959 if mem::needs_drop::<T>() {
1960 for e in self.as_mut_slice() {
1961 unsafe { ptr::drop_in_place(e) };
1962 }
1963 }
1964 if !self.len.is_stack() {
1965 unsafe { self.inner.raw.destroy(); }
1966 }
1967 }
1968}
1969
1970macro_rules! impl_from_call {
1971 ($( $({$($im:tt)*})? $(where { $($w:tt)* })? $t:ty => $c:ident ),* $(,)?) => {
1972 $(
1973 impl<T, const N: usize, $($($im)*)?> From<$t> for TinyVec<T, N>
1974 $(where $($w)* )?
1975 {
1976 fn from(value: $t) -> Self {
1977 Self:: $c (value)
1978 }
1979 }
1980 )*
1981 };
1982}
1983
1984impl_from_call! {
1985 Vec<T> => from_vec,
1986 Box<[T]> => from_boxed_slice,
1987 [T; N] => from_array_eq_size,
1988
1989 where { T: Clone } &[T] => from_slice,
1990 where { T: Clone } &mut [T] => from_slice,
1991
1992 { const M: usize } where { T: Clone } &[T; M] => from_slice,
1993 { const M: usize } where { T: Clone } &mut [T; M] => from_slice,
1994}
1995
1996impl<T, const N: usize> FromIterator<T> for TinyVec<T, N> {
1997 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1998 let iter = iter.into_iter();
1999 let cap = match iter.size_hint() {
2000 (_, Some(max)) => max,
2001 (n, None) => n,
2002 };
2003 let mut vec = Self::with_capacity(cap);
2004 for elem in iter {
2005 unsafe { vec.push_unchecked(elem) };
2006 }
2007 vec
2008 }
2009}
2010
2011impl<T, const N: usize> From<TinyVec<T, N>> for Vec<T> {
2012 #[inline]
2013 fn from(value: TinyVec<T, N>) -> Self {
2014 value.into_vec()
2015 }
2016}
2017
2018impl<T, const N: usize> AsRef<[T]> for TinyVec<T, N> {
2019 #[inline]
2020 fn as_ref(&self) -> &[T] {
2021 self.as_slice()
2022 }
2023}
2024
2025impl<T, const N: usize> AsMut<[T]> for TinyVec<T, N> {
2026 #[inline]
2027 fn as_mut(&mut self) -> &mut [T] {
2028 self.as_mut_slice()
2029 }
2030}
2031
2032impl<T: Clone, const N: usize> Clone for TinyVec<T, N> {
2033 fn clone(&self) -> Self {
2034 Self::from_slice(self.as_slice())
2035 }
2036}
2037
2038#[cfg(test)]
2039mod test;