1 // SPDX-License-Identifier: GPL-2.0
3 //! Implementation of [`Vec`].
6 allocator::{KVmalloc, Kmalloc, Vmalloc},
8 AllocError, Allocator, Box, Flags,
13 mem::{ManuallyDrop, MaybeUninit},
24 /// Create a [`KVec`] containing the arguments.
26 /// New memory is allocated with `GFP_KERNEL`.
31 /// let mut v = kernel::kvec![];
32 /// v.push(1, GFP_KERNEL)?;
33 /// assert_eq!(v, [1]);
35 /// let mut v = kernel::kvec![1; 3]?;
36 /// v.push(4, GFP_KERNEL)?;
37 /// assert_eq!(v, [1, 1, 1, 4]);
39 /// let mut v = kernel::kvec![1, 2, 3]?;
40 /// v.push(4, GFP_KERNEL)?;
41 /// assert_eq!(v, [1, 2, 3, 4]);
43 /// # Ok::<(), Error>(())
48 $crate::alloc::KVec::new()
50 ($elem:expr; $n:expr) => (
51 $crate::alloc::KVec::from_elem($elem, $n, GFP_KERNEL)
53 ($($x:expr),+ $(,)?) => (
54 match $crate::alloc::KBox::new_uninit(GFP_KERNEL) {
55 Ok(b) => Ok($crate::alloc::KVec::from($crate::alloc::KBox::write(b, [$($x),+]))),
61 /// The kernel's [`Vec`] type.
63 /// A contiguous growable array type with contents allocated with the kernel's allocators (e.g.
64 /// [`Kmalloc`], [`Vmalloc`] or [`KVmalloc`]), written `Vec<T, A>`.
66 /// For non-zero-sized values, a [`Vec`] will use the given allocator `A` for its allocation. For
67 /// the most common allocators the type aliases [`KVec`], [`VVec`] and [`KVVec`] exist.
69 /// For zero-sized types the [`Vec`]'s pointer must be `dangling_mut::<T>`; no memory is allocated.
71 /// Generally, [`Vec`] consists of a pointer that represents the vector's backing buffer, the
72 /// capacity of the vector (the number of elements that currently fit into the vector), its length
73 /// (the number of elements that are currently stored in the vector) and the `Allocator` type used
74 /// to allocate (and free) the backing buffer.
76 /// A [`Vec`] can be deconstructed into and (re-)constructed from its previously named raw parts
77 /// and manually modified.
79 /// [`Vec`]'s backing buffer gets, if required, automatically increased (re-allocated) when elements
80 /// are added to the vector.
84 /// - `self.ptr` is always properly aligned and either points to memory allocated with `A` or, for
85 /// zero-sized types, is a dangling, well aligned pointer.
87 /// - `self.len` always represents the exact number of elements stored in the vector.
89 /// - `self.layout` represents the absolute number of elements that can be stored within the vector
90 /// without re-allocation. For ZSTs `self.layout`'s capacity is zero. However, it is legal for the
91 /// backing buffer to be larger than `layout`.
93 /// - The `Allocator` type `A` of the vector is the exact same `Allocator` type the backing buffer
94 /// was allocated with (and must be freed with).
95 pub struct Vec<T, A: Allocator> {
97 /// Represents the actual buffer size as `cap` times `size_of::<T>` bytes.
99 /// Note: This isn't quite the same as `Self::capacity`, which in contrast returns the number of
100 /// elements we can still store without reallocating.
101 layout: ArrayLayout<T>,
106 /// Type alias for [`Vec`] with a [`Kmalloc`] allocator.
111 /// let mut v = KVec::new();
112 /// v.push(1, GFP_KERNEL)?;
113 /// assert_eq!(&v, &[1]);
115 /// # Ok::<(), Error>(())
117 pub type KVec<T> = Vec<T, Kmalloc>;
119 /// Type alias for [`Vec`] with a [`Vmalloc`] allocator.
124 /// let mut v = VVec::new();
125 /// v.push(1, GFP_KERNEL)?;
126 /// assert_eq!(&v, &[1]);
128 /// # Ok::<(), Error>(())
130 pub type VVec<T> = Vec<T, Vmalloc>;
132 /// Type alias for [`Vec`] with a [`KVmalloc`] allocator.
137 /// let mut v = KVVec::new();
138 /// v.push(1, GFP_KERNEL)?;
139 /// assert_eq!(&v, &[1]);
141 /// # Ok::<(), Error>(())
143 pub type KVVec<T> = Vec<T, KVmalloc>;
145 // SAFETY: `Vec` is `Send` if `T` is `Send` because `Vec` owns its elements.
146 unsafe impl<T, A> Send for Vec<T, A>
153 // SAFETY: `Vec` is `Sync` if `T` is `Sync` because `Vec` owns its elements.
154 unsafe impl<T, A> Sync for Vec<T, A>
166 const fn is_zst() -> bool {
167 core::mem::size_of::<T>() == 0
170 /// Returns the number of elements that can be stored within the vector without allocating
171 /// additional memory.
172 pub fn capacity(&self) -> usize {
173 if const { Self::is_zst() } {
180 /// Returns the number of elements stored within the vector.
182 pub fn len(&self) -> usize {
186 /// Forcefully sets `self.len` to `new_len`.
190 /// - `new_len` must be less than or equal to [`Self::capacity`].
191 /// - If `new_len` is greater than `self.len`, all elements within the interval
192 /// [`self.len`,`new_len`) must be initialized.
194 pub unsafe fn set_len(&mut self, new_len: usize) {
195 debug_assert!(new_len <= self.capacity());
199 /// Returns a slice of the entire vector.
201 pub fn as_slice(&self) -> &[T] {
205 /// Returns a mutable slice of the entire vector.
207 pub fn as_mut_slice(&mut self) -> &mut [T] {
211 /// Returns a mutable raw pointer to the vector's backing buffer, or, if `T` is a ZST, a
212 /// dangling raw pointer.
214 pub fn as_mut_ptr(&mut self) -> *mut T {
218 /// Returns a raw pointer to the vector's backing buffer, or, if `T` is a ZST, a dangling raw
221 pub fn as_ptr(&self) -> *const T {
225 /// Returns `true` if the vector contains no elements, `false` otherwise.
230 /// let mut v = KVec::new();
231 /// assert!(v.is_empty());
233 /// v.push(1, GFP_KERNEL);
234 /// assert!(!v.is_empty());
237 pub fn is_empty(&self) -> bool {
241 /// Creates a new, empty `Vec<T, A>`.
243 /// This method does not allocate by itself.
245 pub const fn new() -> Self {
246 // INVARIANT: Since this is a new, empty `Vec` with no backing memory yet,
247 // - `ptr` is a properly aligned dangling pointer for type `T`,
248 // - `layout` is an empty `ArrayLayout` (zero capacity)
249 // - `len` is zero, since no elements can be or have been stored,
250 // - `A` is always valid.
252 ptr: NonNull::dangling(),
253 layout: ArrayLayout::empty(),
255 _p: PhantomData::<A>,
259 /// Returns a slice of `MaybeUninit<T>` for the remaining spare capacity of the vector.
260 pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
262 // - `self.len` is smaller than `self.capacity` and hence, the resulting pointer is
263 // guaranteed to be part of the same allocated object.
264 // - `self.len` can not overflow `isize`.
265 let ptr = unsafe { self.as_mut_ptr().add(self.len) } as *mut MaybeUninit<T>;
267 // SAFETY: The memory between `self.len` and `self.capacity` is guaranteed to be allocated
268 // and valid, but uninitialized.
269 unsafe { slice::from_raw_parts_mut(ptr, self.capacity() - self.len) }
272 /// Appends an element to the back of the [`Vec`] instance.
277 /// let mut v = KVec::new();
278 /// v.push(1, GFP_KERNEL)?;
279 /// assert_eq!(&v, &[1]);
281 /// v.push(2, GFP_KERNEL)?;
282 /// assert_eq!(&v, &[1, 2]);
283 /// # Ok::<(), Error>(())
285 pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {
286 self.reserve(1, flags)?;
289 // - `self.len` is smaller than `self.capacity` and hence, the resulting pointer is
290 // guaranteed to be part of the same allocated object.
291 // - `self.len` can not overflow `isize`.
292 let ptr = unsafe { self.as_mut_ptr().add(self.len) };
295 // - `ptr` is properly aligned and valid for writes.
296 unsafe { core::ptr::write(ptr, v) };
298 // SAFETY: We just initialised the first spare entry, so it is safe to increase the length
299 // by 1. We also know that the new length is <= capacity because of the previous call to
301 unsafe { self.set_len(self.len() + 1) };
305 /// Creates a new [`Vec`] instance with at least the given capacity.
310 /// let v = KVec::<u32>::with_capacity(20, GFP_KERNEL)?;
312 /// assert!(v.capacity() >= 20);
313 /// # Ok::<(), Error>(())
315 pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError> {
316 let mut v = Vec::new();
318 v.reserve(capacity, flags)?;
323 /// Creates a `Vec<T, A>` from a pointer, a length and a capacity using the allocator `A`.
328 /// let mut v = kernel::kvec![1, 2, 3]?;
329 /// v.reserve(1, GFP_KERNEL)?;
331 /// let (mut ptr, mut len, cap) = v.into_raw_parts();
333 /// // SAFETY: We've just reserved memory for another element.
334 /// unsafe { ptr.add(len).write(4) };
337 /// // SAFETY: We only wrote an additional element at the end of the `KVec`'s buffer and
338 /// // correspondingly increased the length of the `KVec` by one. Otherwise, we construct it
339 /// // from the exact same raw parts.
340 /// let v = unsafe { KVec::from_raw_parts(ptr, len, cap) };
342 /// assert_eq!(v, [1, 2, 3, 4]);
344 /// # Ok::<(), Error>(())
351 /// - `ptr` must be a dangling, well aligned pointer.
355 /// - `ptr` must have been allocated with the allocator `A`.
356 /// - `ptr` must satisfy or exceed the alignment requirements of `T`.
357 /// - `ptr` must point to memory with a size of at least `size_of::<T>() * capacity` bytes.
358 /// - The allocated size in bytes must not be larger than `isize::MAX`.
359 /// - `length` must be less than or equal to `capacity`.
360 /// - The first `length` elements must be initialized values of type `T`.
362 /// It is also valid to create an empty `Vec` passing a dangling pointer for `ptr` and zero for
364 pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
365 let layout = if Self::is_zst() {
368 // SAFETY: By the safety requirements of this function, `capacity * size_of::<T>()` is
369 // smaller than `isize::MAX`.
370 unsafe { ArrayLayout::new_unchecked(capacity) }
373 // INVARIANT: For ZSTs, we store an empty `ArrayLayout`, all other type invariants are
374 // covered by the safety requirements of this function.
376 // SAFETY: By the safety requirements, `ptr` is either dangling or pointing to a valid
377 // memory allocation, allocated with `A`.
378 ptr: unsafe { NonNull::new_unchecked(ptr) },
381 _p: PhantomData::<A>,
385 /// Consumes the `Vec<T, A>` and returns its raw components `pointer`, `length` and `capacity`.
387 /// This will not run the destructor of the contained elements and for non-ZSTs the allocation
388 /// will stay alive indefinitely. Use [`Vec::from_raw_parts`] to recover the [`Vec`], drop the
389 /// elements and free the allocation, if any.
390 pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
391 let mut me = ManuallyDrop::new(self);
393 let capacity = me.capacity();
394 let ptr = me.as_mut_ptr();
398 /// Ensures that the capacity exceeds the length by at least `additional` elements.
403 /// let mut v = KVec::new();
404 /// v.push(1, GFP_KERNEL)?;
406 /// v.reserve(10, GFP_KERNEL)?;
407 /// let cap = v.capacity();
408 /// assert!(cap >= 10);
410 /// v.reserve(10, GFP_KERNEL)?;
411 /// let new_cap = v.capacity();
412 /// assert_eq!(new_cap, cap);
414 /// # Ok::<(), Error>(())
416 pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError> {
417 let len = self.len();
418 let cap = self.capacity();
420 if cap - len >= additional {
425 // The capacity is already `usize::MAX` for ZSTs, we can't go higher.
426 return Err(AllocError);
429 // We know that `cap <= isize::MAX` because of the type invariants of `Self`. So the
430 // multiplication by two won't overflow.
431 let new_cap = core::cmp::max(cap * 2, len.checked_add(additional).ok_or(AllocError)?);
432 let layout = ArrayLayout::new(new_cap).map_err(|_| AllocError)?;
435 // - `ptr` is valid because it's either `None` or comes from a previous call to
437 // - `self.layout` matches the `ArrayLayout` of the preceding allocation.
440 Some(self.ptr.cast()),
448 // - `layout` is some `ArrayLayout::<T>`,
449 // - `ptr` has been created by `A::realloc` from `layout`.
450 self.ptr = ptr.cast();
451 self.layout = layout;
457 impl<T: Clone, A: Allocator> Vec<T, A> {
458 /// Extend the vector by `n` clones of `value`.
459 pub fn extend_with(&mut self, n: usize, value: T, flags: Flags) -> Result<(), AllocError> {
464 self.reserve(n, flags)?;
466 let spare = self.spare_capacity_mut();
468 for item in spare.iter_mut().take(n - 1) {
469 item.write(value.clone());
472 // We can write the last element directly without cloning needlessly.
473 spare[n - 1].write(value);
476 // - `self.len() + n < self.capacity()` due to the call to reserve above,
477 // - the loop and the line above initialized the next `n` elements.
478 unsafe { self.set_len(self.len() + n) };
483 /// Pushes clones of the elements of slice into the [`Vec`] instance.
488 /// let mut v = KVec::new();
489 /// v.push(1, GFP_KERNEL)?;
491 /// v.extend_from_slice(&[20, 30, 40], GFP_KERNEL)?;
492 /// assert_eq!(&v, &[1, 20, 30, 40]);
494 /// v.extend_from_slice(&[50, 60], GFP_KERNEL)?;
495 /// assert_eq!(&v, &[1, 20, 30, 40, 50, 60]);
496 /// # Ok::<(), Error>(())
498 pub fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), AllocError> {
499 self.reserve(other.len(), flags)?;
500 for (slot, item) in core::iter::zip(self.spare_capacity_mut(), other) {
501 slot.write(item.clone());
505 // - `other.len()` spare entries have just been initialized, so it is safe to increase
506 // the length by the same number.
507 // - `self.len() + other.len() <= self.capacity()` is guaranteed by the preceding `reserve`
509 unsafe { self.set_len(self.len() + other.len()) };
513 /// Create a new `Vec<T, A>` and extend it by `n` clones of `value`.
514 pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> {
515 let mut v = Self::with_capacity(n, flags)?;
517 v.extend_with(n, value, flags)?;
523 impl<T, A> Drop for Vec<T, A>
528 // SAFETY: `self.as_mut_ptr` is guaranteed to be valid by the type invariant.
530 ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut(
537 // - `self.ptr` was previously allocated with `A`.
538 // - `self.layout` matches the `ArrayLayout` of the preceding allocation.
539 unsafe { A::free(self.ptr.cast(), self.layout.into()) };
543 impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A>
547 fn from(b: Box<[T; N], A>) -> Vec<T, A> {
549 let ptr = Box::into_raw(b);
552 // - `b` has been allocated with `A`,
553 // - `ptr` fulfills the alignment requirements for `T`,
554 // - `ptr` points to memory with at least a size of `size_of::<T>() * len`,
555 // - all elements within `b` are initialized values of `T`,
556 // - `len` does not exceed `isize::MAX`.
557 unsafe { Vec::from_raw_parts(ptr as _, len, len) }
561 impl<T> Default for KVec<T> {
563 fn default() -> Self {
568 impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> {
569 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
570 fmt::Debug::fmt(&**self, f)
574 impl<T, A> Deref for Vec<T, A>
581 fn deref(&self) -> &[T] {
582 // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len`
583 // initialized elements of type `T`.
584 unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
588 impl<T, A> DerefMut for Vec<T, A>
593 fn deref_mut(&mut self) -> &mut [T] {
594 // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len`
595 // initialized elements of type `T`.
596 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
600 impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {}
602 impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>
606 type Output = I::Output;
609 fn index(&self, index: I) -> &Self::Output {
610 Index::index(&**self, index)
614 impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A>
619 fn index_mut(&mut self, index: I) -> &mut Self::Output {
620 IndexMut::index_mut(&mut **self, index)
624 macro_rules! impl_slice_eq {
625 ($([$($vars:tt)*] $lhs:ty, $rhs:ty,)*) => {
627 impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs
632 fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] }
639 [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>,
640 [A: Allocator] Vec<T, A>, &[U],
641 [A: Allocator] Vec<T, A>, &mut [U],
642 [A: Allocator] &[T], Vec<U, A>,
643 [A: Allocator] &mut [T], Vec<U, A>,
644 [A: Allocator] Vec<T, A>, [U],
645 [A: Allocator] [T], Vec<U, A>,
646 [A: Allocator, const N: usize] Vec<T, A>, [U; N],
647 [A: Allocator, const N: usize] Vec<T, A>, &[U; N],
650 impl<'a, T, A> IntoIterator for &'a Vec<T, A>
655 type IntoIter = slice::Iter<'a, T>;
657 fn into_iter(self) -> Self::IntoIter {
662 impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A>
666 type Item = &'a mut T;
667 type IntoIter = slice::IterMut<'a, T>;
669 fn into_iter(self) -> Self::IntoIter {
674 /// An [`Iterator`] implementation for [`Vec`] that moves elements out of a vector.
676 /// This structure is created by the [`Vec::into_iter`] method on [`Vec`] (provided by the
677 /// [`IntoIterator`] trait).
682 /// let v = kernel::kvec![0, 1, 2]?;
683 /// let iter = v.into_iter();
685 /// # Ok::<(), Error>(())
687 pub struct IntoIter<T, A: Allocator> {
691 layout: ArrayLayout<T>,
695 impl<T, A> IntoIter<T, A>
699 fn into_raw_parts(self) -> (*mut T, NonNull<T>, usize, usize) {
700 let me = ManuallyDrop::new(self);
704 let cap = me.layout.len();
708 /// Same as `Iterator::collect` but specialized for `Vec`'s `IntoIter`.
713 /// let v = kernel::kvec![1, 2, 3]?;
714 /// let mut it = v.into_iter();
716 /// assert_eq!(it.next(), Some(1));
718 /// let v = it.collect(GFP_KERNEL);
719 /// assert_eq!(v, [2, 3]);
721 /// # Ok::<(), Error>(())
724 /// # Implementation details
726 /// Currently, we can't implement `FromIterator`. There are a couple of issues with this trait
727 /// in the kernel, namely:
729 /// - Rust's specialization feature is unstable. This prevents us to optimize for the special
730 /// case where `I::IntoIter` equals `Vec`'s `IntoIter` type.
731 /// - We also can't use `I::IntoIter`'s type ID either to work around this, since `FromIterator`
732 /// doesn't require this type to be `'static`.
733 /// - `FromIterator::from_iter` does return `Self` instead of `Result<Self, AllocError>`, hence
734 /// we can't properly handle allocation failures.
735 /// - Neither `Iterator::collect` nor `FromIterator::from_iter` can handle additional allocation
738 /// Instead, provide `IntoIter::collect`, such that we can at least convert a `IntoIter` into a
741 /// Note that `IntoIter::collect` doesn't require `Flags`, since it re-uses the existing backing
742 /// buffer. However, this backing buffer may be shrunk to the actual count of elements.
743 pub fn collect(self, flags: Flags) -> Vec<T, A> {
744 let old_layout = self.layout;
745 let (mut ptr, buf, len, mut cap) = self.into_raw_parts();
746 let has_advanced = ptr != buf.as_ptr();
749 // Copy the contents we have advanced to at the beginning of the buffer.
752 // - `ptr` is valid for reads of `len * size_of::<T>()` bytes,
753 // - `buf.as_ptr()` is valid for writes of `len * size_of::<T>()` bytes,
754 // - `ptr` and `buf.as_ptr()` are not be subject to aliasing restrictions relative to
756 // - both `ptr` and `buf.ptr()` are properly aligned.
757 unsafe { ptr::copy(ptr, buf.as_ptr(), len) };
760 // SAFETY: `len` is guaranteed to be smaller than `self.layout.len()`.
761 let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) };
763 // SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed to be
764 // smaller than `cap`. Depending on `alloc` this operation may shrink the buffer or leaves
767 A::realloc(Some(buf.cast()), layout.into(), old_layout.into(), flags)
769 // If we fail to shrink, which likely can't even happen, continue with the existing
779 // SAFETY: If the iterator has been advanced, the advanced elements have been copied to
780 // the beginning of the buffer and `len` has been adjusted accordingly.
782 // - `ptr` is guaranteed to point to the start of the backing buffer.
783 // - `cap` is either the original capacity or, after shrinking the buffer, equal to `len`.
784 // - `alloc` is guaranteed to be unchanged since `into_iter` has been called on the original
786 unsafe { Vec::from_raw_parts(ptr, len, cap) }
790 impl<T, A> Iterator for IntoIter<T, A>
799 /// let v = kernel::kvec![1, 2, 3]?;
800 /// let mut it = v.into_iter();
802 /// assert_eq!(it.next(), Some(1));
803 /// assert_eq!(it.next(), Some(2));
804 /// assert_eq!(it.next(), Some(3));
805 /// assert_eq!(it.next(), None);
807 /// # Ok::<(), Error>(())
809 fn next(&mut self) -> Option<T> {
814 let current = self.ptr;
816 // SAFETY: We can't overflow; decreasing `self.len` by one every time we advance `self.ptr`
817 // by one guarantees that.
818 unsafe { self.ptr = self.ptr.add(1) };
822 // SAFETY: `current` is guaranteed to point at a valid element within the buffer.
823 Some(unsafe { current.read() })
829 /// let v: KVec<u32> = kernel::kvec![1, 2, 3]?;
830 /// let mut iter = v.into_iter();
831 /// let size = iter.size_hint().0;
834 /// assert_eq!(iter.size_hint().0, size - 1);
837 /// assert_eq!(iter.size_hint().0, size - 2);
840 /// assert_eq!(iter.size_hint().0, size - 3);
842 /// # Ok::<(), Error>(())
844 fn size_hint(&self) -> (usize, Option<usize>) {
845 (self.len, Some(self.len))
849 impl<T, A> Drop for IntoIter<T, A>
854 // SAFETY: `self.ptr` is guaranteed to be valid by the type invariant.
855 unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) };
858 // - `self.buf` was previously allocated with `A`.
859 // - `self.layout` matches the `ArrayLayout` of the preceding allocation.
860 unsafe { A::free(self.buf.cast(), self.layout.into()) };
864 impl<T, A> IntoIterator for Vec<T, A>
869 type IntoIter = IntoIter<T, A>;
871 /// Consumes the `Vec<T, A>` and creates an `Iterator`, which moves each value out of the
872 /// vector (from start to end).
877 /// let v = kernel::kvec![1, 2]?;
878 /// let mut v_iter = v.into_iter();
880 /// let first_element: Option<u32> = v_iter.next();
882 /// assert_eq!(first_element, Some(1));
883 /// assert_eq!(v_iter.next(), Some(2));
884 /// assert_eq!(v_iter.next(), None);
886 /// # Ok::<(), Error>(())
890 /// let v = kernel::kvec![];
891 /// let mut v_iter = v.into_iter();
893 /// let first_element: Option<u32> = v_iter.next();
895 /// assert_eq!(first_element, None);
897 /// # Ok::<(), Error>(())
900 fn into_iter(self) -> Self::IntoIter {
902 let layout = self.layout;
903 let (ptr, len, _) = self.into_raw_parts();
910 _p: PhantomData::<A>,