| From stable+bounces-121500-greg=kroah.com@vger.kernel.org Fri Mar 7 23:52:58 2025 |
| From: Miguel Ojeda <ojeda@kernel.org> |
| Date: Fri, 7 Mar 2025 23:49:52 +0100 |
| Subject: rust: str: test: replace `alloc::format` |
| To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Sasha Levin <sashal@kernel.org>, stable@vger.kernel.org |
| Cc: Danilo Krummrich <dakr@kernel.org>, Alice Ryhl <aliceryhl@google.com>, Alyssa Ross <hi@alyssa.is>, NoisyCoil <noisycoil@disroot.org>, patches@lists.linux.dev, Miguel Ojeda <ojeda@kernel.org> |
| Message-ID: <20250307225008.779961-46-ojeda@kernel.org> |
| |
| From: Danilo Krummrich <dakr@kernel.org> |
| |
| commit eb6f92cd3f755c179204ea1f933b07cf992892fd upstream. |
| |
| The current implementation of tests in str.rs use `format!` to format |
| strings for comparison, which, internally, creates a new `String`. |
| |
| In order to prepare for getting rid of Rust's alloc crate, we have to |
| cut this dependency. Instead, implement `format!` for `CString`. |
| |
| Note that for userspace tests, `Kmalloc`, which is backing `CString`'s |
| memory, is just a type alias to `Cmalloc`. |
| |
| Reviewed-by: Alice Ryhl <aliceryhl@google.com> |
| Reviewed-by: Benno Lossin <benno.lossin@proton.me> |
| Reviewed-by: Gary Guo <gary@garyguo.net> |
| Signed-off-by: Danilo Krummrich <dakr@kernel.org> |
| Link: https://lore.kernel.org/r/20241004154149.93856-27-dakr@kernel.org |
| Signed-off-by: Miguel Ojeda <ojeda@kernel.org> |
| Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> |
| --- |
| rust/kernel/str.rs | 23 ++++++++++++++++++++++- |
| 1 file changed, 22 insertions(+), 1 deletion(-) |
| |
| --- a/rust/kernel/str.rs |
| +++ b/rust/kernel/str.rs |
| @@ -524,7 +524,28 @@ macro_rules! c_str { |
| #[cfg(test)] |
| mod tests { |
| use super::*; |
| - use alloc::format; |
| + |
| + struct String(CString); |
| + |
| + impl String { |
| + fn from_fmt(args: fmt::Arguments<'_>) -> Self { |
| + String(CString::try_from_fmt(args).unwrap()) |
| + } |
| + } |
| + |
| + impl Deref for String { |
| + type Target = str; |
| + |
| + fn deref(&self) -> &str { |
| + self.0.to_str().unwrap() |
| + } |
| + } |
| + |
| + macro_rules! format { |
| + ($($f:tt)*) => ({ |
| + &*String::from_fmt(kernel::fmt!($($f)*)) |
| + }) |
| + } |
| |
| const ALL_ASCII_CHARS: &'static str = |
| "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\ |