rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Brendan Shephard <bshephar@bne-home.net>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: Alexandre Courbot <acourbot@nvidia.com>,
	dakr@kernel.org, airlied@gmail.com, aliceryhl@google.com,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: Return Option from page_align and ensure no usize overflow
Date: Fri, 28 Nov 2025 15:29:15 +1000	[thread overview]
Message-ID: <aSkzK4QDU6pN2GMS@fedora> (raw)
In-Reply-To: <CANiq72kiscT5euAUjcSzvxMzM9Hdj8aQGeUN_pVF-vHf3DhBuQ@mail.gmail.com>

On Fri, Nov 28, 2025 at 03:12:13AM +0100, Miguel Ojeda wrote:
> On Fri, Nov 28, 2025 at 1:29 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
> >
> > Ah, indeed. In that case, can we make the if arms symmetric:
> >
> >     if let Some(sum) = addr.checked_add(PAGE_SIZE - 1) {
> >         Some(sum & PAGE_MASK);
> >     } else {
> >         None
> >     }
> 
> Early return style makes the happy path less indented and is more
> consistent with the C side. However, here the happy case would be the
> indented case, so `let else` would be better:
> 
>     let Some(sum) = addr.checked_add(PAGE_SIZE - 1) else {
>         return None;
>     };
> 
>     Some(sum & PAGE_MASK)
> 
> Cheers,
> Miguel
> 

A wildcard entrant to this conversation. We could do a match instead if
you all might prefer that?

match addr.checked_add(PAGE_SIZE - 1) {
  Some(v) => Some(v & PAGE_MASK),
  None => None,
}

Something about that match syntax doesn't appeal to me though. I think I
prefer Miguel's suggestion here where we flip the happy and sad paths.
Make the sad path the early return and happy path the default. But I do
see at least a couple of `match` examples here, so it's probably worth a
mention to see what you think about it as an alternative?

  reply	other threads:[~2025-11-28  5:29 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27 13:07 [PATCH] rust: Return Option from page_align and ensure no usize overflow Brendan Shephard
2025-11-27 13:42 ` Alexandre Courbot
2025-11-27 14:18   ` Brendan Shephard
2025-11-28  0:28     ` Alexandre Courbot
2025-11-28  2:12       ` Miguel Ojeda
2025-11-28  5:29         ` Brendan Shephard [this message]
2025-11-28  6:51           ` Alexandre Courbot
2025-11-28  9:09             ` Alice Ryhl
2025-11-28 13:34               ` Alexandre Courbot
2025-11-27 13:44 ` Daniel Almeida
2025-11-27 14:21   ` Brendan Shephard
2025-11-27 14:40 ` [PATCH v2] " Brendan Shephard
2025-11-27 16:24   ` Miguel Ojeda
2025-11-28  3:35     ` Brendan Shephard

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=aSkzK4QDU6pN2GMS@fedora \
    --to=bshephar@bne-home.net \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=rust-for-linux@vger.kernel.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).