On Sun, 7 Dec 2025 08:12:10 +0100
Dirk Behme <[email protected]> wrote:
> On 06.12.25 13:42, Zhi Wang wrote:
snip
> In the thread [1] there was some discussion about the `if {} else {}`
> "style". From that discussion I "distilled" 6 options [2] which I
> liked for having an overview :) Of course not all of these applied
> there (const), neither will they here. And all have pros and cons. I
> think in the end option #4 was selected.
>
> What's about to do something similar here (and in the 2/7 patch as
> well)?
>
> if vfs == 0 {
> return Err(ENODEV);
> }
>
> Ok(vfs)
>
> Dirk
>
Hey Dirk:
Thanks for the detailed summary! As a C programmer before, I tend to do
as above, because it improves a lot of readability on success path.
While playing with rust, I tend to use 5). Personally, I try to stay
away from if let chains, as some lifecycle changes happen in rust 2024
[1]. It wouldn't bite right now, but stay away from the bumps. :)
As Miguel's comment in another thread, I would improve this as
discussed.
[1]
https://doc.rust-lang.org/edition-guide/rust-2024/temporary-if-let-scope.html
> [1]
> https://lore.kernel.org/rust-for-linux/caniq72kisct5euaujcszvxmzm9hdj8aqgeun_pvf-vhf3dh...@mail.gmail.com/
>
> [2] Options distilled from the thread [1]:
>
> 1.
>
> if let Some(sum) = addr.checked_add(PAGE_SIZE - 1) {
> return Some(sum & PAGE_MASK);
> }
> None
>
>
> 2.
>
> addr.checked_add(PAGE_SIZE - 1).map(|sum| sum & PAGE_MASK)
>
>
> 3.
>
> if let Some(sum) = addr.checked_add(PAGE_SIZE - 1) {
> Some(sum & PAGE_MASK);
> } else {
> None
> }
>
>
> 4.
>
> let Some(sum) = addr.checked_add(PAGE_SIZE - 1) else {
> return None;
> };
>
> Some(sum & PAGE_MASK)
>
>
> 5.
>
> match addr.checked_add(PAGE_SIZE - 1) {
> Some(v) => Some(v & PAGE_MASK),
> None => None,
> }
>
> 6.
>
> Some(addr.checked_add(PAGE_SIZE - 1)? & PAGE_MASK)
>