mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: Danilo Krummrich <dakr@kernel.org>
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>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"Alexandre Courbot" <acourbot@nvidia.com>
Subject: Re: [PATCH v9] rust: kernel: add support for bits/genmask macros
Date: Wed, 16 Jul 2025 16:11:59 -0300	[thread overview]
Message-ID: <705CD461-60D9-492E-B82F-C88A848A4348@collabora.com> (raw)
In-Reply-To: <DBDP54SLN4EZ.2EQ004NXWCX2L@kernel.org>

Hi Danilo,

> 
> Just for reference, I asked some questions regarding this code in [1].
> 
> Additional to "Why does genmask_u64(0..=100) compile?" I would expect the
> corresponding genmask_checked_u64() call to return None.
> 
> [1] https://lore.kernel.org/lkml/DBDP0BJW9VAZ.5KRU4V4288R8@kernel.org/
> 

Let’s transfer this discussion to this patch.

> I also quickly tried genmask and I have a few questions:
> 
>  (1) Why does genmask not use a const generic? I think this makes it more
>      obvious that it's only intended to be used from const context.

I guess none of us thought about it, since the current version also works.

> 
>  (2) Why is there no build_assert() when the range exceeds the number of bits
>      of the target type? I would expect genmask_u64(0..100) to fail.

Doesn’t it?

There is a build_assert in the underlying bit implementation. It was redundant
to have it both in bit_* and in genmask, because genmask calls bit().

In your example, bit_u64(100) hits that assert, and so it shouldn't compile.

Note that I can't add a test for this because "compile_fail" tests are not
supported yet, but there is a test for the checked version, i.e.:

+    /// // `80` is out of the supported bit range.
+    /// assert_eq!(genmask_checked_u64(21..=80), None);

> Additional to "Why does genmask_u64(0..=100) compile?" I would expect the
> corresponding genmask_checked_u64() call to return None.

The test above proves that it returns None.

> 
>  (3) OOC, why did you choose u32 as argument type?

No reason. i32 is the default integer type and signed integers don’t make
sense here, so I chose u32.

Also, we can trivially promote integers to wider types,  but the other way
around is not true. So my reasoning was that if you had u8, or u16s you could
trivially get u32s using into(), but if you had u32s and e.g. genmask_u16
required u16s, you'd have to resort to try_into() or `as`, which is annoying.

In any case, feel free to suggest anything else, I think.

— Daniel

  reply	other threads:[~2025-07-16 19:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-14 23:29 Daniel Almeida
2025-07-16 18:38 ` Danilo Krummrich
2025-07-16 19:11   ` Daniel Almeida [this message]
2025-07-16 19:18     ` Daniel Almeida
2025-07-16 19:32     ` Danilo Krummrich
2025-07-16 19:44       ` Daniel Almeida
2025-07-16 19:49         ` Danilo Krummrich
2025-07-16 20:06           ` Danilo Krummrich
2025-07-16 20:07 ` Danilo Krummrich
2025-07-19 22:17 ` Miguel Ojeda

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=705CD461-60D9-492E-B82F-C88A848A4348@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.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

all inboxes | Powered by JetHome®