From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_RED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 986A0C433ED for ; Fri, 16 Apr 2021 18:10:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7A4EA613B7 for ; Fri, 16 Apr 2021 18:10:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244326AbhDPSKr (ORCPT ); Fri, 16 Apr 2021 14:10:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237802AbhDPSKq (ORCPT ); Fri, 16 Apr 2021 14:10:46 -0400 Received: from zeniv-ca.linux.org.uk (zeniv-ca.linux.org.uk [IPv6:2607:5300:60:148a::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1451EC061574; Fri, 16 Apr 2021 11:10:21 -0700 (PDT) Received: from viro by zeniv-ca.linux.org.uk with local (Exim 4.94 #2 (Red Hat Linux)) id 1lXSul-005p9O-6e; Fri, 16 Apr 2021 18:09:59 +0000 Date: Fri, 16 Apr 2021 18:09:59 +0000 From: Al Viro To: Miguel Ojeda Cc: Linus Torvalds , Peter Zijlstra , Miguel Ojeda , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, Linux Kbuild mailing list , "open list:DOCUMENTATION" , Linux Kernel Mailing List , Alex Gaynor , Geoffrey Thomas , Finn Behrens , Adam Bratschi-Kaye , Wedson Almeida Filho , Michael Ellerman Subject: Re: [PATCH 04/13] Kbuild: Rust support Message-ID: References: <20210414184604.23473-1-ojeda@kernel.org> <20210414184604.23473-5-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-doc@vger.kernel.org On Fri, Apr 16, 2021 at 07:47:32PM +0200, Miguel Ojeda wrote: > On Fri, Apr 16, 2021 at 7:05 PM Linus Torvalds > 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` type which is a > `Result`, 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 > > 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 { > let data = foo()?; > > // `data` is already a `Data` here, not a `KernelResult` > } 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.