From: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
To: "Asahi Lina" <lina@asahilina.net>,
"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>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Sven Van Asbroeck" <thesven73@gmail.com>
Cc: Fox Chen <foxhlchen@gmail.com>,
Andreas Hindborg <a.hindborg@samsung.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
asahi@lists.linux.dev
Subject: Re: [PATCH v2 6/6] rust: error: Add from_result() helper
Date: Wed, 29 Mar 2023 11:55:01 -0300 [thread overview]
Message-ID: <bc99d578-0905-5cf6-db0d-1ebad2b3d9b2@gmail.com> (raw)
In-Reply-To: <20230224-rust-error-v2-6-3900319812da@asahilina.net>
On 3/29/23 09:04, Asahi Lina wrote:
> [...]
>
> +
> +/// Calls a closure returning a [`crate::error::Result<T>`] and converts the result to
> +/// a C integer result.
> +///
> +/// This is useful when calling Rust functions that return [`crate::error::Result<T>`]
> +/// from inside `extern "C"` functions that need to return an integer error result.
> +///
> +/// `T` should be convertible from an `i16` via `From<i16>`.
> +///
> +/// # Examples
> +///
> +/// ```ignore
> +/// # use kernel::from_result;
> +/// # use kernel::bindings;
> +/// unsafe extern "C" fn probe_callback(
> +/// pdev: *mut bindings::platform_device,
> +/// ) -> core::ffi::c_int {
> +/// from_result(|| {
> +/// let ptr = devm_alloc(pdev)?;
> +/// bindings::platform_set_drvdata(pdev, ptr);
> +/// Ok(0)
> +/// })
> +/// }
> +/// ```
> +// TODO: Remove `dead_code` marker once an in-kernel client is available.
> +#[allow(dead_code)]
> +pub(crate) fn from_result<T, F>(f: F) -> T
> +where
> + T: From<i16>,
> + F: FnOnce() -> Result<T>,
> +{
> + match f() {
> + Ok(v) => v,
> + // NO-OVERFLOW: negative `errno`s are no smaller than `-bindings::MAX_ERRNO`,
> + // `-bindings::MAX_ERRNO` fits in an `i16` as per invariant above,
> + // therefore a negative `errno` always fits in an `i16` and will not overflow.
> + Err(e) => T::from(e.to_errno() as i16),
> + }
> +}
>
Reviewed-by: Martin Rodriguez Reboredo
prev parent reply other threads:[~2023-03-29 14:55 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-29 12:04 [PATCH v2 0/6] rust: error: Add missing wrappers to convert to/from kernel error codes Asahi Lina
2023-03-29 12:04 ` [PATCH v2 1/6] rust: error: Rename to_kernel_errno() -> to_errno() Asahi Lina
2023-03-29 14:47 ` Martin Rodriguez Reboredo
2023-03-29 15:04 ` Miguel Ojeda
2023-03-29 18:16 ` Martin Rodriguez Reboredo
2023-03-29 22:33 ` Miguel Ojeda
2023-03-29 20:32 ` Gary Guo
2023-03-29 22:33 ` Miguel Ojeda
2023-03-29 12:04 ` [PATCH v2 2/6] rust: error: Add Error::to_ptr() Asahi Lina
2023-03-29 14:49 ` Martin Rodriguez Reboredo
2023-03-29 20:34 ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 3/6] rust: error: Add Error::from_errno() Asahi Lina
2023-03-29 14:51 ` Martin Rodriguez Reboredo
2023-03-29 20:35 ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 4/6] rust: error: Add to_result() helper Asahi Lina
2023-03-29 14:52 ` Martin Rodriguez Reboredo
2023-03-29 20:36 ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 5/6] rust: error: Add a helper to convert a C ERR_PTR to a `Result` Asahi Lina
2023-03-29 14:53 ` Martin Rodriguez Reboredo
2023-03-29 20:42 ` Gary Guo
2023-03-29 12:04 ` [PATCH v2 6/6] rust: error: Add from_result() helper Asahi Lina
2023-03-29 14:55 ` Martin Rodriguez Reboredo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bc99d578-0905-5cf6-db0d-1ebad2b3d9b2@gmail.com \
--to=yakoyoku@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=asahi@lists.linux.dev \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=foxhlchen@gmail.com \
--cc=gary@garyguo.net \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=thesven73@gmail.com \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.