Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp943754rwb; Thu, 10 Nov 2022 09:11:06 -0800 (PST) X-Google-Smtp-Source: AMsMyM6cjfqviDq18v7eUYFTQ9RSEPAoig/Ve4gdvLyx/jRZm0ktJd0ujaWEGpxZgwayNA62SIGf X-Received: by 2002:a17:90b:4f45:b0:213:ccc6:87df with SMTP id pj5-20020a17090b4f4500b00213ccc687dfmr61633448pjb.227.1668100265801; Thu, 10 Nov 2022 09:11:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668100265; cv=none; d=google.com; s=arc-20160816; b=L0TIs6cRd+xZKWoLzw17AQJugdSQrIexWc7fkNZmaFByQennQD7AlOvx9ZkAL59sSK WXvLnyKywQ3ZVKUo+wW2nvXNJCQEzrx8KBPbCIf1kCd6r7Cs+TeWpDPajeqDmUFX4k1e ek74uFD5tJO3D9vXWt6KN9U9K+/hfIA6zrUac+T7DeXLNnlqAEFqkr/rYSXEf+/BELfT vkV3CajhFi8+LmkMRbaTpIMHjjIyQ0m7+bnky6T+tgiVhZaVDojIKZMf/FePdWq0X8/W e0U3XPSDSrddvGsX/fiXtD2AvTU9jxfrz8CSgUl7wxligHKynuAVSPxkeTWy3YRUyiq5 5Rvw== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i187-20020a6254c4000000b0056aff71af29si18709732pfb.209.2022.11.10.09.10.40; Thu, 10 Nov 2022 09:11:05 -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 + 93 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=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?= 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 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 [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