Re: [PATCH 04/13] Kbuild: Rust support
From: Miguel Ojeda <hidden>
Date: 2021-04-16 17:47:47
Also in:
linux-doc, linux-kbuild, lkml
From: Miguel Ojeda <hidden>
Date: 2021-04-16 17:47:47
Also in:
linux-doc, linux-kbuild, lkml
On Fri, Apr 16, 2021 at 7:05 PM Linus Torvalds [off-list ref] wrote:
Typical Rust error handling should match the regular kernel IS_ERR/ERR_PTR/PTR_ERR model fairly well, although the syntax is fairly different (and it's not limited to pointers).
Yeah, exactly. We already have a `KernelResult<T>` type which is a
`Result<T, Error>`, where `Error` is a wrapper for the usual kernel
int errors.
So, for instance, a function that can either fail or return `Data`
would have a declaration like:
pub fn foo() -> KernelResult<Data>
A caller that needs to handle the error can use pattern matching or
one of the methods in `Result`. And if they only need to bubble the
error up, they can use the ? operator:
pub fn bar() -> KernelResult<Data> {
let data = foo()?;
// `data` is already a `Data` here, not a `KernelResult<Data>`
}
Cheers,
Miguel