Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2326679rwb; Fri, 2 Dec 2022 08:19:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf75kBuCgpnInCzZRzqb2zHhZabuQKWXmABLTMZWBv6uJRpmb2fn5GYsk7IwRwPK6Apld6dJ X-Received: by 2002:a17:902:d4cf:b0:186:80e7:1e1 with SMTP id o15-20020a170902d4cf00b0018680e701e1mr53294810plg.135.1669997940554; Fri, 02 Dec 2022 08:19:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669997940; cv=none; d=google.com; s=arc-20160816; b=ej5kh0hRe/o6YzlkdCKCtON2/P7Oa7VgkDAxMZ/nI8rnglRYRVvsYM++WiZSRZAltT sNA8JfFxGYtFYQcEil9wfOCNJZb2ypXRUpFNEoVa0NiBIMdWt/TGRIdHQX/olL+LreK0 RY6VGxFS8CG0FEnqAyb9m3yf5E/prDmyUYvCwXkO43SG8QB4muf/Wm3kGPw8LzMhgPg4 Zb8zL9zMjrfN77qeUvnDW/UYrQREpSjMdM7AZd+pniLREghsqNJjH6oymdrAysF+Q/Ij 3YQ7S7SvpEC1AeITRZDIoXI5D7O3mOLVfapOYC90UeV0TM3TMOOJ+EirB/QZg/QpXmTz J0Qw== 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:from :dkim-signature; bh=EkNrpAFqjipBzdHQQXtLZS8Vobai3szRR8mHotjHa0A=; b=UBSjlA46Ao0t+VlqvfgWJjgKEOmjZGVJU2J+FF4txgYbBafTHKhSQymh9f2kU+6iFd dEiOx4UdE8LUOqc8yh5VpQa1xkb5ccKdyC9mjDtc+7wQxePbEkPMj5hCh0McArStpv2Q foTboYfgHAcqYuZmiZMeBlfiAcSs4Fflbt3DNko8HpTi41jep+RIBVfJvroxBdxjdOdE yHV0HXwUWlRYXhRRWbpRPCJyGz05k1Hxrs7c5DDZnLbeeprnMk083wgm09QPzWGZ6zA8 UWqOVt1EyKmxZr7D+I/c7fJlI90aBu4EBBdFRoWxKeom3gt1nMyOPo8Pc8z4wrQepeEY qUrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CaLL6Fg6; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z1-20020a056a00240100b0057593ed4874si8456807pfh.315.2022.12.02.08.18.48; Fri, 02 Dec 2022 08:19:00 -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=@kernel.org header.s=k20201202 header.b=CaLL6Fg6; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233961AbiLBQQ4 (ORCPT + 83 others); Fri, 2 Dec 2022 11:16:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233895AbiLBQQ2 (ORCPT ); Fri, 2 Dec 2022 11:16:28 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2457DF62F; Fri, 2 Dec 2022 08:16:00 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 75525B821EF; Fri, 2 Dec 2022 16:15:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 788FAC433D7; Fri, 2 Dec 2022 16:15:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997758; bh=LHdw9TfUAyqQhYug7LeGlRw9Sq8mQsT00lw24iqeNao=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CaLL6Fg6EFH72SLa7DwuDVUZqmHFJMaZ7EzzJVbe4oDzaplP1xRuLk9saKkzY1qT8 +j/GZdi+3tbWT9/clKmNH8MOXegnJvagR8NW03O92ux16Y/gym2zV54UV9qqmqEcv7 QSSCN935YzhVvdOlrb8iU48O66LiAmJWY0QEJFR9Ohh/KPfwDQvvs6/zOtxlImXrOL kPjTgeOhsGrsnL4FI2dt0SIJSsdQ21YLkFaetBE5KjX/zC4weTPfAAxV+tw23y+Sxe r8A24ohFgqTwMJW3qltF4xhWMxAX/3/z3ql4RGBdDXy082LWz8bzLQ9sWf/TiL/MUi 776M4eiEvRmow== From: ojeda@kernel.org To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye , =?UTF-8?q?N=C3=A1ndor=20Istv=C3=A1n=20Kr=C3=A1cser?= , Finn Behrens Subject: [PATCH v2 10/28] rust: error: add `From` implementations for `Error` Date: Fri, 2 Dec 2022 17:14:41 +0100 Message-Id: <20221202161502.385525-11-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 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 = 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ándor István Krácser Signed-off-by: Nándor István Krácser Signed-off-by: Wedson Almeida Filho Reviewed-by: Finn Behrens [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda --- 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`](../../../include/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 works; -- 2.38.1