Skip to content

Commit 76f8ff8

Browse files
Danilo Krummrichfbq
authored andcommitted
rust: alloc: implement IntoIterator for Vec
Implement `IntoIterator` for `Vec`, `Vec`'s `IntoIter` type, as well as `Iterator` for `IntoIter`. `Vec::into_iter` disassembles the `Vec` into its raw parts; additionally, `IntoIter` keeps track of a separate pointer, which is incremented correspondingsly as the iterator advances, while the length, or the count of elements, is decremented. This also means that `IntoIter` takes the ownership of the backing buffer and is responsible to drop the remaining elements and free the backing buffer, if it's dropped. Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 105b2b8 commit 76f8ff8

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

rust/kernel/alloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use self::kbox::KBox;
1919
pub use self::kbox::KVBox;
2020
pub use self::kbox::VBox;
2121

22+
pub use self::kvec::IntoIter;
2223
pub use self::kvec::KVVec;
2324
pub use self::kvec::KVec;
2425
pub use self::kvec::VVec;

rust/kernel/alloc/kvec.rs

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use core::{
1111
ops::DerefMut,
1212
ops::Index,
1313
ops::IndexMut,
14+
ptr,
1415
ptr::NonNull,
1516
slice,
1617
slice::SliceIndex,
@@ -627,3 +628,186 @@ __impl_slice_eq! { [A: Allocator] Vec<T, A>, [U] }
627628
__impl_slice_eq! { [A: Allocator] [T], Vec<U, A> }
628629
__impl_slice_eq! { [A: Allocator, const N: usize] Vec<T, A>, [U; N] }
629630
__impl_slice_eq! { [A: Allocator, const N: usize] Vec<T, A>, &[U; N] }
631+
632+
impl<'a, T, A> IntoIterator for &'a Vec<T, A>
633+
where
634+
A: Allocator,
635+
{
636+
type Item = &'a T;
637+
type IntoIter = slice::Iter<'a, T>;
638+
639+
fn into_iter(self) -> Self::IntoIter {
640+
self.iter()
641+
}
642+
}
643+
644+
impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A>
645+
where
646+
A: Allocator,
647+
{
648+
type Item = &'a mut T;
649+
type IntoIter = slice::IterMut<'a, T>;
650+
651+
fn into_iter(self) -> Self::IntoIter {
652+
self.iter_mut()
653+
}
654+
}
655+
656+
/// An `Iterator` implementation for `Vec<T,A>` that moves elements out of a vector.
657+
///
658+
/// This structure is created by the `Vec::into_iter` method on [`Vec`] (provided by the
659+
/// [`IntoIterator`] trait).
660+
///
661+
/// # Examples
662+
///
663+
/// ```
664+
/// let v = kernel::kvec![0, 1, 2]?;
665+
/// let iter = v.into_iter();
666+
///
667+
/// # Ok::<(), Error>(())
668+
/// ```
669+
pub struct IntoIter<T, A: Allocator> {
670+
ptr: *mut T,
671+
buf: NonNull<T>,
672+
len: usize,
673+
cap: usize,
674+
_p: PhantomData<A>,
675+
}
676+
677+
impl<T, A> IntoIter<T, A>
678+
where
679+
A: Allocator,
680+
{
681+
fn as_raw_mut_slice(&mut self) -> *mut [T] {
682+
ptr::slice_from_raw_parts_mut(self.ptr, self.len)
683+
}
684+
}
685+
686+
impl<T, A> Iterator for IntoIter<T, A>
687+
where
688+
A: Allocator,
689+
{
690+
type Item = T;
691+
692+
/// # Examples
693+
///
694+
/// ```
695+
/// let v = kernel::kvec![1, 2, 3]?;
696+
/// let mut it = v.into_iter();
697+
///
698+
/// assert_eq!(it.next(), Some(1));
699+
/// assert_eq!(it.next(), Some(2));
700+
/// assert_eq!(it.next(), Some(3));
701+
/// assert_eq!(it.next(), None);
702+
///
703+
/// # Ok::<(), Error>(())
704+
/// ```
705+
fn next(&mut self) -> Option<T> {
706+
if self.len == 0 {
707+
return None;
708+
}
709+
710+
let ptr = self.ptr;
711+
if !Vec::<T, A>::is_zst() {
712+
// SAFETY: We can't overflow; `end` is guaranteed to mark the end of the buffer.
713+
unsafe { self.ptr = self.ptr.add(1) };
714+
} else {
715+
// For ZST `ptr` has to stay where it is to remain aligned, so we just reduce `self.len`
716+
// by 1.
717+
}
718+
self.len -= 1;
719+
720+
// SAFETY: `ptr` is guaranteed to point at a valid element within the buffer.
721+
Some(unsafe { ptr.read() })
722+
}
723+
724+
/// # Examples
725+
///
726+
/// ```
727+
/// let v: KVec<u32> = kernel::kvec![1, 2, 3]?;
728+
/// let mut iter = v.into_iter();
729+
/// let size = iter.size_hint().0;
730+
///
731+
/// iter.next();
732+
/// assert_eq!(iter.size_hint().0, size - 1);
733+
///
734+
/// iter.next();
735+
/// assert_eq!(iter.size_hint().0, size - 2);
736+
///
737+
/// iter.next();
738+
/// assert_eq!(iter.size_hint().0, size - 3);
739+
///
740+
/// # Ok::<(), Error>(())
741+
/// ```
742+
fn size_hint(&self) -> (usize, Option<usize>) {
743+
(self.len, Some(self.len))
744+
}
745+
}
746+
747+
impl<T, A> Drop for IntoIter<T, A>
748+
where
749+
A: Allocator,
750+
{
751+
fn drop(&mut self) {
752+
// SAFETY: Drop the remaining vector's elements in place, before we free the backing
753+
// memory.
754+
unsafe { ptr::drop_in_place(self.as_raw_mut_slice()) };
755+
756+
// If `cap == 0` we never allocated any memory in the first place.
757+
if self.cap != 0 {
758+
// SAFETY: `self.buf` was previously allocated with `A`.
759+
unsafe { A::free(self.buf.cast()) };
760+
}
761+
}
762+
}
763+
764+
impl<T, A> IntoIterator for Vec<T, A>
765+
where
766+
A: Allocator,
767+
{
768+
type Item = T;
769+
type IntoIter = IntoIter<T, A>;
770+
771+
/// Consumes the `Vec<T, A>` and creates an `Iterator`, which moves each value out of the
772+
/// vector (from start to end).
773+
///
774+
/// # Examples
775+
///
776+
/// ```
777+
/// let v = kernel::kvec![1, 2]?;
778+
/// let mut v_iter = v.into_iter();
779+
///
780+
/// let first_element: Option<u32> = v_iter.next();
781+
///
782+
/// assert_eq!(first_element, Some(1));
783+
/// assert_eq!(v_iter.next(), Some(2));
784+
/// assert_eq!(v_iter.next(), None);
785+
///
786+
/// # Ok::<(), Error>(())
787+
/// ```
788+
///
789+
/// ```
790+
/// let v = kernel::kvec![];
791+
/// let mut v_iter = v.into_iter();
792+
///
793+
/// let first_element: Option<u32> = v_iter.next();
794+
///
795+
/// assert_eq!(first_element, None);
796+
///
797+
/// # Ok::<(), Error>(())
798+
/// ```
799+
#[inline]
800+
fn into_iter(self) -> Self::IntoIter {
801+
let (ptr, len, cap) = self.into_raw_parts();
802+
803+
IntoIter {
804+
ptr,
805+
// SAFETY: `ptr` is either a dangling pointer or a pointer to a valid memory
806+
// allocation, allocated with `A`.
807+
buf: unsafe { NonNull::new_unchecked(ptr) },
808+
len,
809+
cap,
810+
_p: PhantomData::<A>,
811+
}
812+
}
813+
}

0 commit comments

Comments
 (0)