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

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

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/str.rs | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 488b0e97004e..41af456a46c8 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -6,6 +6,7 @@
 use crate::prelude::*;
 use core::{
     fmt::{self, Write},
+    marker::PhantomData,
     ops::{self, Deref, DerefMut, Index},
 };
 
@@ -794,9 +795,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
@@ -805,11 +806,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<'a>(buffer: &'a mut [u8]) -> Formatter<'a> {
+        // 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 {
@@ -817,7 +826,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 v3 02/16] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Daniel Almeida 2 months ago
Hi Andreas,

> On 11 Jul 2025, at 08:43, Andreas Hindborg <a.hindborg@kernel.org> wrote:
> 
> Improve `Formatter` so that it can write to an array or slice buffer.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/str.rs | 19 ++++++++++++++-----
> 1 file changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 488b0e97004e..41af456a46c8 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -6,6 +6,7 @@
> use crate::prelude::*;
> use core::{
>     fmt::{self, Write},
> +    marker::PhantomData,
>     ops::{self, Deref, DerefMut, Index},
> };
> 
> @@ -794,9 +795,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
> @@ -805,11 +806,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<'a>(buffer: &'a mut [u8]) -> Formatter<'a> {

nit: the function above this one returns Self, and this one returns Formatter.
Perhaps this one should also return Self for consistency?
 
> +        // 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 {
> @@ -817,7 +826,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
> 
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Re: [PATCH v3 02/16] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Andreas Hindborg 2 months ago
"Daniel Almeida" <daniel.almeida@collabora.com> writes:

> Hi Andreas,
>
>> On 11 Jul 2025, at 08:43, Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>
>> Improve `Formatter` so that it can write to an array or slice buffer.
>>
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>> rust/kernel/str.rs | 19 ++++++++++++++-----
>> 1 file changed, 14 insertions(+), 5 deletions(-)
>>
>> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
>> index 488b0e97004e..41af456a46c8 100644
>> --- a/rust/kernel/str.rs
>> +++ b/rust/kernel/str.rs
>> @@ -6,6 +6,7 @@
>> use crate::prelude::*;
>> use core::{
>>     fmt::{self, Write},
>> +    marker::PhantomData,
>>     ops::{self, Deref, DerefMut, Index},
>> };
>>
>> @@ -794,9 +795,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
>> @@ -805,11 +806,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<'a>(buffer: &'a mut [u8]) -> Formatter<'a> {
>
> nit: the function above this one returns Self, and this one returns Formatter.
> Perhaps this one should also return Self for consistency?

Thanks. Not sure about the explicit lifetime either, I'll
reformat:

@@ -844,7 +844,7 @@ pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self {
 
     /// Create a new [`Self`] instance.
     #[expect(dead_code)]
-    pub(crate) fn new<'a>(buffer: &'a mut [u8]) -> Formatter<'a> {
+    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()) }


Best regards,
Andreas Hindborg
Re: [PATCH v3 02/16] rust: str: allow `str::Formatter` to format into `&mut [u8]`.
Posted by Alice Ryhl 2 months, 3 weeks ago
On Fri, Jul 11, 2025 at 01:43:03PM +0200, Andreas Hindborg wrote:
> Improve `Formatter` so that it can write to an array or slice buffer.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

> +    #[expect(dead_code)]

This could just be a public type to avoid this.