[v1,22/28] rust: str: add `fmt!` macro

Message ID 20221110164152.26136-23-ojeda@kernel.org
State New
Headers
Series Rust core additions |

Commit Message

Miguel Ojeda Nov. 10, 2022, 4:41 p.m. UTC
  From: Wedson Almeida Filho <wedsonaf@gmail.com>

Add the `fmt!` macro, which is a convenience alias for the Rust
`core::format_args!` macro.

For instance, it may be used to create a `CString`:

    CString::try_from_fmt(fmt!("{}{}", "abc", 42))?

Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/str.rs | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

Gary Guo Nov. 14, 2022, 2:58 p.m. UTC | #1
On Thu, 10 Nov 2022 17:41:34 +0100
Miguel Ojeda <ojeda@kernel.org> wrote:

> From: Wedson Almeida Filho <wedsonaf@gmail.com>
> 
> Add the `fmt!` macro, which is a convenience alias for the Rust
> `core::format_args!` macro.
> 
> For instance, it may be used to create a `CString`:
> 
>     CString::try_from_fmt(fmt!("{}{}", "abc", 42))?
> 
> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> [Reworded, adapted for upstream and applied latest changes]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/str.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index 877148b77e71..ffac633423db 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -592,3 +592,9 @@ impl Deref for CString {
>          unsafe { CStr::from_bytes_with_nul_unchecked(self.buf.as_slice()) }
>      }
>  }
> +
> +/// A convenience alias for [`core::format_args`].
> +#[macro_export]
> +macro_rules! fmt {
> +    ($($f:tt)*) => ( core::format_args!($($f)*) )
> +}
  

Patch

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 877148b77e71..ffac633423db 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -592,3 +592,9 @@  impl Deref for CString {
         unsafe { CStr::from_bytes_with_nul_unchecked(self.buf.as_slice()) }
     }
 }
+
+/// A convenience alias for [`core::format_args`].
+#[macro_export]
+macro_rules! fmt {
+    ($($f:tt)*) => ( core::format_args!($($f)*) )
+}