[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DCNEM8ECAQED.1CTI4N9WE311P@nvidia.com>
Date: Mon, 08 Sep 2025 21:06:08 +0900
From: "Alexandre Courbot" <acourbot@...dia.com>
To: "Danilo Krummrich" <dakr@...nel.org>
Cc: "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" <lossin@...nel.org>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
"Trevor Gross" <tmgross@...ch.edu>, <linux-kernel@...r.kernel.org>,
<rust-for-linux@...r.kernel.org>, <nouveau@...ts.freedesktop.org>
Subject: Re: [PATCH v4 1/2] rust: add `Alignment` type
On Mon Sep 8, 2025 at 9:02 PM JST, Danilo Krummrich wrote:
> On 8/21/25 2:42 PM, Alexandre Courbot wrote:
>> + /// Validates that `align` is a power of two at build-time, and returns an [`Alignment`] of the
>> + /// same value.
>> + ///
>> + /// A build error is triggered if `align` cannot be asserted to be a power of two.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::ptr::Alignment;
>> + ///
>> + /// let v = Alignment::new(16);
>> + /// assert_eq!(v.as_usize(), 16);
>> + /// ```
>> + #[inline(always)]
>> + pub const fn new(align: usize) -> Self {
>> + build_assert!(align.is_power_of_two());
>> +
>> + // INVARIANT: `align` is a power of two.
>> + // SAFETY: `align` is a power of two, and thus non-zero.
>> + Self(unsafe { NonZero::new_unchecked(align) })
>> + }
>
> For DmaMask::new() we used a const generic instead, which makes it more obvious
> to the caller that the argument must be known at compile time. So, I'd prefer
> this here as well.
Ah, being consistent is good, and I think I prefer the const generic as
well. Let's do the same thing here.
Powered by blists - more mailing lists