[PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`.

Andreas Hindborg posted 18 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Andreas Hindborg 1 month, 2 weeks ago
Improve `Formatter` so that it can write to an array or slice buffer.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/str.rs | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 082790b7a621..76632da357a6 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -7,7 +7,10 @@
     fmt::{self, Write},
     prelude::*,
 };
-use core::ops::{self, Deref, DerefMut, Index};
+use core::{
+    marker::PhantomData,
+    ops::{self, Deref, DerefMut, Index},
+};
 
 /// Byte string without UTF-8 validity guarantee.
 #[repr(transparent)]
@@ -825,9 +828,9 @@ fn write_str(&mut self, s: &str) -> fmt::Result {
 /// Allows formatting of [`fmt::Arguments`] into a raw buffer.
 ///
 /// Fails if callers attempt to write more than will fit in the buffer.
-pub(crate) struct Formatter(RawFormatter);
+pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>);
 
-impl Formatter {
+impl Formatter<'_> {
     /// Creates a new instance of [`Formatter`] with the given buffer.
     ///
     /// # Safety
@@ -836,11 +839,19 @@ impl Formatter {
     /// for the lifetime of the returned [`Formatter`].
     pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
         // SAFETY: The safety requirements of this function satisfy those of the callee.
-        Self(unsafe { RawFormatter::from_buffer(buf, len) })
+        Self(unsafe { RawFormatter::from_buffer(buf, len) }, PhantomData)
+    }
+
+    /// Create a new [`Self`] instance.
+    #[expect(dead_code)]
+    pub(crate) fn new(buffer: &mut [u8]) -> Self {
+        // SAFETY: `buffer` is valid for writes for the entire length for
+        // the lifetime of `Self`.
+        unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len()) }
     }
 }
 
-impl Deref for Formatter {
+impl Deref for Formatter<'_> {
     type Target = RawFormatter;
 
     fn deref(&self) -> &Self::Target {
@@ -848,7 +859,7 @@ fn deref(&self) -> &Self::Target {
     }
 }
 
-impl fmt::Write for Formatter {
+impl fmt::Write for Formatter<'_> {
     fn write_str(&mut self, s: &str) -> fmt::Result {
         self.0.write_str(s)?;
 

-- 
2.47.2
Re: [PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Daniel Almeida 1 month, 1 week ago
Hi Andreas,

> On 15 Aug 2025, at 04:30, Andreas Hindborg <a.hindborg@kernel.org> wrote:
> 
> Improve `Formatter` so that it can write to an array or slice buffer.
> 
> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/str.rs | 23 +++++++++++++++++------
> 1 file changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 082790b7a621..76632da357a6 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -7,7 +7,10 @@
>     fmt::{self, Write},
>     prelude::*,
> };
> -use core::ops::{self, Deref, DerefMut, Index};
> +use core::{
> +    marker::PhantomData,
> +    ops::{self, Deref, DerefMut, Index},
> +};
> 
> /// Byte string without UTF-8 validity guarantee.
> #[repr(transparent)]
> @@ -825,9 +828,9 @@ fn write_str(&mut self, s: &str) -> fmt::Result {
> /// Allows formatting of [`fmt::Arguments`] into a raw buffer.
> ///
> /// Fails if callers attempt to write more than will fit in the buffer.
> -pub(crate) struct Formatter(RawFormatter);
> +pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>);
> 
> -impl Formatter {
> +impl Formatter<'_> {
>     /// Creates a new instance of [`Formatter`] with the given buffer.
>     ///
>     /// # Safety
> @@ -836,11 +839,19 @@ impl Formatter {
>     /// for the lifetime of the returned [`Formatter`].
>     pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
>         // SAFETY: The safety requirements of this function satisfy those of the callee.
> -        Self(unsafe { RawFormatter::from_buffer(buf, len) })
> +        Self(unsafe { RawFormatter::from_buffer(buf, len) }, PhantomData)
> +    }
> +
> +    /// Create a new [`Self`] instance.
> +    #[expect(dead_code)]
> +    pub(crate) fn new(buffer: &mut [u8]) -> Self {
> +        // SAFETY: `buffer` is valid for writes for the entire length for
> +        // the lifetime of `Self`.
> +        unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len()) }
>     }
> }
> 
> -impl Deref for Formatter {
> +impl Deref for Formatter<'_> {
>     type Target = RawFormatter;
> 
>     fn deref(&self) -> &Self::Target {
> @@ -848,7 +859,7 @@ fn deref(&self) -> &Self::Target {
>     }
> }
> 
> -impl fmt::Write for Formatter {
> +impl fmt::Write for Formatter<'_> {
>     fn write_str(&mut self, s: &str) -> fmt::Result {
>         self.0.write_str(s)?;
> 
> 
> -- 
> 2.47.2
> 
> 
> 

I gave my r-b on v3 for this patch IIRC. What happened?

— Daniel 
Re: [PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Andreas Hindborg 1 month ago
"Daniel Almeida" <daniel.almeida@collabora.com> writes:

> Hi Andreas,
>
>> On 15 Aug 2025, at 04:30, Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> Improve `Formatter` so that it can write to an array or slice buffer.
>>
>> Reviewed-by: Alice Ryhl <aliceryhl@google.com>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

<cut>

>
> I gave my r-b on v3 for this patch IIRC. What happened?

I probably messed up. I'm having a really bad time with b4 collecting
the tags.

I'll be sure to add your tag in next spin. Thanks!


Best regards,
Andreas Hindborg
Re: [PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Konstantin Ryabitsev 1 month ago
On Fri, Aug 29, 2025 at 01:18:09PM +0200, Andreas Hindborg wrote:
> > I gave my r-b on v3 for this patch IIRC. What happened?
> 
> I probably messed up. I'm having a really bad time with b4 collecting
> the tags.

Can I offer any help?

-K
Re: [PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Andreas Hindborg 1 month ago
"Konstantin Ryabitsev" <konstantin@linuxfoundation.org> writes:

> On Fri, Aug 29, 2025 at 01:18:09PM +0200, Andreas Hindborg wrote:
>> > I gave my r-b on v3 for this patch IIRC. What happened?
>>
>> I probably messed up. I'm having a really bad time with b4 collecting
>> the tags.
>
> Can I offer any help?

That would be great. I'll be sure to write up a proper bug report next time I have issues.


Best regards,
Andreas Hindborg