Re: [PATCH] rust: device: Replace CString with CStr in property_present()

From: Viresh Kumar
Date: Mon Jan 20 2025 - 00:05:51 EST


On 18-01-25, 00:44, Gary Guo wrote:
> On Thu, 16 Jan 2025 09:25:17 +0100
> Alice Ryhl <aliceryhl@xxxxxxxxxx> wrote:
>
> > On Thu, Jan 16, 2025 at 6:26 AM Viresh Kumar <viresh.kumar@xxxxxxxxxx> wrote:
> > >
> > > The property_present() method expects a &CString currently and will work
> > > only with heap allocated C strings.
> > >
> > > In order to make it work with compile-time string constants too, change
> > > the argument type to &CStr.
> > >
> > > Signed-off-by: Viresh Kumar <viresh.kumar@xxxxxxxxxx>
> >
> > One nit below, but either way:
> >
> > Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> >
> > > - pub fn property_present(&self, name: &CString) -> bool {
> > > - // SAFETY: By the invariant of `CString`, `name` is null-terminated.
> > > + pub fn property_present(&self, name: &CStr) -> bool {
> > > + // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
> > > unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_ptr() as *const _) }
> >
> > If you use `name.as_char_ptr()` then you don't need the cast.
>
> Isn't the cast not needed anyway with `as_ptr()`?

Yes that works too..

--
viresh