linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Viresh Kumar <viresh.kumar@linaro.org>,
	Erik Schilling <erik.schilling@linaro.org>,
	linux-gpio@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [libgpiod][PATCH v2 5/5] bindings: rust: provide LineRequest::chip_name()
Date: Sat, 22 Jul 2023 10:07:34 +0800	[thread overview]
Message-ID: <ZLs55rrghb/X3rd2@sol> (raw)
In-Reply-To: <CAMRc=MfyHqp5gWBmAtw6MhGS3p1oMt3yKTLQGOK09kccuLq+dw@mail.gmail.com>

On Fri, Jul 21, 2023 at 08:35:07PM +0200, Bartosz Golaszewski wrote:
> On Fri, Jul 21, 2023 at 5:15 AM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > On Thu, Jul 20, 2023 at 04:47:47PM +0200, Bartosz Golaszewski wrote:
> > > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > >
> > > Provide a wrapper around gpiod_line_request_get_chip_name() for Rust
> > > bindings and add a test-case.
> > >
> > > Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> > > ---
> > >  bindings/rust/libgpiod/src/line_request.rs   | 16 ++++++++++++++++
> > >  bindings/rust/libgpiod/tests/line_request.rs | 14 ++++++++++++++
> > >  2 files changed, 30 insertions(+)
> > >
> > > diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs
> > > index 1140aa9..737c06f 100644
> > > --- a/bindings/rust/libgpiod/src/line_request.rs
> > > +++ b/bindings/rust/libgpiod/src/line_request.rs
> > > @@ -2,6 +2,7 @@
> > >  // SPDX-FileCopyrightText: 2022 Linaro Ltd.
> > >  // SPDX-FileCopyrightText: 2022 Viresh Kumar <viresh.kumar@linaro.org>
> > >
> > > +use std::ffi::CStr;
> > >  use std::os::unix::prelude::AsRawFd;
> > >  use std::time::Duration;
> > >
> > > @@ -25,6 +26,21 @@ impl Request {
> > >          Ok(Self { request })
> > >      }
> > >
> > > +    /// Get the name of the chip this request was made on.
> > > +    pub fn chip_name(&self) -> Result<&str> {
> > > +        // SAFETY: The `gpiod_line_request` is guaranteed to be live as long
> > > +        // as `&self`
> > > +        let name = unsafe { gpiod::gpiod_line_request_get_chip_name(self.request) };
> > > +
> > > +        // SAFETY: The string is guaranteed to be valid, non-null and immutable
> > > +        // by the C API for the lifetime of the `gpiod_line_request`. The
> > > +        // `gpiod_line_request` is living as long as `&self`. The string is
> > > +        // returned read-only with a lifetime of `&self`.
> > > +        unsafe { CStr::from_ptr(name) }
> > > +            .to_str()
> > > +            .map_err(Error::StringNotUtf8)
> > > +    }
> > > +
> >
> > I would drop the name temp var myself, but that is just a nit.
> >
> 
> I would too but rust was making it very difficult with borrow semantics. :)
> 

Really?  What error are you getting?

This works for me:

    /// Get the name of the chip this request was made on.
    pub fn chip_name(&self) -> Result<&str> {
        // SAFETY: The string is guaranteed to be valid, non-null and immutable
        // by the C API for the lifetime of the `gpiod_line_request`. The
        // `gpiod_line_request` is living as long as `&self`. The string is
        // returned read-only with a lifetime of `&self`.
        unsafe { CStr::from_ptr(gpiod::gpiod_line_request_get_chip_name(self.request)) }
            .to_str()
            .map_err(Error::StringNotUtf8)
    }

And the last sentence of the SAFETY comment looks redundant to me -
it is just repeating what the signature already says.
(otherwise the return would be something like Result<&'a mut String>)

Cheers,
Kent.

  reply	other threads:[~2023-07-22  2:07 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 14:47 [libgpiod][PATCH v2 0/5] core: provide information about the parent chip in line requests Bartosz Golaszewski
2023-07-20 14:47 ` [libgpiod][PATCH v2 1/5] core: provide gpiod_line_request_get_chip_name() Bartosz Golaszewski
2023-07-20 14:47 ` [libgpiod][PATCH v2 2/5] tests: add a test-case for gpiod_line_request_get_chip_name() Bartosz Golaszewski
2023-07-20 14:47 ` [libgpiod][PATCH v2 3/5] bindings: cxx: provide line_request::chip_name() Bartosz Golaszewski
2023-07-20 14:47 ` [libgpiod][PATCH v2 4/5] bindings: python: provide the chip_name property in line_request Bartosz Golaszewski
2023-07-20 14:47 ` [libgpiod][PATCH v2 5/5] bindings: rust: provide LineRequest::chip_name() Bartosz Golaszewski
2023-07-20 14:53   ` Erik Schilling
2023-07-21  3:15   ` Kent Gibson
2023-07-21 18:35     ` Bartosz Golaszewski
2023-07-22  2:07       ` Kent Gibson [this message]
2023-07-22 12:30         ` Bartosz Golaszewski
2023-07-21  3:27   ` Kent Gibson
2023-07-21  6:55   ` 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=ZLs55rrghb/X3rd2@sol \
    --to=warthog618@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=brgl@bgdev.pl \
    --cc=erik.schilling@linaro.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.org \
    --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).