Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1935943rwb; Fri, 11 Nov 2022 02:38:45 -0800 (PST) X-Google-Smtp-Source: AA0mqf4RZWuoGFx90GU31odJCNQbWNGS1EEJHGUTNoxIhwUg9mrvj6DzU838fKwCPnmIBZcVM25m X-Received: by 2002:a17:906:a1d3:b0:78d:9022:f146 with SMTP id bx19-20020a170906a1d300b0078d9022f146mr1265283ejb.656.1668163125106; Fri, 11 Nov 2022 02:38:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668163125; cv=none; d=google.com; s=arc-20160816; b=Zuxp069yUHhnj8zKKXgnBKWEYSSaVGvf6h7TTQtoLoVJEdUGBCZzb5ZOKXONeF3d39 U5qB2hODNyE1Bnjq3mo6eAYA3Tujk1nST8B3REYOWLpKs6pG4E6LXJ7EAR4OWRd2yvZM EMYySTvDGKWkyvZuPMc5kyvgC4QOZC4VsCT5jFKOKtJ9cA77TPYR7dmXntX7mx/23cUo 7Hc56Xal10oa8xa9Gdvm1Nn3joqYIWHQfctvhGcqbiiousEOg3DwaK/JTZGeAfQFc9FO +yq+8un0xAAGXnwI0+F0z4AzCExWQaXndmQU5fugC1gm60tfBYLu9plqqhVvSGAooIOu 5FIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :from; bh=k/lbX2lCWCPhAd8PfCnpbmF965t2ayUlczN62ucvq74=; b=yrYlbxuUgKhMib80m1jvulSOGEDY49VhA8sAl1UC/0Ajbl38pDrFlR6mVULhsQx5PL tvwh9xUtuJcIO65bGmqBZtiCZ+HmZfp/jrTo7ljG6mxwBjJxXRmbcY4pEtUS9nT8vW6K ut1QI0bHn7ZpfPGaPMEnKz6nZzh7cFECs3RsMcbjZ0RITzDL2zuDhFZlIy2+fWB0SZ45 vHKLfAfIPBUQcyvq4y6eHZCUMOyavTH3651uzoqrSRvQkgHs82DYrBjp1xcZcXDu37zi ULdCIMRWEUQIA6q8aTSDkFGXSWz00KF4bYwGq+16qdNe7FE7APWQ2uqOCNHz7TnyQPsX nGrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kloenk.dev header.s=mail header.b=E93lyMGG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=kloenk.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o13-20020a170906974d00b007708400bee5si1801139ejy.1003.2022.11.11.02.38.20; Fri, 11 Nov 2022 02:38:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kloenk.dev header.s=mail header.b=E93lyMGG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=kloenk.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233570AbiKKJu7 (ORCPT + 92 others); Fri, 11 Nov 2022 04:50:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232182AbiKKJu5 (ORCPT ); Fri, 11 Nov 2022 04:50:57 -0500 X-Greylist: delayed 1502 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Fri, 11 Nov 2022 01:50:54 PST Received: from gimli.kloenk.dev (gimli.kloenk.dev [195.39.247.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFA57B78; Fri, 11 Nov 2022 01:50:54 -0800 (PST) From: Finn Behrens DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kloenk.dev; s=mail; t=1668160252; bh=k/lbX2lCWCPhAd8PfCnpbmF965t2ayUlczN62ucvq74=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=E93lyMGGH+YBdWFgAEPVyeQz5gFoksKISCRhPIAqTcZlcE6vIrpThQFNU8bkWVQzo lMklOWgoyPrf50+FShaUhBbUj+l9K2K4fusfc7Ae7G+LS/T0z3A27GE1znZvbyHFEQ 3r4y26RIJIec02R5f0BOGs8klEsCURiiqHwokgS8= To: Miguel Ojeda Cc: Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye , =?utf-8?b?TsOhbmRvciBJc3R2w6FuIEtyw6Fjc2Vy?= Subject: Re: [PATCH v1 10/28] rust: error: add `From` implementations for `Error` Date: Fri, 11 Nov 2022 10:50:51 +0100 Message-ID: <0FFA9049-1F58-4C8C-9EAD-8ABD4B0489A2@kloenk.dev> In-Reply-To: <20221110164152.26136-11-ojeda@kernel.org> References: <20221110164152.26136-1-ojeda@kernel.org> <20221110164152.26136-11-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10 Nov 2022, at 17:41, Miguel Ojeda wrote: > From: Wedson Almeida Filho > > Add a set of `From` implementations for the `Error` kernel type. > > These implementations allow to easily convert from standard Rust > error types to the usual kernel errors based on one of the `E*` > integer codes. > > On top of that, the question mark Rust operator (`?`) implicitly > performs a conversion on the error value using the `From` trait > when propagating. Thus it is extra convenient to use. > > For instance, a kernel function that needs to convert a `i64` into > a `i32` and to bubble up the error as a kernel error may write: > > fn f(x: i64) -> Result<...> { > ... > let y =3D i32::try_from(x)?; > ... > } > > which will transform the `TryFromIntError` into an `Err(EINVAL)`. > > Co-developed-by: Adam Bratschi-Kaye > Signed-off-by: Adam Bratschi-Kaye > Co-developed-by: N=C3=A1ndor Istv=C3=A1n Kr=C3=A1cser > Signed-off-by: N=C3=A1ndor Istv=C3=A1n Kr=C3=A1cser > Signed-off-by: Wedson Almeida Filho > [Reworded, adapted for upstream and applied latest changes] > Signed-off-by: Miguel Ojeda Reviewed-by: Finn Behrens Regards, Finn > --- > rust/kernel/error.rs | 45 +++++++++++++++++++++++++++++++++++++++++++-= > rust/kernel/lib.rs | 1 + > 2 files changed, 45 insertions(+), 1 deletion(-) > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs > index 861746f2422d..5b9751d7ff1d 100644 > --- a/rust/kernel/error.rs > +++ b/rust/kernel/error.rs > @@ -4,7 +4,14 @@ > //! > //! C header: [`include/uapi/asm-generic/errno-base.h`](../../../inclu= de/uapi/asm-generic/errno-base.h) > > -use alloc::collections::TryReserveError; > +use alloc::{ > + alloc::{AllocError, LayoutError}, > + collections::TryReserveError, > +}; > + > +use core::convert::From; > +use core::num::TryFromIntError; > +use core::str::Utf8Error; > > /// Contains the C-compatible error codes. > pub mod code { > @@ -71,12 +78,48 @@ impl Error { > } > } > > +impl From for Error { > + fn from(_: AllocError) -> Error { > + code::ENOMEM > + } > +} > + > +impl From for Error { > + fn from(_: TryFromIntError) -> Error { > + code::EINVAL > + } > +} > + > +impl From for Error { > + fn from(_: Utf8Error) -> Error { > + code::EINVAL > + } > +} > + > impl From for Error { > fn from(_: TryReserveError) -> Error { > code::ENOMEM > } > } > > +impl From for Error { > + fn from(_: LayoutError) -> Error { > + code::ENOMEM > + } > +} > + > +impl From for Error { > + fn from(_: core::fmt::Error) -> Error { > + code::EINVAL > + } > +} > + > +impl From for Error { > + fn from(e: core::convert::Infallible) -> Error { > + match e {} > + } > +} > + > /// A [`Result`] with an [`Error`] error type. > /// > /// To be used as the return type for functions that may fail. > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index abd46261d385..ffc6626a6d29 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -12,6 +12,7 @@ > //! do so first instead of bypassing this crate. > > #![no_std] > +#![feature(allocator_api)] > #![feature(core_ffi_c)] > > // Ensure conditional compilation based on the kernel configuration wo= rks; > -- = > 2.38.1