Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: str: test: replace `alloc::format`

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>

authored by

Danilo Krummrich and committed by
Miguel Ojeda
eb6f92cd dd09538f

+22 -1
+22 -1
rust/kernel/str.rs
··· 524 524 #[cfg(test)] 525 525 mod tests { 526 526 use super::*; 527 - use alloc::format; 527 + 528 + struct String(CString); 529 + 530 + impl String { 531 + fn from_fmt(args: fmt::Arguments<'_>) -> Self { 532 + String(CString::try_from_fmt(args).unwrap()) 533 + } 534 + } 535 + 536 + impl Deref for String { 537 + type Target = str; 538 + 539 + fn deref(&self) -> &str { 540 + self.0.to_str().unwrap() 541 + } 542 + } 543 + 544 + macro_rules! format { 545 + ($($f:tt)*) => ({ 546 + &*String::from_fmt(kernel::fmt!($($f)*)) 547 + }) 548 + } 528 549 529 550 const ALL_ASCII_CHARS: &'static str = 530 551 "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\