rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: Alice Ryhl <aliceryhl@google.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: "Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Marco Elver" <elver@google.com>,
	"Kees Cook" <keescook@chromium.org>, "Coly Li" <colyli@suse.de>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Pierre Gondois" <pierre.gondois@arm.com>,
	"Ingo Molnar" <mingo@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Wei Yang" <richard.weiyang@gmail.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v2 6/9] rust: list: add iterators
Date: Mon, 27 May 2024 10:31:35 +0000	[thread overview]
Message-ID: <cc65eeaf-81e1-45f5-8350-8e8259efe289@proton.me> (raw)
In-Reply-To: <20240506-linked-list-v2-6-7b910840c91f@google.com>

On 06.05.24 11:53, Alice Ryhl wrote:
> Rust Binder has lists containing stuff such as all contexts or all
> processes, and sometimes need to iterate over them. This patch enables

typo: need -> needs

> Rust Binder to do that using a normal for loop.
> 
> The iterator returns the ArcBorrow type, so it is possible to grab a
> refcount to values while iterating.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Two documentation nits below, with those fixed:

Reviewed-by: Benno Lossin <benno.lossin@proton.me>

> ---
>  rust/kernel/list.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 102 insertions(+)
> 
> diff --git a/rust/kernel/list.rs b/rust/kernel/list.rs
> index d0ff29a3e5d1..e36afc7ee44a 100644
> --- a/rust/kernel/list.rs
> +++ b/rust/kernel/list.rs
> @@ -5,7 +5,9 @@
>  //! A linked list implementation.
> 
>  use crate::init::PinInit;
> +use crate::sync::ArcBorrow;
>  use crate::types::Opaque;
> +use core::iter::{DoubleEndedIterator, FusedIterator};
>  use core::marker::PhantomData;
>  use core::ptr;
> 
> @@ -435,6 +437,17 @@ pub fn push_all_back(&mut self, other: &mut List<T, ID>) {
>          // INVARIANT: The other list is now empty, so update its pointer.
>          other.first = ptr::null_mut();
>      }
> +
> +    /// Creates an iterator over the list.
> +    pub fn iter(&self) -> Iter<'_, T, ID> {
> +        // INVARIANT: If the list is empty, both pointers are null. Otherwise, both pointers point
> +        // at the first element of the same list.
> +        Iter {
> +            current: self.first,
> +            stop: self.first,
> +            _ty: PhantomData,
> +        }
> +    }
>  }
> 
>  impl<T: ?Sized + ListItem<ID>, const ID: u64> Default for List<T, ID> {
> @@ -450,3 +463,92 @@ fn drop(&mut self) {
>          }
>      }
>  }
> +
> +/// An iterator into a [`List`].

I would use "over" instead of "into".

> +///
> +/// # Invariants
> +///
> +/// * There must be a [`List`] that is immutably borrowed for the duration of `'a`.
> +/// * The `current` pointer is null or points at a value in that [`List`].
> +/// * The `stop` pointer is equal to the `first` field of the [`List`].

the -> that

---
Cheers,
Benno

> +#[derive(Clone)]
> +pub struct Iter<'a, T: ?Sized + ListItem<ID>, const ID: u64 = 0> {
> +    current: *mut ListLinksFields,
> +    stop: *mut ListLinksFields,
> +    _ty: PhantomData<&'a ListArc<T, ID>>,
> +}


  reply	other threads:[~2024-05-27 10:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-06  9:53 [PATCH v2 0/9] Add Rust linked list for reference counted values Alice Ryhl
2024-05-06  9:53 ` [PATCH v2 1/9] rust: list: add ListArc Alice Ryhl
2024-05-27  9:25   ` Benno Lossin
2024-05-06  9:53 ` [PATCH v2 2/9] rust: list: add tracking for ListArc Alice Ryhl
2024-05-27  9:39   ` Benno Lossin
2024-05-06  9:53 ` [PATCH v2 3/9] rust: list: add struct with prev/next pointers Alice Ryhl
2024-05-27  9:58   ` Benno Lossin
2024-05-27 11:42     ` Alice Ryhl
2024-05-06  9:53 ` [PATCH v2 4/9] rust: list: add macro for implementing ListItem Alice Ryhl
2024-05-27 10:06   ` Benno Lossin
2024-05-06  9:53 ` [PATCH v2 5/9] rust: list: add List Alice Ryhl
2024-05-27 10:25   ` Benno Lossin
2024-05-06  9:53 ` [PATCH v2 6/9] rust: list: add iterators Alice Ryhl
2024-05-27 10:31   ` Benno Lossin [this message]
2024-05-06  9:53 ` [PATCH v2 7/9] rust: list: add cursor Alice Ryhl
2024-05-27 10:37   ` Benno Lossin
2024-05-06  9:53 ` [PATCH v2 8/9] rust: list: support heterogeneous lists Alice Ryhl
2024-05-27 10:46   ` Benno Lossin
2024-05-06  9:53 ` [PATCH v2 9/9] rust: list: add ListArcField Alice Ryhl
2024-05-27 10:50   ` Benno Lossin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cc65eeaf-81e1-45f5-8350-8e8259efe289@proton.me \
    --to=benno.lossin@proton.me \
    --cc=a.hindborg@samsung.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=colyli@suse.de \
    --cc=elver@google.com \
    --cc=gary@garyguo.net \
    --cc=keescook@chromium.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pierre.gondois@arm.com \
    --cc=richard.weiyang@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).