[RESEND PATCH v18 13/16] rust: regulator: use `CStr::as_char_ptr`

Tamir Duberstein posted 16 patches 1 month, 3 weeks ago
[RESEND PATCH v18 13/16] rust: regulator: use `CStr::as_char_ptr`
Posted by Tamir Duberstein 1 month, 3 weeks ago
From: Tamir Duberstein <tamird@gmail.com>

Replace the use of `as_ptr` which works through `<CStr as
Deref<Target=&[u8]>::deref()` in preparation for replacing
`kernel::str::CStr` with `core::ffi::CStr` as the latter does not
implement `Deref<Target=&[u8]>`.

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 rust/kernel/regulator.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs
index b55a201e5029..65a4eb096cae 100644
--- a/rust/kernel/regulator.rs
+++ b/rust/kernel/regulator.rs
@@ -84,7 +84,7 @@ pub struct Error<State: RegulatorState> {
 pub fn devm_enable(dev: &Device<Bound>, name: &CStr) -> Result {
     // SAFETY: `dev` is a valid and bound device, while `name` is a valid C
     // string.
-    to_result(unsafe { bindings::devm_regulator_get_enable(dev.as_raw(), name.as_ptr()) })
+    to_result(unsafe { bindings::devm_regulator_get_enable(dev.as_raw(), name.as_char_ptr()) })
 }
 
 /// Same as [`devm_enable`], but calls `devm_regulator_get_enable_optional`
@@ -102,7 +102,9 @@ pub fn devm_enable(dev: &Device<Bound>, name: &CStr) -> Result {
 pub fn devm_enable_optional(dev: &Device<Bound>, name: &CStr) -> Result {
     // SAFETY: `dev` is a valid and bound device, while `name` is a valid C
     // string.
-    to_result(unsafe { bindings::devm_regulator_get_enable_optional(dev.as_raw(), name.as_ptr()) })
+    to_result(unsafe {
+        bindings::devm_regulator_get_enable_optional(dev.as_raw(), name.as_char_ptr())
+    })
 }
 
 /// A `struct regulator` abstraction.
@@ -268,7 +270,8 @@ pub fn get_voltage(&self) -> Result<Voltage> {
     fn get_internal(dev: &Device, name: &CStr) -> Result<Regulator<T>> {
         // SAFETY: It is safe to call `regulator_get()`, on a device pointer
         // received from the C code.
-        let inner = from_err_ptr(unsafe { bindings::regulator_get(dev.as_raw(), name.as_ptr()) })?;
+        let inner =
+            from_err_ptr(unsafe { bindings::regulator_get(dev.as_raw(), name.as_char_ptr()) })?;
 
         // SAFETY: We can safely trust `inner` to be a pointer to a valid
         // regulator if `ERR_PTR` was not returned.

-- 
2.51.1
Re: [RESEND PATCH v18 13/16] rust: regulator: use `CStr::as_char_ptr`
Posted by Daniel Almeida 1 month, 3 weeks ago

> On 18 Oct 2025, at 16:16, Tamir Duberstein <tamird@kernel.org> wrote:
> 
> From: Tamir Duberstein <tamird@gmail.com>
> 
> Replace the use of `as_ptr` which works through `<CStr as
> Deref<Target=&[u8]>::deref()` in preparation for replacing
> `kernel::str::CStr` with `core::ffi::CStr` as the latter does not
> implement `Deref<Target=&[u8]>`.
> 
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> rust/kernel/regulator.rs | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/rust/kernel/regulator.rs b/rust/kernel/regulator.rs
> index b55a201e5029..65a4eb096cae 100644
> --- a/rust/kernel/regulator.rs
> +++ b/rust/kernel/regulator.rs
> @@ -84,7 +84,7 @@ pub struct Error<State: RegulatorState> {
> pub fn devm_enable(dev: &Device<Bound>, name: &CStr) -> Result {
>     // SAFETY: `dev` is a valid and bound device, while `name` is a valid C
>     // string.
> -    to_result(unsafe { bindings::devm_regulator_get_enable(dev.as_raw(), name.as_ptr()) })
> +    to_result(unsafe { bindings::devm_regulator_get_enable(dev.as_raw(), name.as_char_ptr()) })
> }
> 
> /// Same as [`devm_enable`], but calls `devm_regulator_get_enable_optional`
> @@ -102,7 +102,9 @@ pub fn devm_enable(dev: &Device<Bound>, name: &CStr) -> Result {
> pub fn devm_enable_optional(dev: &Device<Bound>, name: &CStr) -> Result {
>     // SAFETY: `dev` is a valid and bound device, while `name` is a valid C
>     // string.
> -    to_result(unsafe { bindings::devm_regulator_get_enable_optional(dev.as_raw(), name.as_ptr()) })
> +    to_result(unsafe {
> +        bindings::devm_regulator_get_enable_optional(dev.as_raw(), name.as_char_ptr())
> +    })
> }
> 
> /// A `struct regulator` abstraction.
> @@ -268,7 +270,8 @@ pub fn get_voltage(&self) -> Result<Voltage> {
>     fn get_internal(dev: &Device, name: &CStr) -> Result<Regulator<T>> {
>         // SAFETY: It is safe to call `regulator_get()`, on a device pointer
>         // received from the C code.
> -        let inner = from_err_ptr(unsafe { bindings::regulator_get(dev.as_raw(), name.as_ptr()) })?;
> +        let inner =
> +            from_err_ptr(unsafe { bindings::regulator_get(dev.as_raw(), name.as_char_ptr()) })?;
> 
>         // SAFETY: We can safely trust `inner` to be a pointer to a valid
>         // regulator if `ERR_PTR` was not returned.
> 
> -- 
> 2.51.1
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Re: [RESEND PATCH v18 13/16] rust: regulator: use `CStr::as_char_ptr`
Posted by Miguel Ojeda 1 month, 3 weeks ago
On Sat, Oct 18, 2025 at 9:17 PM Tamir Duberstein <tamird@kernel.org> wrote:
>
> From: Tamir Duberstein <tamird@gmail.com>
>
> Replace the use of `as_ptr` which works through `<CStr as
> Deref<Target=&[u8]>::deref()` in preparation for replacing
> `kernel::str::CStr` with `core::ffi::CStr` as the latter does not
> implement `Deref<Target=&[u8]>`.
>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>

Liam, Mark: I will apply this since it would be nice to try to get the
flag day patch in this series finally done -- please shout if you have
a problem with this.

An Acked-by would be very appreciated, thanks!

Cheers,
Miguel
Re: [RESEND PATCH v18 13/16] rust: regulator: use `CStr::as_char_ptr`
Posted by Mark Brown 1 month, 3 weeks ago
On Sun, Oct 19, 2025 at 11:25:16PM +0200, Miguel Ojeda wrote:
> On Sat, Oct 18, 2025 at 9:17 PM Tamir Duberstein <tamird@kernel.org> wrote:
> > From: Tamir Duberstein <tamird@gmail.com>

> > Replace the use of `as_ptr` which works through `<CStr as
> > Deref<Target=&[u8]>::deref()` in preparation for replacing
> > `kernel::str::CStr` with `core::ffi::CStr` as the latter does not
> > implement `Deref<Target=&[u8]>`.

> Liam, Mark: I will apply this since it would be nice to try to get the
> flag day patch in this series finally done -- please shout if you have
> a problem with this.

Acked-by: Mark Brown <broonie@kernel.org>