Message ID | 20240805152004.5039-27-dakr@kernel.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Generic `Allocator` support for Rust | expand |
On 8/8/24 9:22 AM, Benno Lossin wrote: > On 05.08.24 17:19, Danilo Krummrich wrote: >> 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`. >> >> Signed-off-by: Danilo Krummrich <dakr@kernel.org> >> --- >> rust/kernel/str.rs | 23 ++++++++++++++++++++++- >> 1 file changed, 22 insertions(+), 1 deletion(-) >> >> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs >> index 0b6ffbade521..8f234224cbf5 100644 >> --- a/rust/kernel/str.rs >> +++ b/rust/kernel/str.rs >> @@ -523,7 +523,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() >> + } >> + } > > Don't actually think we need this newtype. Right, but I thought it's a bit more extensible and cleaner than... > >> + >> + macro_rules! format { >> + ($($f:tt)*) => ({ >> + &*String::from_fmt(kernel::fmt!($($f)*)) > > We could just do this: > > CString::try_from_fmt(kernel::fmt!($(f)*)).unwrap().to_str().unwrap() ...doing it inline. I'm fine either way, but unless you really want me to change it, I'd rather keep it as it is. > > --- > Cheers, > Benno > >> + }) >> + } >> >> const ALL_ASCII_CHARS: &'static str = >> "\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x0f\ >> -- >> 2.45.2 >> >
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 0b6ffbade521..8f234224cbf5 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -523,7 +523,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\
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`. Signed-off-by: Danilo Krummrich <dakr@kernel.org> --- rust/kernel/str.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)