linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: Viresh Kumar <viresh.kumar@linaro.org>,
	Bartosz Golaszewski <brgl@bgdev.pl>,
	"open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>
Subject: Re: libgpiod: rust bindings and bindgen issue with C enums
Date: Thu, 24 Nov 2022 22:58:01 +0800	[thread overview]
Message-ID: <Y3+GeREjXKkTQY6Y@sol> (raw)
In-Reply-To: <CANiq72=ufe1eGRVAcHcn9TZiMx2-HC-QQPZMbss5ErSdcpMyBA@mail.gmail.com>

On Thu, Nov 24, 2022 at 03:46:27PM +0100, Miguel Ojeda wrote:
> On Thu, Nov 24, 2022 at 2:32 PM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > I don't see this as a problem for generics.  Whether the enum is signed
> > or unsigned doesn't need to affect the Error variant, much less the whole
> > Error type.  The Error doesn't need to respresent the type of the source
> > of the error, it needs to represent the type required to convey
> > information to the user.
> > Just accepting that the InvalidEnumValue variant expects i32, and casting
> > from u32 if necessary, seems appropriate to me.  Unless there are some
> > extreme values you are concerned about - but then you always switch it
> > up to  i64 ;-).
> 
> Yeah, I am not sure what a generic `Error` buys us here.
> 
> If one really wants to preserve whether it is signed or not, that is
> only two possibilities, not an open set of them. Moreover, I imagine
> one wants to constraint them for users, rather than let users provide
> the type `E`.
> 
> Thus one could have a sum type with 2 variants like
> `InvalidEnumValue(..., Either<i32, u32>)` or something more explicit.
> 
> But that is assuming there is a need to preserve it. What is the
> variant meant to be used for by users? e.g. if it is just for
> reporting, it probably doesn't matter. Actually, does the user even
> need the number? Could `InvalidArguments` be enough?
> 

AIUI, it is just for reporting.  The value itself is helpful to
understand the root cause of the problem.  Not critical, but nice to
have.

> Looking at it a bit more, I am confused why the error is possible to
> begin with. There is `Value::new()` which appears to be public and can
> return `InvalidEnumValue`, but why should it be used by users? They
> should create the `enum` directly, no? And for the other `new()`s that
> also return `InvalidEnumValue`s, I see they are `pub(crate)`. That is
> what I would expect, but still, why is the error a possibility to
> begin with?
> 

The possibility for error can arise from running against a later
libgpiod that has additional values for the enum that these bindings are
obviously unaware of. e.g. the hte event clock recently added.  If you
had bindings built prior to that addition there is no Rust variant
in the event clock enum for that to map to.

Cheers,
Kent.

> For instance, somewhere else the library does `Direction::new(...)`
> with a value from the C side. The values from the C side must be
> correct, i.e. it is a bug otherwise, right? Thus one can trust them,
> or assert them, or if one wants to avoid panics for something that is
> a bug, one could return a `InternalLibraryError` with no extra
> information, because there is really not much users can do with the
> error details apart from knowing there is a bug in either the Rust or
> the C side.
> 
> I have taken a quick look at the C++ side for that same case, and from
> a quick look, C++ appears to throw if the mappings are wrong, so it
> sounds to me like you can similarly assert the validity in Rust and
> remove the `InvalidEnumValue` variant altogether.
> 
> Cheers,
> Miguel

  reply	other threads:[~2022-11-24 14:58 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-22 15:37 libgpiod: rust bindings and bindgen issue with C enums Bartosz Golaszewski
2022-11-22 16:55 ` Miguel Ojeda
2022-11-22 19:08   ` Bartosz Golaszewski
2022-11-23 18:37     ` Bartosz Golaszewski
2022-11-24 10:45       ` Viresh Kumar
2022-11-24 13:32         ` Kent Gibson
2022-11-24 14:27           ` Bartosz Golaszewski
2022-11-25  5:49             ` Viresh Kumar
2022-11-24 14:46           ` Miguel Ojeda
2022-11-24 14:58             ` Kent Gibson [this message]
2022-11-24 16:18               ` Miguel Ojeda
2022-11-25  1:48                 ` Kent Gibson
2022-11-25  5:48           ` Viresh Kumar

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=Y3+GeREjXKkTQY6Y@sol \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=viresh.kumar@linaro.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 NNTP newsgroup(s).