[PATCH v4 4/7] rust: str: make `from_bytes_with_nul_unchecked_mut` const

Alexandre Courbot posted 7 patches 3 months, 3 weeks ago
[PATCH v4 4/7] rust: str: make `from_bytes_with_nul_unchecked_mut` const
Posted by Alexandre Courbot 3 months, 3 weeks ago
This method was probably kept non-const due to the absence of the
`const_mut_refs` feature, but it has been enabled since the introduction
of this code (and stabilized with Rust 1.83). Thus, make it const to
match its non-const counterpart.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/str.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index a927db8e079c3597860880947a03959e1d6d712e..2640a050847e64bfd18c127a5a83b93f01f036bc 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -288,7 +288,7 @@ pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, CStrConvertError
     /// `bytes` *must* end with a `NUL` byte, and should only have a single
     /// `NUL` byte (or the string will be truncated).
     #[inline]
-    pub unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr {
+    pub const unsafe fn from_bytes_with_nul_unchecked_mut(bytes: &mut [u8]) -> &mut CStr {
         // SAFETY: Properties of `bytes` guaranteed by the safety precondition.
         unsafe { &mut *(bytes as *mut [u8] as *mut CStr) }
     }

-- 
2.49.0
Re: [PATCH v4 4/7] rust: str: make `from_bytes_with_nul_unchecked_mut` const
Posted by Benno Lossin 3 months, 3 weeks ago
On Mon Jun 16, 2025 at 5:34 AM CEST, Alexandre Courbot wrote:
> This method was probably kept non-const due to the absence of the
> `const_mut_refs` feature, but it has been enabled since the introduction
> of this code (and stabilized with Rust 1.83). Thus, make it const to
> match its non-const counterpart.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Ah on second thought, this and the next two patches are a bit redundant,
since Tamir has a series [1] removing our `CStr` and using the one from
`core`.

If you need this *now* and can't wait for Tamir's series to land, then
we can do this and the other two changes, but othrwise I'd just use
`CStr` from `core`.

It does seem like you need `&mut CStr`, which the one in `core` doesn't
seem to provide... But our `CStr` also doesn't have `IndexMut`, so...
how are you using it? Giving it to a C API?

In that case I don't know what we should do about [1]... @Miguel?

[1]: https://lore.kernel.org/all/20250530-cstr-core-v11-0-cd9c0cbcb902@gmail.com

---
Cheers,
Benno
Re: [PATCH v4 4/7] rust: str: make `from_bytes_with_nul_unchecked_mut` const
Posted by Alexandre Courbot 3 months, 3 weeks ago
On Thu Jun 19, 2025 at 6:16 AM JST, Benno Lossin wrote:
> On Mon Jun 16, 2025 at 5:34 AM CEST, Alexandre Courbot wrote:
>> This method was probably kept non-const due to the absence of the
>> `const_mut_refs` feature, but it has been enabled since the introduction
>> of this code (and stabilized with Rust 1.83). Thus, make it const to
>> match its non-const counterpart.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>
> Ah on second thought, this and the next two patches are a bit redundant,
> since Tamir has a series [1] removing our `CStr` and using the one from
> `core`.
>
> If you need this *now* and can't wait for Tamir's series to land, then
> we can do this and the other two changes, but othrwise I'd just use
> `CStr` from `core`.
>
> It does seem like you need `&mut CStr`, which the one in `core` doesn't
> seem to provide... But our `CStr` also doesn't have `IndexMut`, so...
> how are you using it? Giving it to a C API?
>
> In that case I don't know what we should do about [1]... @Miguel?
>
> [1]: https://lore.kernel.org/all/20250530-cstr-core-v11-0-cd9c0cbcb902@gmail.com

Let's drop this part (patches 4..=7) for now to avoid interfering with
Tamir's work - the CString implementation was more of a drive-by, the
container types are more important to support. I will revisit after
Tamir's series lands, if needed.

As Danilo took patches 1 and 3, this just leaves patch 2 to be picked
up if it looks ok.
Re: [PATCH v4 4/7] rust: str: make `from_bytes_with_nul_unchecked_mut` const
Posted by Benno Lossin 3 months, 3 weeks ago
On Mon Jun 16, 2025 at 5:34 AM CEST, Alexandre Courbot wrote:
> This method was probably kept non-const due to the absence of the
> `const_mut_refs` feature, but it has been enabled since the introduction
> of this code (and stabilized with Rust 1.83). Thus, make it const to
> match its non-const counterpart.
>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Reviewed-by: Benno Lossin <lossin@kernel.org>

---
Cheers,
Benno

> ---
>  rust/kernel/str.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)