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