lkml.org 
[lkml]   [2021]   [Apr]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    Date
    From
    SubjectRe: [PATCH 04/13] Kbuild: Rust support
    On Fri, Apr 16, 2021 at 07:47:32PM +0200, Miguel Ojeda wrote:
    > On Fri, Apr 16, 2021 at 7:05 PM Linus Torvalds
    > <torvalds@linux-foundation.org> 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>`
    > }

    Umm... A fairly common situation is

    foo() returns a pointer to struct foo instance or ERR_PTR()
    bar() returns a pointer to struct bar instance of ERR_PTR()

    bar()
    {
    struct foo *p;
    struct bar *res;
    .... // do some work, grab a mutex, etc.
    p = foo();
    if (IS_ERR(p))
    res = ERR_CAST(p); // (void *)p, internally; conceptually it's
    // ERR_PTR(PTR_ERR(p));
    else
    res = blah();
    .... // matching cleanup
    return res;
    }

    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.

    \
     
     \ /
      Last update: 2021-04-16 20:11    [W:2.522 / U:0.476 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site