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 95258EE01F1 for ; Tue, 10 Sep 2024 23:40:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FEEC8D00CA; Tue, 10 Sep 2024 19:40:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AEB78D0002; Tue, 10 Sep 2024 19:40:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 776088D00CA; Tue, 10 Sep 2024 19:40:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 00BD08D0002 for ; Tue, 10 Sep 2024 19:40:05 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 97A67C0DB6 for ; Tue, 10 Sep 2024 23:40:05 +0000 (UTC) X-FDA: 82550449170.16.8DB1289 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf14.hostedemail.com (Postfix) with ESMTP id EE1E0100011 for ; Tue, 10 Sep 2024 23:40:03 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UCEKcEF3; spf=pass (imf14.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011501; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YojkFdkxzi60vFwMAdkk1ohvGHJ6V+yMl7zduO0crOk=; b=XscYxKOMXuwyj8MuqNC2/Mlc5+PJwJL+fykbsojlbHsf6g7jlseL7HzQy4W5BCgQxzpa7T D6hkL3bp/wWNNpT7OYQIiVEeicw/1vAFPzuCJvqVTlE5+EzPf+nTmixOFARW5gd+dw7ewN OksKkz0jMge3vgciRQd1wxjUmqs/WXo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011501; a=rsa-sha256; cv=none; b=I7s5SAstnj06uvB/6rQ2JSYRLLlG+396oXVDY3H1pIUSfqWvsqfVOTPBESFZzZ1xPFwk6R VZfTzn0NPqAi90GTZukx8spNVzQxUOt8qTR5Xu+7CktKzZHiMu+LQcyv3tNQeZADm6c78p zBIJqv6ZxAeetbvoc/fp4s09Z5hgATE= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UCEKcEF3; spf=pass (imf14.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 70CC7A441B0; Tue, 10 Sep 2024 23:39:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9A86C4CEC3; Tue, 10 Sep 2024 23:39:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726011602; bh=NJVGgneIeHeVh/TWM2tmUema/6fUc/+nPEQT0jbcZ6s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=UCEKcEF3WIcYpamSJmlIkN9E05ecXJNc4rkPWh0Fw2+nMA8tYBZ09RE+a39ejDogu ANiKrBMTTnPrDh1DCjEGC+UTPIs2ys6gbcfpylsq9zX0nYZln1jBMDw1f6dE9nqEch IyUXyXuqHWure81bwl8Tf6y83R92DFU7ECiOxOmdDB0JoAajRjbjvNKxAM/uwhsUV2 Vgndf4lw4zMIms1sD14D21IbdB9+DvQNLdBDFYGAzOGuhymi9G/MXmXTAtrw8HK++D m5rBuAdgv4lOWievxaw8qkmIkj0S4xWHe4XduoAX+HKeVoFDqT0oQtXvbwzv2KuG8k Fi6cSsM7LxiSw== Date: Wed, 11 Sep 2024 01:39:55 +0200 From: Danilo Krummrich To: Benno Lossin Cc: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, a.hindborg@samsung.com, aliceryhl@google.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, 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 v6 14/26] rust: alloc: implement `IntoIterator` for `Vec` Message-ID: References: <20240816001216.26575-1-dakr@kernel.org> <20240816001216.26575-15-dakr@kernel.org> <308d40dd-105b-4ebf-ba70-585a97d66928@proton.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <308d40dd-105b-4ebf-ba70-585a97d66928@proton.me> X-Rspamd-Queue-Id: EE1E0100011 X-Stat-Signature: ab9ijda9ingizkahryjr4qe633n9bk36 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1726011603-185142 X-HE-Meta: U2FsdGVkX1+wXe/pLlyGnWxlflMQuos8ohvcdqoMGGBBkMCRowGdQLsUKhQt1wQPaxTIiofA57ygb80vLgl500AJon/rrMVAvx+JZIK+YlY6CToMNSOCxLSw4V943pegsgSrtYF7xjPYEodpxx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iBydJfIITC92qs2E8kDPdx8Cv56/cnKLWZpeMkGM51zm0OXS4p4gz020sOyI2Mvrxq4iN4o/3nUpFhIPV8I90QI/4SP/++Y4HAUpXuz3vV275hEAZ0TeBh5AWyTaGnk7hNOGWa4bvJ117Q9hAtvkrsujA3ztjm 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 Tue, Sep 10, 2024 at 08:04:27PM +0000, Benno Lossin wrote: > On 16.08.24 02:10, 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 | 184 ++++++++++++++++++++++++++++++++++++++ > > 2 files changed, 185 insertions(+) > > > > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > > index e88c7e10ee9b..4ff4df4597a3 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 89afc0f25bd4..3b79f977b65e 100644 > > --- a/rust/kernel/alloc/kvec.rs > > +++ b/rust/kernel/alloc/kvec.rs > > @@ -11,6 +11,7 @@ > > ops::DerefMut, > > ops::Index, > > ops::IndexMut, > > + ptr, > > ptr::NonNull, > > slice, > > slice::SliceIndex, > > @@ -627,3 +628,186 @@ 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` implementation for `Vec` that moves elements out of a vector. > > Please make both links. > > > +/// > > +/// This structure is created by the `Vec::into_iter` method on [`Vec`] (provided by the > > Ditto. > > > +/// [`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, > > No invariants for these two fields? Suggestions? > > > + 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>(()) > > + /// ``` > > AFAIK documentation on functions in trait implementations won't show up > in rustdoc (I just checked this). So I would remove it. They don't, but the KUnit tests are still executed. :) > > > + 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. > > Note that `<*mut T>::add` advances the pointer by `size_of::()` > bytes. So in the case that `T` is a ZST, it won't be advanced. > So you could remove this `if`. > > > + } > > + 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. > > This comment explains why you are doing it, not why it's ok to do it. > > > + 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()) }; > > + } > > + } > > +} > > + > > +impl IntoIterator for Vec > > +where > > + A: Allocator, > > +{ > > + type Item = T; > > + type IntoIter = IntoIter; > > + > > + /// Consumes the `Vec` 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 = 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>(()) > > + /// ``` > > I feel a bit bad that you wrote all of this nice documentation for > functions that receive their documentation from the trait... No worries, I really only added them for the KUnit tests. > > --- > Cheers, > Benno > > > + #[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.46.0 > > >