All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: "Onur Özkan" <work@onurozkan.dev>
Cc: rust-for-linux@vger.kernel.org, ojeda@kernel.org,
	alex.gaynor@gmail.com,  boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com,  lossin@kernel.org,
	a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org,
	 tamird@gmail.com
Subject: Re: [PATCH v3 2/2] rust: drop `error::to_result` and utilize `ToResult`
Date: Mon, 13 Oct 2025 18:29:19 +0000	[thread overview]
Message-ID: <aO1E_-KdnvpG5vXl@google.com> (raw)
In-Reply-To: <20251013124139.18809-3-work@onurozkan.dev>

On Mon, Oct 13, 2025 at 03:41:39PM +0300, Onur Özkan wrote:
> Current `to_result` helper takes a `c_int` and returns `Ok(())` on
> success and this has some issues like:
> 
>     - Callers lose the original return value and often have to store
> 	it in a temporary variable before calling `to_result`.
> 
>     - It only supports `c_int`, which makes callers to unnecessarily
> 	cast when working with other types (e.g. `u16` in phy
> 	abstractions). We even have some places that ignore to use
> 	`to_result` helper because the input doesn't fit in `c_int`
> 	(see [1]).
> 
> [1]: https://lore.kernel.org/all/20250822080252.773d6f54@nimda.home/
> 
> This patch removes the `error::to_result` function and replaces it
> with a more advanced helper, `ToResult` trait. This change brings
> three main benefits:
> 
>     - A better calling convention. Instead of wrapping function calls
> 	with `to_result`, we can now call `.to_result()` directly as an
> 	extension function.
> 
>     - The returned value is preserved as an unsigned integer on success
> 	which was previously discarded by error::to_result.
> 
>     - It's no longer limited with a single input type. E.g., right now we
> 	can call to_result on isize without manually casting them into i32.
> 
> So that the code that previously looked like:
> 
>     let ret = unsafe { bindings::some_ffi_call() };
>     to_result(ret).map(|()| SomeType::new(ret))
> 
> can now be written more directly as:
> 
>     unsafe { bindings::some_ffi_call() }.to_result()
> 	.map(SomeType::new)
> 
> Similarly, code such as:
> 
>     let res: isize = unsafe { bindings::some_ffi_call() };
>     if res < 0 {
> 	    return Err(Error::from_errno(res as i32));
>     }
> 
> can be done without manually casting into i32:
> 
>     unsafe { bindings::some_ffi_call() }.to_result()?;
> 
> This patch only fixes the callers that broke after changes on `to_result`.
> I haven't included all the improvements made possible by the new design
> since that could conflict with other ongoing patches [2]. Once this patch
> is approved and applied, I am planning to follow up with creating a
> "good first issue" on [3] for those additional changes.
> 
> [2]: https://lore.kernel.org/rust-for-linux/?q=to_result
> [3]: https://github.com/Rust-for-Linux/linux
> 
> Link: https://rust-for-linux.zulipchat.com/#narrow/channel/288089/topic/x/near/536374456
> Signed-off-by: Onur Özkan <work@onurozkan.dev>

I think we should keep the function call syntax and just change its
return type based on the new trait.

Or maybe even introduce a new method to keep the existing method that
returns Result<()>. Many functions only return 0 or an error, and I
don't want to map from int to () either.

Alice

      parent reply	other threads:[~2025-10-13 18:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-13 12:41 [PATCH v3 0/2] rust: refactor `to_result` Onur Özkan
2025-10-13 12:41 ` [PATCH v3 1/2] rust: add `ToResult` trait Onur Özkan
2025-10-13 12:48   ` Onur Özkan
2025-10-13 12:41 ` [PATCH v3 2/2] rust: drop `error::to_result` and utilize `ToResult` Onur Özkan
2025-10-13 17:04   ` Miguel Ojeda
2025-10-13 19:03     ` Onur Özkan
2025-10-13 18:29   ` Alice Ryhl [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=aO1E_-KdnvpG5vXl@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@gmail.com \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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.