From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Daniel Almeida" <daniel.almeida@collabora.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
nouveau@lists.freedesktop.org
Subject: Re: [PATCH v2 2/4] rust: add `Alignment` type
Date: Tue, 05 Aug 2025 22:18:56 +0900 [thread overview]
Message-ID: <DBUIVGA74QX5.2KDKFG809YZ0A@nvidia.com> (raw)
In-Reply-To: <4A13D612-F5A6-4D7C-A2FC-2FF54646D4E4@collabora.com>
On Tue Aug 5, 2025 at 12:47 AM JST, Daniel Almeida wrote:
<snip>
>> +/// [`align_down!`] macros.
>> +///
>> +/// Heavily inspired by the [`Alignment`] nightly feature from the Rust standard library, and
>> +/// hopefully to be eventually replaced by it.
>
> It’s a bit hard to parse this.
>
> Also, I wonder if we should standardize some syntax for TODOs so we can parse
> them using a script? This way we can actually keep track and perhaps pipe them
> to our GitHub page as “good first issues” or just regular issues.
>
> I guess a simple "// TODO: “ here will do, for example.
FWIW, in Nova we tag each TODO items with a 4-letter identifier (i.e.
`TODO[ABCD]:` that is defined in our `todo.rst` file. This makes
grepping all the sites relevant to a given item easy.
>
>> +///
>> +/// [`Alignment`]: https://github.com/rust-lang/rust/issues/102070
>> +///
>> +/// # Invariants
>> +///
>> +/// An alignment is always a power of two.
>> +#[repr(transparent)]
>> +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
>> +pub struct Alignment(NonZero<usize>);
>> +
>> +impl Alignment {
>> + /// 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) })
>> + }
>> +
>> + /// Validates that `align` is a power of two at runtime, and returns an
>> + /// [`Alignment`] of the same value.
>> + ///
>> + /// [`None`] is returned if `align` was not a power of two.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::ptr::Alignment;
>> + ///
>> + /// assert_eq!(Alignment::new_checked(16), Some(Alignment::new(16)));
>> + /// assert_eq!(Alignment::new_checked(15), None);
>> + /// assert_eq!(Alignment::new_checked(1), Some(Alignment::new(1)));
>> + /// assert_eq!(Alignment::new_checked(0), None);
>> + /// ```
>> + #[inline(always)]
>> + pub const fn new_checked(align: usize) -> Option<Self> {
>> + if align.is_power_of_two() {
>> + // INVARIANT: `align` is a power of two.
>> + // SAFETY: `align` is a power of two, and thus non-zero.
>> + Some(Self(unsafe { NonZero::new_unchecked(align) }))
>> + } else {
>> + None
>> + }
>> + }
>> +
>> + /// Returns the alignment of `T`.
>> + #[inline(always)]
>> + pub const fn of<T>() -> Self {
>> + // INVARIANT: `align_of` always returns a power of 2.
>> + Self(unsafe { NonZero::new_unchecked(align_of::<T>()) })
>> + }
>
>> +
>> + /// Returns the base-2 logarithm of the alignment.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::ptr::Alignment;
>> + ///
>> + /// assert_eq!(Alignment::of::<u8>().log2(), 0);
>> + /// assert_eq!(Alignment::new(16).log2(), 4);
>> + /// ```
>> + #[inline(always)]
>> + pub const fn log2(self) -> u32 {
>> + self.0.ilog2()
>> + }
>> +
>> + /// Returns this alignment as a [`NonZero`].
>> + ///
>> + /// It is guaranteed to be a power of two.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::ptr::Alignment;
>> + ///
>> + /// assert_eq!(Alignment::new(16).as_nonzero().get(), 16);
>> + /// ```
>> + #[inline(always)]
>> + pub const fn as_nonzero(self) -> NonZero<usize> {
>> + if !self.0.is_power_of_two() {
>> + // SAFETY: per the invariants, `self.0` is always a power of two so this block will
>> + // never be reached.
>> + unsafe { core::hint::unreachable_unchecked() }
>> + }
>> + self.0
>> + }
>> +
>> + /// Returns this alignment as a `usize`.
>> + ///
>> + /// It is guaranteed to be a power of two.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::ptr::Alignment;
>> + ///
>> + /// assert_eq!(Alignment::new(16).as_usize(), 16);
>> + /// ```
>> + #[inline(always)]
>> + pub const fn as_usize(self) -> usize {
>> + self.as_nonzero().get()
>> + }
>> +
>> + /// Returns the mask corresponding to `self.as_usize() - 1`.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// use kernel::ptr::Alignment;
>> + ///
>> + /// assert_eq!(Alignment::new(0x10).mask(), 0xf);
>> + /// ```
>> + #[inline(always)]
>> + pub const fn mask(self) -> usize {
>> + // INVARIANT: `self.as_usize()` is guaranteed to be a power of two (i.e. non-zero), thus
>> + // `1` can safely be substracted from it.
>> + self.as_usize() - 1
>> + }
>> +
>> + /// Aligns `value` down to this alignment.
>> + ///
>> + /// If the alignment contained in `self` is too large for `T`, then `0` is returned, which
>> + /// is correct as it is also the result that would have been returned if it did.
>
> I half get this, but still: If it did what?
I also stumbled while re-reading this sentence. :) Fixed.
next prev parent reply other threads:[~2025-08-05 13:19 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-04 11:45 [PATCH v2 0/4] rust: add `Alignment` type Alexandre Courbot
2025-08-04 11:45 ` [PATCH v2 1/4] rust: add `CheckedAdd` trait Alexandre Courbot
2025-08-04 14:37 ` Daniel Almeida
2025-08-05 12:59 ` Alexandre Courbot
2025-08-04 11:45 ` [PATCH v2 2/4] rust: add `Alignment` type Alexandre Courbot
2025-08-04 14:17 ` Miguel Ojeda
2025-08-04 15:11 ` Benno Lossin
2025-08-05 13:13 ` Alexandre Courbot
2025-08-05 16:20 ` Benno Lossin
2025-08-04 15:47 ` Daniel Almeida
2025-08-05 13:18 ` Alexandre Courbot [this message]
2025-08-04 11:45 ` [PATCH v2 3/4] gpu: nova-core: use Alignment for alignment-related operations Alexandre Courbot
2025-08-04 11:45 ` [PATCH v2 4/4] gpu: nova-core: use `checked_ilog2` to emulate `fls` Alexandre Courbot
2025-08-04 14:16 ` [PATCH v2 0/4] rust: add `Alignment` type Miguel Ojeda
2025-08-05 13:26 ` Alexandre Courbot
2025-08-05 19:26 ` John Hubbard
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=DBUIVGA74QX5.2KDKFG809YZ0A@nvidia.com \
--to=acourbot@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nouveau@lists.freedesktop.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).