}} impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A>where A: Allocator,{ fn from(b: Box<[T; N], A>) -> Vec<T, A> { let len = b.len(); let ptr = Box::into_raw(b); // SAFETY: // - `b` has been allocated with `A`, // - `ptr` fulfills the alignment requirements for `T`, // - `ptr` points to memory with at least a size of `size_of::<T>() * len`, // - all elements within `b` are initialized values of `T`, // - `len` does not exceed `isize::MAX`. unsafe { Vec::from_raw_parts(ptr.cast(), len, len) } }} impl<T, A: Allocator> Default for Vec<T, A> { #[inline] fn default() -> Self { Self::new() }} impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) }} impl<T, A> Deref for Vec<T, A>where A: Allocator,{ type Target = [T]; #[inline] fn deref(&self) -> &[T] { // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len` // initialized elements of type `T`. unsafe { slice::from_raw_parts(self.as_ptr(), self.len) } }} impl<T, A> DerefMut for Vec<T, A>where A: Allocator,{ #[inline] fn deref_mut(&mut self) -> &mut [T] { // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len` // initialized elements of type `T`. unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } }} /// # Examples////// ```/// # use core::borrow::Borrow;/// struct Foo<B: Borrow<[u32]>>(B);////// // Owned array./// let owned_array = Foo([1, 2, 3]);////// // Owned vector./// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);////// let arr = [1, 2, 3];/// // Borrowed slice from `arr`./// let borrowed_slice = Foo(&arr[..]);/// # Ok::<(), Error>(())/// ```impl<T, A> Borrow<[T]> for Vec<T, A>where A: Allocator,{ fn borrow(&self) -> &[T] { self.as_slice() }} /// # Examples////// ```/// # use core::borrow::BorrowMut;/// struct Foo<B: BorrowMut<[u32]>>(B);////// // Owned array./// let owned_array = Foo([1, 2, 3]);////// // Owned vector./// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?);////// let mut arr = [1, 2, 3];/// // Borrowed slice from `arr`./// let borrowed_slice = Foo(&mut arr[..]);/// # Ok::<(), Error>(())/// ```impl<T, A> BorrowMut<[T]> for Vec<T, A>where A: Allocator,{ fn borrow_mut(&mut self) -> &mut [T] { self.as_mut_slice() }} impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {} impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A>where A: Allocator,{ type Output = I::Output; #[inline] fn index(&self, index: I) -> &Self::Output { Index::index(&**self, index) }} impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A>where A: Allocator,{ #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { IndexMut::index_mut(&mut **self, index) }} macro_rules! impl_slice_eq { ($([$($vars:tt)*] $lhs:ty, $rhs:ty,)*) => { $( impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, { #[inline] fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } } )* }} impl_slice_eq! { [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>, [A: Allocator] Vec<T, A>, &[U], [A: Allocator] Vec<T, A>, &mut [U], [A: Allocator] &[T], Vec<U, A>, [A: Allocator] &mut [T], Vec<U, A>, [A: Allocator] Vec<T, A>, [U], [A: Allocator] [T], Vec<U, A>, [A: Allocator, const N: usize] Vec<T, A>, [U; N], [A: Allocator, const N: usize] Vec<T, A>, &[U; N],} impl<'a, T, A> IntoIterator for &'a Vec<T, A>where A: Allocator,{ type Item = &'a T; type IntoIter = slice::Iter<'a, T>; fn into_iter(self) -> Self::IntoIter { self.iter() }} impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A>where A: Allocator,{ type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; fn into_iter(self) -> Self::IntoIter { self.iter_mut() }} /// # Examples////// ```/// use kernel::{/// alloc::allocator::VmallocPageIter,/// page::{/// AsPageIter,/// PAGE_SIZE, ///// }, ///// };////// let mut vec = VVec::<u8>::new();////// assert!(vec.page_iter().next().is_none());////// vec.reserve(PAGE_SIZE, GFP_KERNEL)?;////// let page = vec.page_iter().next().expect("At least one page should be available.\n");////// // SAFETY: There is no concurrent read or write to the same page./// unsafe { page.fill_zero_raw(0, PAGE_SIZE)? };/// # Ok::<(), Error>(())/// ```impl<T> AsPageIter for VVec<T> { type Iter<'a> = VmallocPageIter<'a> where T: 'a; fn page_iter(&mut self) -> Self::Iter<'_> { let ptr = self.ptr.cast(); let size = self.layout.size(); // SAFETY: // - `ptr` is a valid pointer to the beginning of a `Vmalloc` allocation. // - `ptr` is guaranteed to be valid for the lifetime of `'a`. // - `size` is the size of the `Vmalloc` allocation `ptr` points to. unsafe { VmallocPageIter::new(ptr, size) } }} /// An [`Iterator`] implementation for [`Vec`] that moves elements out of a vector.////// This structure is created by the [`Vec::into_iter`] method on [`Vec`] (provided by the/// [`IntoIterator`] trait).////// # Examples////// ```/// let v = kernel::kvec![0, 1, 2]?;/// let iter = v.into_iter();////// # Ok::<(), Error>(())/// ```pub struct IntoIter<T, A: Allocator> { ptr: *mut T, buf: NonNull<T>, len: usize, layout: ArrayLayout<T>, _p: PhantomData<A>,} impl<T, A> IntoIter<T, A>where A: Allocator,{ fn into_raw_parts(self) -> (*mut T, NonNull<T>, usize, usize) { let me = ManuallyDrop::new(self); let ptr = me.ptr; let buf = me.buf; let len = me.len; let cap = me.layout.len(); (ptr, buf, len, cap) } /// Same as `Iterator::collect` but specialized for `Vec`'s `IntoIter`. /// /// # Examples /// /// ``` /// let v = kernel::kvec![1, 2, 3]?; /// let mut it = v.into_iter(); /// /// assert_eq!(it.next(), Some(1)); /// /// let v = it.collect(GFP_KERNEL); /// assert_eq!(v, [2, 3]); /// /// # Ok::<(), Error>(()) /// ``` /// /// # Implementation details /// /// Currently, we can't implement `FromIterator`. There are a couple of issues with this trait /// in the kernel, namely: /// /// - Rust's specialization feature is unstable. This prevents us to optimize for the special /// case where `I::IntoIter` equals `Vec`'s `IntoIter` type. /// - We also can't use `I::IntoIter`'s type ID either to work around this, since `FromIterator` /// doesn't require this type to be `'static`. /// - `FromIterator::from_iter` does return `Self` instead of `Result<Self, AllocError>`, hence /// we can't properly handle allocation failures. /// - Neither `Iterator::collect` nor `FromIterator::from_iter` can handle additional allocation /// flags. /// /// Instead, provide `IntoIter::collect`, such that we can at least convert a `IntoIter` into a /// `Vec` again. /// /// Note that `IntoIter::collect` doesn't require `Flags`, since it re-uses the existing backing /// buffer. However, this backing buffer may be shrunk to the actual count of elements. pub fn collect(self, flags: Flags) -> Vec<T, A> { let old_layout = self.layout; let (mut ptr, buf, len, mut cap) = self.into_raw_parts(); let has_advanced = ptr != buf.as_ptr(); if has_advanced { // Copy the contents we have advanced to at the beginning of the buffer. // // SAFETY: // - `ptr` is valid for reads of `len * size_of::<T>()` bytes, // - `buf.as_ptr()` is valid for writes of `len * size_of::<T>()` bytes, // - `ptr` and `buf.as_ptr()` are not be subject to aliasing restrictions relative to // each other, // - both `ptr` and `buf.ptr()` are properly aligned. unsafe { ptr::copy(ptr, buf.as_ptr(), len) }; ptr = buf.as_ptr(); // SAFETY: `len` is guaranteed to be smaller than `self.layout.len()` by the type // invariant. let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) }; // SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed by // the type invariant to be smaller than `cap`. Depending on `realloc` this operation // may shrink the buffer or leave it as it is. ptr = match unsafe { A::realloc( Some(buf.cast()), layout.into(), old_layout.into(), flags, NumaNode::NO_NODE, ) } { // If we fail to shrink, which likely can't even happen, continue with the existing // buffer. Err(_) => ptr, Ok(ptr) => { cap = len; ptr.as_ptr().cast() } }; } // SAFETY: If the iterator has been advanced, the advanced elements have been copied to // the beginning of the buffer and `len` has been adjusted accordingly. // // - `ptr` is guaranteed to point to the start of the backing buffer. // - `cap` is either the original capacity or, after shrinking the buffer, equal to `len`. // - `alloc` is guaranteed to be unchanged since `into_iter` has been called on the original // `Vec`. unsafe { Vec::from_raw_parts(ptr, len, cap) } }} impl<T, A> Iterator for IntoIter<T, A>where A: Allocator,{ type Item = T; /// # Examples /// /// ``` /// let v = kernel::kvec![1, 2, 3]?; /// let mut it = v.into_iter(); /// /// assert_eq!(it.next(), Some(1)); /// assert_eq!(it.next(), Some(2)); /// assert_eq!(it.next(), Some(3)); /// assert_eq!(it.next(), None); /// /// # Ok::<(), Error>(()) /// ``` fn next(&mut self) -> Option<T> { if self.len == 0 { return None; } let current = self.ptr; // SAFETY: We can't overflow; decreasing `self.len` by one every time we advance `self.ptr` // by one guarantees that. unsafe { self.ptr = self.ptr.add(1) }; self.len -= 1; // SAFETY: `current` is guaranteed to point at a valid element within the buffer. Some(unsafe { current.read() }) } /// # Examples /// /// ``` /// let v: KVec<u32> = kernel::kvec![1, 2, 3]?; /// let mut iter = v.into_iter(); /// let size = iter.size_hint().0; /// /// iter.next(); /// assert_eq!(iter.size_hint().0, size - 1); /// /// iter.next(); /// assert_eq!(iter.size_hint().0, size - 2); /// /// iter.next(); /// assert_eq!(iter.size_hint().0, size - 3); /// /// # Ok::<(), Error>(()) /// ``` fn size_hint(&self) -> (usize, Option<usize>) { (self.len, Some(self.len)) }} impl<T, A> Drop for IntoIter<T, A>where A: Allocator,{ fn drop(&mut self) { // SAFETY: `self.ptr` is guaranteed to be valid by the type invariant. unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) }; // SAFETY: // - `self.buf` was previously allocated with `A`. // - `self.layout` matches the `ArrayLayout` of the preceding allocation. unsafe { A::free(self.buf.cast(), self.layout.into()) }; }} impl<T, A> IntoIterator for Vec<T, A>where A: Allocator,{ type Item = T; type IntoIter = IntoIter<T, A>; /// Consumes the `Vec<T, A>` and creates an `Iterator`, which moves each value out of the /// vector (from start to end). /// /// # Examples /// /// ``` /// let v = kernel::kvec![1, 2]?; /// let mut v_iter = v.into_iter(); /// /// let first_element: Option<u32> = v_iter.next(); /// /// assert_eq!(first_element, Some(1)); /// assert_eq!(v_iter.next(), Some(2)); /// assert_eq!(v_iter.next(), None); /// /// # Ok::<(), Error>(()) /// ``` /// /// ``` /// let v = kernel::kvec![]; /// let mut v_iter = v.into_iter(); /// /// let first_element: Option<u32> = v_iter.next(); /// /// assert_eq!(first_element, None); /// /// # Ok::<(), Error>(()) /// ``` #[inline] fn into_iter(self) -> Self::IntoIter { let buf = self.ptr; let layout = self.layout; let (ptr, len, _) = self.into_raw_parts(); IntoIter { ptr, buf, len, layout, _p: PhantomData::<A>, } }} /// An iterator that owns all items in a vector, but does not own its allocation.////// # Invariants////// Every `&mut T` returned by the iterator references a `T` that the iterator may take ownership/// of.pub struct DrainAll<'vec, T> { elements: slice::IterMut<'vec, T>,} impl<'vec, T> Iterator for DrainAll<'vec, T> { type Item = T; fn next(&mut self) -> Option<T> { let elem: *mut T = self.elements.next()?; // SAFETY: By the type invariants, we may take ownership of this value. Some(unsafe { elem.read() }) } fn size_hint(&self) -> (usize, Option<usize>) { self.elements.size_hint() }} impl<'vec, T> Drop for DrainAll<'vec, T> {Lines 1001–1500 of 1661. Previous lines Load more
} } impl<T, A, const N: usize> From<Box<[T; N], A>> for Vec<T, A> where A: Allocator, { fn from(b: Box<[T; N], A>) -> Vec<T, A> { let len = b.len(); let ptr = Box::into_raw(b); // SAFETY: // - `b` has been allocated with `A`, // - `ptr` fulfills the alignment requirements for `T`, // - `ptr` points to memory with at least a size of `size_of::<T>() * len`, // - all elements within `b` are initialized values of `T`, // - `len` does not exceed `isize::MAX`. unsafe { Vec::from_raw_parts(ptr.cast(), len, len) } } } impl<T, A: Allocator> Default for Vec<T, A> { #[inline] fn default() -> Self { Self::new() } } impl<T: fmt::Debug, A: Allocator> fmt::Debug for Vec<T, A> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } impl<T, A> Deref for Vec<T, A> where A: Allocator, { type Target = [T]; #[inline] fn deref(&self) -> &[T] { // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len` // initialized elements of type `T`. unsafe { slice::from_raw_parts(self.as_ptr(), self.len) } } } impl<T, A> DerefMut for Vec<T, A> where A: Allocator, { #[inline] fn deref_mut(&mut self) -> &mut [T] { // SAFETY: The memory behind `self.as_ptr()` is guaranteed to contain `self.len` // initialized elements of type `T`. unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) } } } /// # Examples /// /// ``` /// # use core::borrow::Borrow; /// struct Foo<B: Borrow<[u32]>>(B); /// /// // Owned array. /// let owned_array = Foo([1, 2, 3]); /// /// // Owned vector. /// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?); /// /// let arr = [1, 2, 3]; /// // Borrowed slice from `arr`. /// let borrowed_slice = Foo(&arr[..]); /// # Ok::<(), Error>(()) /// ``` impl<T, A> Borrow<[T]> for Vec<T, A> where A: Allocator, { fn borrow(&self) -> &[T] { self.as_slice() } } /// # Examples /// /// ``` /// # use core::borrow::BorrowMut; /// struct Foo<B: BorrowMut<[u32]>>(B); /// /// // Owned array. /// let owned_array = Foo([1, 2, 3]); /// /// // Owned vector. /// let owned_vec = Foo(KVec::from_elem(0, 3, GFP_KERNEL)?); /// /// let mut arr = [1, 2, 3]; /// // Borrowed slice from `arr`. /// let borrowed_slice = Foo(&mut arr[..]); /// # Ok::<(), Error>(()) /// ``` impl<T, A> BorrowMut<[T]> for Vec<T, A> where A: Allocator, { fn borrow_mut(&mut self) -> &mut [T] { self.as_mut_slice() } } impl<T: Eq, A> Eq for Vec<T, A> where A: Allocator {} impl<T, I: SliceIndex<[T]>, A> Index<I> for Vec<T, A> where A: Allocator, { type Output = I::Output; #[inline] fn index(&self, index: I) -> &Self::Output { Index::index(&**self, index) } } impl<T, I: SliceIndex<[T]>, A> IndexMut<I> for Vec<T, A> where A: Allocator, { #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { IndexMut::index_mut(&mut **self, index) } } macro_rules! impl_slice_eq { ($([$($vars:tt)*] $lhs:ty, $rhs:ty,)*) => { $( impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs where T: PartialEq<U>, { #[inline] fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } } )* } } impl_slice_eq! { [A1: Allocator, A2: Allocator] Vec<T, A1>, Vec<U, A2>, [A: Allocator] Vec<T, A>, &[U], [A: Allocator] Vec<T, A>, &mut [U], [A: Allocator] &[T], Vec<U, A>, [A: Allocator] &mut [T], Vec<U, A>, [A: Allocator] Vec<T, A>, [U], [A: Allocator] [T], Vec<U, A>, [A: Allocator, const N: usize] Vec<T, A>, [U; N], [A: Allocator, const N: usize] Vec<T, A>, &[U; N], } impl<'a, T, A> IntoIterator for &'a Vec<T, A> where A: Allocator, { type Item = &'a T; type IntoIter = slice::Iter<'a, T>; fn into_iter(self) -> Self::IntoIter { self.iter() } } impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> where A: Allocator, { type Item = &'a mut T; type IntoIter = slice::IterMut<'a, T>; fn into_iter(self) -> Self::IntoIter { self.iter_mut() } } /// # Examples /// /// ``` /// use kernel::{ /// alloc::allocator::VmallocPageIter, /// page::{ /// AsPageIter, /// PAGE_SIZE, // /// }, // /// }; /// /// let mut vec = VVec::<u8>::new(); /// /// assert!(vec.page_iter().next().is_none()); /// /// vec.reserve(PAGE_SIZE, GFP_KERNEL)?; /// /// let page = vec.page_iter().next().expect("At least one page should be available.\n"); /// /// // SAFETY: There is no concurrent read or write to the same page. /// unsafe { page.fill_zero_raw(0, PAGE_SIZE)? }; /// # Ok::<(), Error>(()) /// ``` impl<T> AsPageIter for VVec<T> { type Iter<'a> = VmallocPageIter<'a> where T: 'a; fn page_iter(&mut self) -> Self::Iter<'_> { let ptr = self.ptr.cast(); let size = self.layout.size(); // SAFETY: // - `ptr` is a valid pointer to the beginning of a `Vmalloc` allocation. // - `ptr` is guaranteed to be valid for the lifetime of `'a`. // - `size` is the size of the `Vmalloc` allocation `ptr` points to. unsafe { VmallocPageIter::new(ptr, size) } } } /// An [`Iterator`] implementation for [`Vec`] that moves elements out of a vector. /// /// This structure is created by the [`Vec::into_iter`] method on [`Vec`] (provided by the /// [`IntoIterator`] trait). /// /// # Examples /// /// ``` /// let v = kernel::kvec![0, 1, 2]?; /// let iter = v.into_iter(); /// /// # Ok::<(), Error>(()) /// ``` pub struct IntoIter<T, A: Allocator> { ptr: *mut T, buf: NonNull<T>, len: usize, layout: ArrayLayout<T>, _p: PhantomData<A>, } impl<T, A> IntoIter<T, A> where A: Allocator, { fn into_raw_parts(self) -> (*mut T, NonNull<T>, usize, usize) { let me = ManuallyDrop::new(self); let ptr = me.ptr; let buf = me.buf; let len = me.len; let cap = me.layout.len(); (ptr, buf, len, cap) } /// Same as `Iterator::collect` but specialized for `Vec`'s `IntoIter`. /// /// # Examples /// /// ``` /// let v = kernel::kvec![1, 2, 3]?; /// let mut it = v.into_iter(); /// /// assert_eq!(it.next(), Some(1)); /// /// let v = it.collect(GFP_KERNEL); /// assert_eq!(v, [2, 3]); /// /// # Ok::<(), Error>(()) /// ``` /// /// # Implementation details /// /// Currently, we can't implement `FromIterator`. There are a couple of issues with this trait /// in the kernel, namely: /// /// - Rust's specialization feature is unstable. This prevents us to optimize for the special /// case where `I::IntoIter` equals `Vec`'s `IntoIter` type. /// - We also can't use `I::IntoIter`'s type ID either to work around this, since `FromIterator` /// doesn't require this type to be `'static`. /// - `FromIterator::from_iter` does return `Self` instead of `Result<Self, AllocError>`, hence /// we can't properly handle allocation failures. /// - Neither `Iterator::collect` nor `FromIterator::from_iter` can handle additional allocation /// flags. /// /// Instead, provide `IntoIter::collect`, such that we can at least convert a `IntoIter` into a /// `Vec` again. /// /// Note that `IntoIter::collect` doesn't require `Flags`, since it re-uses the existing backing /// buffer. However, this backing buffer may be shrunk to the actual count of elements. pub fn collect(self, flags: Flags) -> Vec<T, A> { let old_layout = self.layout; let (mut ptr, buf, len, mut cap) = self.into_raw_parts(); let has_advanced = ptr != buf.as_ptr(); if has_advanced { // Copy the contents we have advanced to at the beginning of the buffer. // // SAFETY: // - `ptr` is valid for reads of `len * size_of::<T>()` bytes, // - `buf.as_ptr()` is valid for writes of `len * size_of::<T>()` bytes, // - `ptr` and `buf.as_ptr()` are not be subject to aliasing restrictions relative to // each other, // - both `ptr` and `buf.ptr()` are properly aligned. unsafe { ptr::copy(ptr, buf.as_ptr(), len) }; ptr = buf.as_ptr(); // SAFETY: `len` is guaranteed to be smaller than `self.layout.len()` by the type // invariant. let layout = unsafe { ArrayLayout::<T>::new_unchecked(len) }; // SAFETY: `buf` points to the start of the backing buffer and `len` is guaranteed by // the type invariant to be smaller than `cap`. Depending on `realloc` this operation // may shrink the buffer or leave it as it is. ptr = match unsafe { A::realloc( Some(buf.cast()), layout.into(), old_layout.into(), flags, NumaNode::NO_NODE, ) } { // If we fail to shrink, which likely can't even happen, continue with the existing // buffer. Err(_) => ptr, Ok(ptr) => { cap = len; ptr.as_ptr().cast() } }; } // SAFETY: If the iterator has been advanced, the advanced elements have been copied to // the beginning of the buffer and `len` has been adjusted accordingly. // // - `ptr` is guaranteed to point to the start of the backing buffer. // - `cap` is either the original capacity or, after shrinking the buffer, equal to `len`. // - `alloc` is guaranteed to be unchanged since `into_iter` has been called on the original // `Vec`. unsafe { Vec::from_raw_parts(ptr, len, cap) } } } impl<T, A> Iterator for IntoIter<T, A> where A: Allocator, { type Item = T; /// # Examples /// /// ``` /// let v = kernel::kvec![1, 2, 3]?; /// let mut it = v.into_iter(); /// /// assert_eq!(it.next(), Some(1)); /// assert_eq!(it.next(), Some(2)); /// assert_eq!(it.next(), Some(3)); /// assert_eq!(it.next(), None); /// /// # Ok::<(), Error>(()) /// ``` fn next(&mut self) -> Option<T> { if self.len == 0 { return None; } let current = self.ptr; // SAFETY: We can't overflow; decreasing `self.len` by one every time we advance `self.ptr` // by one guarantees that. unsafe { self.ptr = self.ptr.add(1) }; self.len -= 1; // SAFETY: `current` is guaranteed to point at a valid element within the buffer. Some(unsafe { current.read() }) } /// # Examples /// /// ``` /// let v: KVec<u32> = kernel::kvec![1, 2, 3]?; /// let mut iter = v.into_iter(); /// let size = iter.size_hint().0; /// /// iter.next(); /// assert_eq!(iter.size_hint().0, size - 1); /// /// iter.next(); /// assert_eq!(iter.size_hint().0, size - 2); /// /// iter.next(); /// assert_eq!(iter.size_hint().0, size - 3); /// /// # Ok::<(), Error>(()) /// ``` fn size_hint(&self) -> (usize, Option<usize>) { (self.len, Some(self.len)) } } impl<T, A> Drop for IntoIter<T, A> where A: Allocator, { fn drop(&mut self) { // SAFETY: `self.ptr` is guaranteed to be valid by the type invariant. unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.ptr, self.len)) }; // SAFETY: // - `self.buf` was previously allocated with `A`. // - `self.layout` matches the `ArrayLayout` of the preceding allocation. unsafe { A::free(self.buf.cast(), self.layout.into()) }; } } impl<T, A> IntoIterator for Vec<T, A> where A: Allocator, { type Item = T; type IntoIter = IntoIter<T, A>; /// Consumes the `Vec<T, A>` and creates an `Iterator`, which moves each value out of the /// vector (from start to end). /// /// # Examples /// /// ``` /// let v = kernel::kvec![1, 2]?; /// let mut v_iter = v.into_iter(); /// /// let first_element: Option<u32> = v_iter.next(); /// /// assert_eq!(first_element, Some(1)); /// assert_eq!(v_iter.next(), Some(2)); /// assert_eq!(v_iter.next(), None); /// /// # Ok::<(), Error>(()) /// ``` /// /// ``` /// let v = kernel::kvec![]; /// let mut v_iter = v.into_iter(); /// /// let first_element: Option<u32> = v_iter.next(); /// /// assert_eq!(first_element, None); /// /// # Ok::<(), Error>(()) /// ``` #[inline] fn into_iter(self) -> Self::IntoIter { let buf = self.ptr; let layout = self.layout; let (ptr, len, _) = self.into_raw_parts(); IntoIter { ptr, buf, len, layout, _p: PhantomData::<A>, } } } /// An iterator that owns all items in a vector, but does not own its allocation. /// /// # Invariants /// /// Every `&mut T` returned by the iterator references a `T` that the iterator may take ownership /// of. pub struct DrainAll<'vec, T> { elements: slice::IterMut<'vec, T>, } impl<'vec, T> Iterator for DrainAll<'vec, T> { type Item = T; fn next(&mut self) -> Option<T> { let elem: *mut T = self.elements.next()?; // SAFETY: By the type invariants, we may take ownership of this value. Some(unsafe { elem.read() }) } fn size_hint(&self) -> (usize, Option<usize>) { self.elements.size_hint() } } impl<'vec, T> Drop for DrainAll<'vec, T> {