[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CANiq72=RLf0FiuLVL-ZeLFp9P2LxTymbzhXoyQGG=tvUY_J-Sg@mail.gmail.com>
Date: Fri, 16 Apr 2021 20:57:07 +0200
From: Miguel Ojeda <miguel.ojeda.sandonis@...il.com>
To: Al Viro <viro@...iv.linux.org.uk>
Cc: Linus Torvalds <torvalds@...ux-foundation.org>,
Peter Zijlstra <peterz@...radead.org>,
Miguel Ojeda <ojeda@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
rust-for-linux@...r.kernel.org,
Linux Kbuild mailing list <linux-kbuild@...r.kernel.org>,
"open list:DOCUMENTATION" <linux-doc@...r.kernel.org>,
Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Geoffrey Thomas <geofft@...reload.com>,
Finn Behrens <me@...enk.de>,
Adam Bratschi-Kaye <ark.email@...il.com>,
Wedson Almeida Filho <wedsonaf@...gle.com>,
Michael Ellerman <mpe@...erman.id.au>
Subject: Re: [PATCH 04/13] Kbuild: Rust support
On Fri, Apr 16, 2021 at 8:10 PM Al Viro <viro@...iv.linux.org.uk> wrote:
>
> How well would ? operator fit that pattern? _If_ it's just a syntax sugar
> along the lines of "if argument matches Err(_), return Err(_)", the types
> shouldn't be an issue, but that might need some fun with releasing resources,
> etc. If it's something more elaborate... details, please.
Yes, it is just syntax sugar -- it doesn't introduce any power to the language.
It was introduced because it is a very common pattern when using the
`Result` and `Option` enums. In fact, before it existed, it was just a
simple macro that you could also implement yourself.
For instance, given `Foo` and `Bar` types that need RAII cleanup of
some kind (let's say `kill_foo()` and `kill_bar()`):
fn foo() -> KernelResult<Foo> {
if black_box() {
return Err(EINVAL);
}
// something that gets you a `Foo`
let foo = ...;
Ok(foo)
}
fn bar() -> KernelResult<Bar> {
let p = foo()?;
// something that gets you a `Bar`, possibly using the `p`
let bar = ...;
Ok(bar)
}
This reduces to (full example at https://godbolt.org/z/hjTxd3oP1):
bar:
push rbx
mov ebx, 1
call qword ptr [rip + black_box@...PCREL]
test al, al
jne .LBB2_2
call qword ptr [rip + kill_foo@...PCREL]
xor ebx, ebx
.LBB2_2:
mov eax, ebx
mov edx, -1234
pop rbx
ret
You can see `bar()` calls `black_box()`. If it failed, it returns the
EINVAL. Otherwise, it cleans up the `foo` automatically and returns
the successful `bar`.
Cheers,
Miguel
Powered by blists - more mailing lists