From patchwork Fri Dec 2 16:14:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 28966 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp947688wrr; Fri, 2 Dec 2022 08:18:18 -0800 (PST) X-Google-Smtp-Source: AA0mqf4fl4ShPHOB/cenOQRMiWApXslbn16LLEvl2HoSOtby86Gb5G0ypqsAqvcOJ+dWYh0wlgE2 X-Received: by 2002:a17:902:7105:b0:186:ff4a:a1f0 with SMTP id a5-20020a170902710500b00186ff4aa1f0mr67449575pll.57.1669997897355; Fri, 02 Dec 2022 08:18:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669997897; cv=none; d=google.com; s=arc-20160816; b=BGVf3HOqUpVq7YzVWmnukM+i3qi8WPS3LhXlnADk6OrAiCSi/FwfbZHYn5rdZLjHyh NyV9X1LMLqtz6EuOqh0m+dsO+z40hw8N3L1Ff8h908TPgv/VCIJYuJP5rthSdFa7YLjF +aZ5XowZ7u0m+sKcvsWkP/d1CbJxEMFGrm73D4nhYQqsiM8OofKH1AFaSuBieSDsloCY 652dP2t4RohRlmhdzvcUA2Aww4kWz+aHHmLtOfZrEShtWTlWkA0Yo05yxJQx0kAgG04a Z5apcHajkL5Aqbbw/M95/7lXnc3MXTa6xNcDN3QmE9Xr9B/ekB2w3Z0lEkXnTyuEimYW 0+uQ== 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 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.03; Fri, 02 Dec 2022 08:18:17 -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 + 99 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_Roy_Baron?= Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Adam Bratschi-Kaye , =?utf-8?b?TsOhbmRvciBJc3R2w6FuIEtyw6Fjc2Vy?= , 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119715371251046?= X-GMAIL-MSGID: =?utf-8?q?1751119715371251046?= 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;