Skip to content

Commit

Permalink
rust: str: test: replace alloc::format
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Signed-off-by: Danilo Krummrich <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
Danilo Krummrich authored and ojeda committed Oct 15, 2024
1 parent 4b28692 commit 855381a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion rust/kernel/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down

0 comments on commit 855381a

Please sign in to comment.