Re: [PATCH V6 04/15] rust: device: Add few helpers

From: Alice Ryhl
Date: Wed Jan 08 2025 - 06:58:45 EST


On Wed, Jan 8, 2025 at 12:52 PM Greg Kroah-Hartman
<gregkh@xxxxxxxxxxxxxxxxxxx> wrote:
> > + /// Checks if property is present or not.
> > + pub fn property_present(&self, name: &CString) -> bool {
> > + // SAFETY: By the invariant of `CString`, `name` is null-terminated.
> > + unsafe { bindings::device_property_present(self.as_raw() as *const _, name.as_ptr() as *const _) }
>
> I hate to ask, but how was this compiling if the const wasn't there
> before? There's no type-checking happening here? If not, how are we
> ever going to notice when function parameters change? If there is type
> checking, how did this ever build without the const?
>
> confused,

Rust auto-converts `*mut` pointers to `*const` when necessary.

Note that this should really be `self.as_raw().cast_const()` if you're
just casting mut to const without changing the pointee type.

Alice