Message ID | 20230224-rust-error-v1-3-f8f9a9a87303@asahilina.net |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp788052wrd; Fri, 24 Feb 2023 01:00:26 -0800 (PST) X-Google-Smtp-Source: AK7set/2hwKVTRnkKg4K4qxmNjZ7lKE9flcthFN+0n0xDOasUzZefZWEyGP+2us8rtsI+fWpDKdC X-Received: by 2002:a17:902:eecc:b0:19c:bcb1:d75c with SMTP id h12-20020a170902eecc00b0019cbcb1d75cmr4548517plb.30.1677229225986; Fri, 24 Feb 2023 01:00:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229225; cv=none; d=google.com; s=arc-20160816; b=AOMsZXH1UkSzxPE4lglFVsrjwXAY9mVV9pn7KZ4M/TaWZAj6Ku7wi+huNUzKyAT1Yx FLyvgz9wWIs8KdxUbxDaO3MwMD258yJQtFKLEX/rNaAme+7WC0Z1NRj6J/7ovvOBa/Ub M5ZV40zfbczb0LscArsbsNU99ZyH3wQ1daRdApSQBMtPuBsSUa/wS+FwT66w4J/ceK1W IQLJ/42MsndbzBloqxSZzWGDxoFKD28SlhrD8gYp6KJT63lgaD4jZqDw2QChvX6dSnsQ eRwoMoTpliSaHX50xXFT3BTpT13fxWthfXgxIM6nJeShJyDa2oeOyAWpbfciivRSDkZM 5HMQ== 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=4H/xJofOehgvcVDOmzK4Ssg9pc4KRdoxRDiCdgz/3NQ=; b=TGQMpcrRuQnNbdQ4uuvflBKN5/Xlor08J0hrrOexE9fbCmmvG3MNjOIJN1GsC+FkKb dzUUfh/TD9muu2GuD3kYJJ8QGcBej11V55y6UQKSYiMx//a88FAcHW3wLkmv+mez6lMM 0G4vVKoVpTQOAytm+YsFrhLt4oLVDXa/LlfUV2aB1dNGvsEZN3fwFA3YJjsF5DI7TnW7 vfK/nbkb7SniNziQnTUZ3fRleqPf1asYHKdRr1Hj+o3adqq6/ZoNpDs6V4L8Vn5eFOCw npjq02j2kg7SFaAQR/bKFu/OEcXCW7z9BEjPrlqe3g8gRn63zPKOimRHZLz5wmVackCa wkcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=eADOkSXn; 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 kd4-20020a17090313c400b001943dcf2035si2674087plb.460.2023.02.24.01.00.12; Fri, 24 Feb 2023 01:00:25 -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=@asahilina.net header.s=default header.b=eADOkSXn; 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 S229895AbjBXIu5 (ORCPT <rfc822;jeff.pang.chn@gmail.com> + 99 others); Fri, 24 Feb 2023 03:50:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229874AbjBXIut (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 24 Feb 2023 03:50:49 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5877165CDC; Fri, 24 Feb 2023 00:50:45 -0800 (PST) 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 615E73FA55; Fri, 24 Feb 2023 08:50:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677228644; bh=kJHc+snPjIgQ0x5b0wG6oWK+hDCWiSQ0+kJLlveg4g4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=eADOkSXnOnrG+bRm9Lumm0rkE0iFgIIYcbPQEVc6tAjTSY7inqONtJOJBFpISJfvU 514K3eerbniJ1CtxpN7Hkb3h0e8Z5BF/DqUq4gzG3bNkfzPCDqO6Jr0Q/LPt7watiM rvdT+A+mCY9kkpLVHYnRBlhTNl/SYg2jzo8cKL615LzPHdQiWhRFF8k6BWbDsvkmV1 +u221r3xiv/Vk2G4Zoxi5COvkD69p8nXqnTGtREa+qDl0gPCQ+Zhh/OWTPS0AH9Yk1 cr0/3fZWwsTsBbxPpwnJAaEbCzbai54MGUNTedh4M6NfIfmbL2/Fp7ZDI9yjSKcyHW t+Q1Yii2HzaTw== From: Asahi Lina <lina@asahilina.net> Date: Fri, 24 Feb 2023 17:50:21 +0900 Subject: [PATCH 3/5] 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-v1-3-f8f9a9a87303@asahilina.net> References: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> In-Reply-To: <20230224-rust-error-v1-0-f8f9a9a87303@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>, 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=1677228630; l=1204; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=NH9SKYYkrqlm+FkWH/ZnDX0pl4F1HcPQKt111GaT3sI=; b=KCT/Vytm/SqVGGDB3HJRNnWM8vV7m/eg//dyk/XUccOo+pJEshAuoTMhBGxReTX91zNPMWhlm Lcfou1e9YyICK6k6MRMrXWr6EYJv1btKFyGKZxWjqu1Xom8w/AQwjpF X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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: <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?1758702312688852921?= X-GMAIL-MSGID: =?utf-8?q?1758702312688852921?= |
Series |
rust: error: Add missing wrappers to convert to/from kernel error codes
|
|
Commit Message
Asahi Lina
Feb. 24, 2023, 8:50 a.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_kernel_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> Signed-off-by: Asahi Lina <lina@asahilina.net> --- rust/kernel/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+)
Comments
Asahi Lina <lina@asahilina.net> writes: > 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_kernel_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> > Signed-off-by: Asahi Lina <lina@asahilina.net> > --- Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> > rust/kernel/error.rs | 10 ++++++++++ > 1 file changed, 10 insertions(+) > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs > index 3b439fdb405c..1e8371f28746 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_kernel_errno(err)) > + } else { > + Ok(()) > + } > +}
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 3b439fdb405c..1e8371f28746 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_kernel_errno(err)) + } else { + Ok(()) + } +}