From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35F8BC3DA4A for ; Thu, 1 Aug 2024 15:30:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C50226B009A; Thu, 1 Aug 2024 11:30:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C00B56B009B; Thu, 1 Aug 2024 11:30:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC7F86B009C; Thu, 1 Aug 2024 11:30:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8D5C66B009A for ; Thu, 1 Aug 2024 11:30:11 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4141240ED3 for ; Thu, 1 Aug 2024 15:30:11 +0000 (UTC) X-FDA: 82404062622.22.F940B5D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id 738251C0015 for ; Thu, 1 Aug 2024 15:30:09 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="dXb/ZzH+"; spf=pass (imf21.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722526152; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lSz/hmNiAhuETYZ+38pCr6j7d5lsAoOuaNVcoSZ8heY=; b=rtyeuXFxZQEpE7l5vwEkV3Rqqh2omsyc5k8QG96CNGY/ESEQliL4XN4xzQNWCtzEKu0va5 FDuFxW15upJ3sjYbOYjOWGgqy3FD4VwKv9I7nkFogl9r6/Tgh60pa9J834o9X0t3OqIRB3 jYs6Mj/p6CaRrHi25286vI5pobWZzJw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722526152; a=rsa-sha256; cv=none; b=aDXsBxaJSB8SuPHdQ3V+BiCsKiGkMe5I/1F+KQqvXoPLJ5S6sdqu7pKF+sij0aUVIi/MUj VmaofTsTpaMJdKy/EozWrIZgA4JKIbe+LJ/v6ghuJuEg2ESGQjzd+GFVzypEX1vk5LrYW3 v+hageTlH4F2pUW9HqyWXKakaSDvGuI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="dXb/ZzH+"; spf=pass (imf21.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7EFF66288B; Thu, 1 Aug 2024 15:30:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 533BAC4AF09; Thu, 1 Aug 2024 15:30:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722526208; bh=0PQbDS0wuvLYars5FMo32gpzSiRNyt2BJDuRhnjMKBc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dXb/ZzH+0ytsrhg6eRGvKJral/3oOCt1JVJCjYgcYnHKfgfqBV24thiE/g+Fdg7z3 4ViwdfjP7GsG2zZD39PIOalW5KGPhkWJ7uoeH39FlT0ezjZG6+vbQ4X14hHVYM7lXl TiKKbY3Bj3BAkooWElV6fyOGm3dIQgbdFyQ28YeoJWxO08sJOsuIl38s1IUgi433UL iimsORy5FluP+raDPookaJafu0333TsD6yuZ1WIvBY735OMF/I24kcBvU6Z5XSkHH8 ZNwCiKX5vKRLlKOzmCnkbCNKqT+vvea5j7iU846S3G0VTGzKMEQ22TFTQjF8Jee/RP OES/3Sgx520jw== Date: Thu, 1 Aug 2024 17:30:00 +0200 From: Danilo Krummrich To: Alice Ryhl Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, akpm@linux-foundation.org, daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, acurrid@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v3 16/25] rust: alloc: implement `IntoIterator` for `Vec` Message-ID: References: <20240801000641.1882-1-dakr@kernel.org> <20240801000641.1882-17-dakr@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 738251C0015 X-Stat-Signature: edcnqosuookk9ind5wn6p1u91dgexcpo X-HE-Tag: 1722526209-81625 X-HE-Meta: U2FsdGVkX1/Pr5jeH/ZJDm5lJNYfZOtSMKgn2RK47ZvupA+JuLnh8DdQ1woyzKsoO1zxZSzR6lA2tXy5aCKNu1q7fumV49sNB22pbCJcp89UvA+0lLBm3k5mqs92kKhnfaSRb1O7bLXvrduySX48VMS5u3CQRz+HYoO11cP5+C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r8gYACoGwYek51LxhFwVz2jxR0FBSSqhkm0236mcCSD8BY10AhUpf4CHp7CSHoFywjTa8BYhx8dDH7N1kXzHLsuYCz6oPsinc/2KKzTwO6XD2h2JCMyf0cVhOYQbslBGvGaGdLBgqqO2tNyrRrajVwnl61S3RWgNhGSNdxosM09Zns68 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Thu, Aug 01, 2024 at 05:07:48PM +0200, Alice Ryhl wrote: > On Thu, Aug 1, 2024 at 2:08 AM Danilo Krummrich wrote: > > > > 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 > > --- > > rust/kernel/alloc.rs | 1 + > > rust/kernel/alloc/kvec.rs | 186 ++++++++++++++++++++++++++++++++++++++ > > 2 files changed, 187 insertions(+) > > > > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > > index bd93140f3094..f2998ad57456 100644 > > --- a/rust/kernel/alloc.rs > > +++ b/rust/kernel/alloc.rs > > @@ -19,6 +19,7 @@ > > pub use self::kbox::KVBox; > > pub use self::kbox::VBox; > > > > +pub use self::kvec::IntoIter; > > pub use self::kvec::KVVec; > > pub use self::kvec::KVec; > > pub use self::kvec::VVec; > > diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs > > index 04cc85f7d92c..50e7705e5686 100644 > > --- a/rust/kernel/alloc/kvec.rs > > +++ b/rust/kernel/alloc/kvec.rs > > @@ -12,6 +12,8 @@ > > ops::DerefMut, > > ops::Index, > > ops::IndexMut, > > + ptr, > > + ptr::NonNull, > > slice, > > slice::SliceIndex, > > }; > > @@ -581,3 +583,187 @@ fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] } > > __impl_slice_eq! { [A: Allocator] [T], Vec } > > __impl_slice_eq! { [A: Allocator, const N: usize] Vec, [U; N] } > > __impl_slice_eq! { [A: Allocator, const N: usize] Vec, &[U; N] } > > + > > +impl<'a, T, A> IntoIterator for &'a Vec > > +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 > > +where > > + A: Allocator, > > +{ > > + type Item = &'a mut T; > > + type IntoIter = slice::IterMut<'a, T>; > > + > > + fn into_iter(self) -> Self::IntoIter { > > + self.iter_mut() > > + } > > +} > > + > > +/// An iterator that moves out of a vector. > > +/// > > +/// This `struct` is created by the `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 { > > + ptr: *mut T, > > + buf: NonNull, > > + len: usize, > > + cap: usize, > > + _p: PhantomData, > > +} > > + > > +impl IntoIter > > +where > > + A: Allocator, > > +{ > > + fn as_raw_mut_slice(&mut self) -> *mut [T] { > > + ptr::slice_from_raw_parts_mut(self.ptr, self.len) > > + } > > +} > > + > > +impl Iterator for IntoIter > > +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 { > > + if self.len == 0 { > > + return None; > > + } > > + > > + let ptr = self.ptr; > > + if !Vec::::is_zst() { > > + // SAFETY: We can't overflow; `end` is guaranteed to mark the end of the buffer. > > + unsafe { self.ptr = self.ptr.add(1) }; > > + } else { > > + // For ZST `ptr` has to stay where it is to remain aligned, so we just reduce `self.len` > > + // by 1. > > + } > > + self.len -= 1; > > + > > + // SAFETY: `ptr` is guaranteed to point at a valid element within the buffer. > > + Some(unsafe { ptr.read() }) > > + } > > + > > + /// # Examples > > + /// > > + /// ``` > > + /// let v: KVec = 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) { > > + (self.len, Some(self.len)) > > + } > > +} > > + > > +impl Drop for IntoIter > > +where > > + A: Allocator, > > +{ > > + fn drop(&mut self) { > > + // SAFETY: Drop the remaining vector's elements in place, before we free the backing > > + // memory. > > + unsafe { ptr::drop_in_place(self.as_raw_mut_slice()) }; > > + > > + // If `cap == 0` we never allocated any memory in the first place. > > + if self.cap != 0 { > > + // SAFETY: `self.buf` was previously allocated with `A`. > > + unsafe { A::free(self.buf.cast()) }; > > + } > > Is this ok for ZST? Yes, for ZST `self.cap` is always zero. > > > + } > > +} > > + > > +impl IntoIterator for Vec > > +where > > + A: Allocator, > > +{ > > + type Item = T; > > + type IntoIter = IntoIter; > > + > > + /// Creates a consuming iterator, that is, one that moves each value out of > > + /// the vector (from start to end). The vector cannot be used after calling > > + /// this. > > + /// > > + /// # Examples > > + /// > > + /// ``` > > + /// let v = kernel::kvec![1, 2]?; > > + /// let mut v_iter = v.into_iter(); > > + /// > > + /// let first_element: Option = 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 = v_iter.next(); > > + /// > > + /// assert_eq!(first_element, None); > > + /// > > + /// # Ok::<(), Error>(()) > > + /// ``` > > + #[inline] > > + fn into_iter(self) -> Self::IntoIter { > > + let (ptr, len, cap) = self.into_raw_parts(); > > + > > + IntoIter { > > + ptr, > > + // SAFETY: `ptr` is either a dangling pointer or a pointer to a valid memory > > + // allocation, allocated with `A`. > > + buf: unsafe { NonNull::new_unchecked(ptr) }, > > + len, > > + cap, > > + _p: PhantomData::, > > + } > > + } > > +} > > -- > > 2.45.2 > > >