Message ID | 20230224-rust-error-v2-4-3900319812da@asahilina.net |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp362637vqo; Wed, 29 Mar 2023 05:10:20 -0700 (PDT) X-Google-Smtp-Source: AKy350aHCAOZo3SIqTtA6pRbpbRwjdwBAsnmmXDOk3kmGIxQHw0TAI5MPQ6l2JYP6XUm1LgbFM4K X-Received: by 2002:a17:90b:3ec3:b0:23f:9448:89c2 with SMTP id rm3-20020a17090b3ec300b0023f944889c2mr21360686pjb.7.1680091819956; Wed, 29 Mar 2023 05:10:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680091819; cv=none; d=google.com; s=arc-20160816; b=oS/vetWfBXbB0Lh/9jvKfHFsoFv7Lrba4Gf/IdM7kLU/r82p2iqa+lu92tp+sLyigW HKxX5fUagjSpHcyW65fBUozMyiMAfSp43VDkdJo+8A+9OT49+QocdIwHD0rk2nfrrWAY mN8oc4fIoZluoogLaAIGOv9NlZbPPwBYklgsdd1eXmP9byyqlhXB+CAA5hKiAziKWSRM kpUqgRIpv0ZUGQK3HgJ7gt6Ap+w8XhsmknqksLKGIaHkLPajt/HFQKFZ5jN273WeqDgc SLJWKie2Iw4QOoLFV/c5Z/4phrQGNheLeOwQNBYH4cnP+QCm5SPPS9zynZxmUnf1KgQu 8dJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=f5drIn1BosmycdOXC/Def4OPHkX3J8O0CQK0F9anX2Q=; b=P7bs//vUX0bEJ7jjgxWkLADj7ZghYWtXz3VBH6tznWCMgZEI5CrXOGA4wHl3LExleK 8XQeR7xuHym+SaOyQ4enrcowD/y2Ag441Cca4AWxO8nonP8Y4gBSVb507B8YUrD8v4NY wDprPDEG5jZz5k19PAYZeIHI6l9ajw1fSvWSJz5kIZJ0kpnWaWq7AP/j7NUmzCiPnh9i 2eG65kawXnFvvR4q+4KTxkHiTZEPJDxFSj7+WX9PxSrkG2sKsPLLbvboirgOgaPaKcyA 7mZweDgj9XfrZGEXXf1xnzRWkFoeQukYe9tDBrf/ESJdfUSuFbbS3NZkoZrEKJiGD9CI 5/NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=O9YVbyPo; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a21-20020a63e855000000b0050bdca1b6eesi32584860pgk.861.2023.03.29.05.10.06; Wed, 29 Mar 2023 05:10:19 -0700 (PDT) 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=@asahilina.net header.s=default header.b=O9YVbyPo; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230079AbjC2MFK (ORCPT <rfc822;rua109.linux@gmail.com> + 99 others); Wed, 29 Mar 2023 08:05:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjC2MFD (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 29 Mar 2023 08:05:03 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03166468B; Wed, 29 Mar 2023 05:04:59 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 1B3A4424A5; Wed, 29 Mar 2023 12:04:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091498; bh=D4/twBr+bzIjY4ZGn+sbXBAJvi8KroM1AK8OX54iYyI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=O9YVbyPosEDBz1Y3e376jGTJdPZ56RObMPGEQKs2kbZDSMJLGz0iqM48RP9Bgg4jP 9j92qyyx8XYyVdP9cHvMvyyaXSRb9OtgPm1wKg/eiWnptEAI6XELAMSSY5aNs1Ar58 X9N0AK2DO4ogwJQwYo2KRRQAeLgzB9Wip9ZJ8n6/0XSPCiOka5Xs06nUzf1sWZ5JgZ LkPFlZMXMaX7E8pZ70aWPsUFsyoarZEJnCR6uF1GjvRmWt+nOWCFN/dmINb2qEsQUW svC9yWrzKhMMRJH07fzM0S9pvF5tlqwpfp2DDeaywrYCtFsFwugn3rLElXy4x5NthK px9nunpoJGpJg== From: Asahi Lina <lina@asahilina.net> Date: Wed, 29 Mar 2023 21:04:36 +0900 Subject: [PATCH v2 4/6] rust: error: Add to_result() helper MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-rust-error-v2-4-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@asahilina.net> To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Sven Van Asbroeck <thesven73@gmail.com> Cc: Fox Chen <foxhlchen@gmail.com>, Martin Rodriguez Reboredo <yakoyoku@gmail.com>, Andreas Hindborg <a.hindborg@samsung.com>, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina <lina@asahilina.net> X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680091478; l=1246; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=Pyrepc31fu1j2hIHl+GlAJgfJXkFKrgb4Ue04icQgQk=; b=Qi1Fy6I/4bHQHbB9VuqBIwCJsY+ax2RlS2ro/Kyr4QO8xcgJz+/zdD8IOdnkYaufWCDB//8ot kA5XhtU7hJlADXksO7SehAh9Q98rL6Ptixy/XzzymQEtuAjJBKlp5vR X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761703959872583022?= X-GMAIL-MSGID: =?utf-8?q?1761703959872583022?= |
Series |
rust: error: Add missing wrappers to convert to/from kernel error codes
|
|
Commit Message
Asahi Lina
March 29, 2023, 12:04 p.m. UTC
From: Wedson Almeida Filho <wedsonaf@gmail.com> Add a to_result() helper to convert kernel C return values to a Rust Result, mapping >=0 values to Ok(()) and negative values to Err(...), with Error::from_errno() ensuring that the errno is within range. Lina: Imported from rust-for-linux/rust, originally developed by Wedson as part of the AMBA device driver support. Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> Signed-off-by: Asahi Lina <lina@asahilina.net> --- rust/kernel/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+)
Comments
On 3/29/23 09:04, Asahi Lina wrote: > [...] > > + > +/// Converts an integer as returned by a C kernel function to an error if it's negative, and > +/// `Ok(())` otherwise. > +pub fn to_result(err: core::ffi::c_int) -> Result { > + if err < 0 { > + Err(Error::from_errno(err)) > + } else { > + Ok(()) > + } > +} > Reviewed-by: Martin Rodriguez Reboredo
On Wed, 29 Mar 2023 21:04:36 +0900 Asahi Lina <lina@asahilina.net> wrote: > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > Add a to_result() helper to convert kernel C return values to a Rust > Result, mapping >=0 values to Ok(()) and negative values to Err(...), > with Error::from_errno() ensuring that the errno is within range. > > Lina: Imported from rust-for-linux/rust, originally developed by Wedson > as part of the AMBA device driver support. > > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> > Signed-off-by: Asahi Lina <lina@asahilina.net> > --- > rust/kernel/error.rs | 10 ++++++++++ > 1 file changed, 10 insertions(+) > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs > index 659468bd1735..4f599c4d1752 100644 > --- a/rust/kernel/error.rs > +++ b/rust/kernel/error.rs > @@ -167,3 +167,13 @@ impl From<core::convert::Infallible> for Error { > /// it should still be modeled as returning a `Result` rather than > /// just an [`Error`]. > pub type Result<T = ()> = core::result::Result<T, Error>; > + > +/// Converts an integer as returned by a C kernel function to an error if it's negative, and > +/// `Ok(())` otherwise. > +pub fn to_result(err: core::ffi::c_int) -> Result { > + if err < 0 { > + Err(Error::from_errno(err)) > + } else { > + Ok(()) > + } > +} > Reviewed-by: Gary Guo <gary@garyguo.net>
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 659468bd1735..4f599c4d1752 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -167,3 +167,13 @@ impl From<core::convert::Infallible> for Error { /// it should still be modeled as returning a `Result` rather than /// just an [`Error`]. pub type Result<T = ()> = core::result::Result<T, Error>; + +/// Converts an integer as returned by a C kernel function to an error if it's negative, and +/// `Ok(())` otherwise. +pub fn to_result(err: core::ffi::c_int) -> Result { + if err < 0 { + Err(Error::from_errno(err)) + } else { + Ok(()) + } +}