[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLggwK0LXAsBnP3FtFHCnEzzBNNZfsCy3iJ6w=nT07CHgVg@mail.gmail.com>
Date: Fri, 13 Dec 2024 14:40:39 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: Tamir Duberstein <tamird@...il.com>
Cc: Danilo Krummrich <dakr@...nel.org>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>, Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>, Maíra Canal <mcanal@...lia.com>,
Asahi Lina <lina@...hilina.net>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v12 2/2] rust: xarray: Add an abstraction for XArray
On Thu, Dec 12, 2024 at 9:31 PM Tamir Duberstein <tamird@...il.com> wrote:
>
> `XArray` is an efficient sparse array of pointers. Add a Rust
> abstraction for this type.
>
> This implementation bounds the element type on `ForeignOwnable` and
> requires explicit locking for all operations. Future work may leverage
> RCU to enable lockless operation.
>
> Inspired-by: Maíra Canal <mcanal@...lia.com>
> Inspired-by: Asahi Lina <lina@...hilina.net>
> Reviewed-by: Andreas Hindborg <a.hindborg@...nel.org>
> Signed-off-by: Tamir Duberstein <tamird@...il.com>
Reviewed-by: Alice Ryhl <aliceryhl@...gle.com>
This code looks okay to me, though I have one comment below:
> + // SAFETY: `self.xa` is always valid by the type invariant.
> + iter::once(unsafe {
> + bindings::xa_find(self.xa.get(), &mut index, MAX, bindings::XA_PRESENT)
> + })
> + .chain(iter::from_fn(move || {
> + // SAFETY: `self.xa` is always valid by the type invariant.
> + Some(unsafe {
> + bindings::xa_find_after(self.xa.get(), &mut index, MAX, bindings::XA_PRESENT)
> + })
> + }))
> + .map_while(|ptr| NonNull::new(ptr.cast()))
> + // SAFETY: `self.xa` is always valid by the type invariant.
> + (unsafe { bindings::xa_trylock(self.xa.get()) } != 0).then(|| Guard {
> + xa: self,
> + _not_send: NotThreadSafe,
> + })
This coding style is pretty far in the functional programming camp
compared to the rest of Rust code in the kernel. I tend to stick with
a more imperative style to be more familiar to C folks.
Alice
Powered by blists - more mailing lists