From patchwork Thu Nov 10 16:41:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 18211 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp251686wru; Thu, 10 Nov 2022 08:44:28 -0800 (PST) X-Google-Smtp-Source: AMsMyM4nXD5OMylyDH65Oam5rp99eImtRM1MsV8bk4fwSPkJOK9sEYYyVFKFzgK6aDuBQBSActoE X-Received: by 2002:a17:906:6155:b0:79f:e0b3:3b9b with SMTP id p21-20020a170906615500b0079fe0b33b9bmr3085710ejl.378.1668098668615; Thu, 10 Nov 2022 08:44:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668098668; cv=none; d=google.com; s=arc-20160816; b=GnNo30hlMF8hrDM1Vw6iKyNq/XqU9s1TW0VIUbTx3LKqxu+QE38pzBOTld9Irn6MFh 4NiY2mreHg2fSY3U04+KBStnrBc9Mg86KnWNfsjdWOyo98sJXOXg1GO9kuIUBjeoCU6L lmgRKZKBvCz/hHo8r0l5knasWugsPpDzErNBH5kLP0wgi0SGYLs79R+qMUDDa1sjMADo KOn1IZy7MENhM5johu1vZ40C5R8cObzC5vw3/yX2AxcYSMayUFz+i/eJXRBYF+Vnkg4d M0lPkwZlV9cIUJEwnuwdt6HiLcOlwQwZxmPzufiHOhNKAPbvdfBmrracxxGmGv5M6qII RQTQ== 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=GEALJcaN68Zb4QzXM43BVOkHunzRyZjiJ4KxVcRED/k=; b=dTsiVKIP02Uro3vjHVAiznB/nOXdPtp42WHe5dYPvIRFiiVvgPAqw9+n0/R4hVe8j9 xdMCQSu/SgUb1RdpDKM4cPtKDULUojgYQTzZXLyDfqHugQPhEruUDDmim+O14PKfWwQn K9z7g4RCWgoYRipxTz2dl6lnlnYBfIP+CA7cW5K3DANwws3SbS7j9k5UT0dQXdcWR0az Fzh7jyuuuXSqptNpgRZoF76QDAu4h90lrer1nhFlJyL9kVlm5PrfM2C6TVwqG6gQfdEa +bnRNw1bqaA8dKPHu5bULF1FwaUFPNRJjwV6KW/VxGpCiG7O1Ld0NF9W6Sv32k0lAprW PQmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WSG25kTp; 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 ed15-20020a056402294f00b0045cfb63a033si21495edb.551.2022.11.10.08.44.04; Thu, 10 Nov 2022 08:44:28 -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=WSG25kTp; 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 S232234AbiKJQnd (ORCPT + 99 others); Thu, 10 Nov 2022 11:43:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232087AbiKJQnI (ORCPT ); Thu, 10 Nov 2022 11:43:08 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8213845A2C; Thu, 10 Nov 2022 08:42:52 -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 sin.source.kernel.org (Postfix) with ESMTPS id CE2D2CE2328; Thu, 10 Nov 2022 16:42:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90A23C433C1; Thu, 10 Nov 2022 16:42:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668098569; bh=7/BYuBysk0ovFW2IxRe1BxcDiN48GCNf/QdZo7baLig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WSG25kTpYBQoKiCLhSZTPhkydYrJwchGquJUJx4Y9FJC1OEa+uK/gflPhWRa+hZua YV48APql0DDQMaQxHYWDX/4ofRlj0tp5tLr1bsx//+9I3IIEjEDtjhm0GTyYX+J2Nr tFb7l87VZ6VbX3o3GPdwD8OfVtkUP266mzSoO4SvHIyYMh+hAMTkareFordH0ZLG4N 5/SBBKVarR5GCjlePpwbDvXzM4mdInmRTUziiDg82udpkJ11F5eaz2O5OK212qSgHv UWuQ7CJ6cj+mfTyBjJU9j4qogTmhl5ffPd1eG4OI3e5l2L331qh9ul7AGPYr/Blnrp Zpu64hINj4AWA== From: Miguel Ojeda 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?= Subject: [PATCH v1 10/28] rust: error: add `From` implementations for `Error` Date: Thu, 10 Nov 2022 17:41:22 +0100 Message-Id: <20221110164152.26136-11-ojeda@kernel.org> In-Reply-To: <20221110164152.26136-1-ojeda@kernel.org> References: <20221110164152.26136-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?1749128229591799823?= X-GMAIL-MSGID: =?utf-8?q?1749128229591799823?= 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 [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda Reviewed-by: Finn Behrens --- 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;