* [PATCH] rust: device: Replace CString with CStr in property_present()
@ 2025-01-16 5:26 Viresh Kumar
2025-01-16 8:25 ` Alice Ryhl
2025-01-16 8:34 ` [PATCH] rust: device: Use as_char_ptr() to avoid explicit cast Viresh Kumar
0 siblings, 2 replies; 7+ messages in thread
From: Viresh Kumar @ 2025-01-16 5:26 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: Viresh Kumar, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
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@linaro.org>
---
Adapted with Rob's version a little.
rust/kernel/device.rs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index e8c4cda5aacc..c775266ae164 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -6,7 +6,7 @@
use crate::{
bindings,
- str::CString,
+ str::CStr,
types::{ARef, Opaque},
};
use core::{fmt, ptr};
@@ -183,8 +183,8 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
}
/// 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.
+ 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 _) }
}
}
--
2.31.1.272.g89b43f80a514
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: device: Replace CString with CStr in property_present()
2025-01-16 5:26 [PATCH] rust: device: Replace CString with CStr in property_present() Viresh Kumar
@ 2025-01-16 8:25 ` Alice Ryhl
2025-01-16 8:35 ` Viresh Kumar
2025-01-18 0:44 ` Gary Guo
2025-01-16 8:34 ` [PATCH] rust: device: Use as_char_ptr() to avoid explicit cast Viresh Kumar
1 sibling, 2 replies; 7+ messages in thread
From: Alice Ryhl @ 2025-01-16 8:25 UTC (permalink / raw)
To: Viresh Kumar
Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
On Thu, Jan 16, 2025 at 6:26 AM Viresh Kumar <viresh.kumar@linaro.org> 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@linaro.org>
One nit below, but either way:
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> - 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.
Alice
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH] rust: device: Use as_char_ptr() to avoid explicit cast
2025-01-16 5:26 [PATCH] rust: device: Replace CString with CStr in property_present() Viresh Kumar
2025-01-16 8:25 ` Alice Ryhl
@ 2025-01-16 8:34 ` Viresh Kumar
2025-01-16 9:01 ` Alice Ryhl
1 sibling, 1 reply; 7+ messages in thread
From: Viresh Kumar @ 2025-01-16 8:34 UTC (permalink / raw)
To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross
Cc: Viresh Kumar, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
Use as_char_ptr() to avoid explicit cast.
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
Rebased over:
https://lore.kernel.org/all/e97dcbe0418cc1053fb4bcfac65cc02a0afcdf78.1737005078.git.viresh.kumar@linaro.org/
rust/kernel/device.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index c775266ae164..db2d9658ba47 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -185,7 +185,7 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
/// Checks if property is present or not.
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 _) }
+ unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
}
}
--
2.31.1.272.g89b43f80a514
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: device: Replace CString with CStr in property_present()
2025-01-16 8:25 ` Alice Ryhl
@ 2025-01-16 8:35 ` Viresh Kumar
2025-01-18 0:44 ` Gary Guo
1 sibling, 0 replies; 7+ messages in thread
From: Viresh Kumar @ 2025-01-16 8:35 UTC (permalink / raw)
To: Alice Ryhl
Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
On 16-01-25, 09:25, Alice Ryhl wrote:
> On Thu, Jan 16, 2025 at 6:26 AM Viresh Kumar <viresh.kumar@linaro.org> 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@linaro.org>
>
> One nit below, but either way:
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
>
> > - 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.
Fixed as a separate commit. Thanks.
--
viresh
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: device: Use as_char_ptr() to avoid explicit cast
2025-01-16 8:34 ` [PATCH] rust: device: Use as_char_ptr() to avoid explicit cast Viresh Kumar
@ 2025-01-16 9:01 ` Alice Ryhl
0 siblings, 0 replies; 7+ messages in thread
From: Alice Ryhl @ 2025-01-16 9:01 UTC (permalink / raw)
To: Viresh Kumar
Cc: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
On Thu, Jan 16, 2025 at 9:34 AM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> Use as_char_ptr() to avoid explicit cast.
>
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: device: Replace CString with CStr in property_present()
2025-01-16 8:25 ` Alice Ryhl
2025-01-16 8:35 ` Viresh Kumar
@ 2025-01-18 0:44 ` Gary Guo
2025-01-20 5:05 ` Viresh Kumar
1 sibling, 1 reply; 7+ messages in thread
From: Gary Guo @ 2025-01-18 0:44 UTC (permalink / raw)
To: Alice Ryhl
Cc: Viresh Kumar, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
On Thu, 16 Jan 2025 09:25:17 +0100
Alice Ryhl <aliceryhl@google.com> wrote:
> On Thu, Jan 16, 2025 at 6:26 AM Viresh Kumar <viresh.kumar@linaro.org> 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@linaro.org>
>
> One nit below, but either way:
>
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
>
> > - 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()`?
c_char is unconditionally u8 now so they're now the same.
Best,
Gary
>
> Alice
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] rust: device: Replace CString with CStr in property_present()
2025-01-18 0:44 ` Gary Guo
@ 2025-01-20 5:05 ` Viresh Kumar
0 siblings, 0 replies; 7+ messages in thread
From: Viresh Kumar @ 2025-01-20 5:05 UTC (permalink / raw)
To: Gary Guo
Cc: Alice Ryhl, Greg Kroah-Hartman, Rafael J. Wysocki,
Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng,
Björn Roy Baron, Benno Lossin, Andreas Hindborg,
Trevor Gross, Rob Herring, Dirk Behme, rust-for-linux,
linux-kernel
On 18-01-25, 00:44, Gary Guo wrote:
> On Thu, 16 Jan 2025 09:25:17 +0100
> Alice Ryhl <aliceryhl@google.com> wrote:
>
> > On Thu, Jan 16, 2025 at 6:26 AM Viresh Kumar <viresh.kumar@linaro.org> 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@linaro.org>
> >
> > One nit below, but either way:
> >
> > Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> >
> > > - 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
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-01-20 5:05 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-16 5:26 [PATCH] rust: device: Replace CString with CStr in property_present() Viresh Kumar
2025-01-16 8:25 ` Alice Ryhl
2025-01-16 8:35 ` Viresh Kumar
2025-01-18 0:44 ` Gary Guo
2025-01-20 5:05 ` Viresh Kumar
2025-01-16 8:34 ` [PATCH] rust: device: Use as_char_ptr() to avoid explicit cast Viresh Kumar
2025-01-16 9:01 ` Alice Ryhl
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox