Rust-for-linux archive mirror
 help / color / mirror / Atom feed
From: "Behme Dirk (CM/ESO2)" <dirk.behme@de.bosch.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH] [RFC] rust: error: Convert 0 being an error to Result
Date: Fri, 12 Jan 2024 08:42:01 +0100	[thread overview]
Message-ID: <a5a8449a-dcc6-447f-b31e-cb7f9a9ae706@de.bosch.com> (raw)
In-Reply-To: <20240111141439.637461-1-aliceryhl@google.com>

On 11.01.2024 15:14, Alice Ryhl wrote:
> Dirk Behme <dirk.behme@de.bosch.com> writes:
>> The existing to_result() takes a (signed) integer from a kernel C function
>> and converts it to an error if it's negative. Additionally, there are
>> kernel C functions returning an unsigned integer where 0 is the error case.
>> For example gen_pool_alloc() and friends. Provide a mechanism to convert
>> this to Result too.
> 
> Hmm. One difference between `to_result` and the proposed
> `to_result_zero` is that `to_result` has logic to determine what kind of
> error to return based on the value of the integer, whereas
> `to_result_zero` is just hard-coded to always return a specific error
> code. Furthermore, I find that usually for these functions, the error
> code you want is not necessarily EINVAL. For example, when `kmalloc`
> returns a null pointer, the error is ENOMEM, or when `fget` returns a
> null pointer, the error is EBADF.
> 
> I think for this kind of use-case, we don't need a new function and can
> `NonNull::new(ptr).ok_or(ENOMEM)?` to return an ENOMEM if the pointer is
> null. Similarly, if it's an integer, we can use e.g. `NonZeroU32` in the
> same way.
> 
> For an example of this pattern, see `File::fget` in [1].
> 
> Alice
> 
> [1]: https://lore.kernel.org/all/20231206-alice-file-v2-1-af617c0d9d94@google.com/

So it seems min 3 people vote for NonNull usage :) Will do so. Let's 
drop this patch then.

Many thanks for all comments!

Best regards

Dirk


      reply	other threads:[~2024-01-12  7:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-11  6:44 [PATCH] [RFC] rust: error: Convert 0 being an error to Result Dirk Behme
2024-01-11  6:59 ` Greg KH
2024-01-11  7:29   ` Behme Dirk (CM/ESO2)
2024-01-11  7:54     ` Greg KH
2024-01-11  8:14       ` Behme Dirk (CM/ESO2)
2024-01-11  9:46         ` Trevor Gross
2024-01-11 12:17         ` Benno Lossin
2024-01-11 20:52           ` Trevor Gross
2024-01-11  6:59 ` Michael Büsch
2024-01-11 14:14 ` Alice Ryhl
2024-01-12  7:42   ` Behme Dirk (CM/ESO2) [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=a5a8449a-dcc6-447f-b31e-cb7f9a9ae706@de.bosch.com \
    --to=dirk.behme@de.bosch.com \
    --cc=aliceryhl@google.com \
    --cc=rust-for-linux@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).