Skip to content

Commit 386f285

Browse files
tamirdojeda
authored andcommitted
rust: use kernel::{fmt,prelude::fmt!}
Reduce coupling to implementation details of the formatting machinery by avoiding direct use for `core`'s formatting traits and macros. Signed-off-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20250704-core-cstr-prepare-v1-3-a91524037783@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent bda947d commit 386f285

4 files changed

Lines changed: 8 additions & 8 deletions

File tree

rust/kernel/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
77
use crate::{
88
alloc::{layout::LayoutError, AllocError},
9+
fmt,
910
str::CStr,
1011
};
1112

12-
use core::fmt;
1313
use core::num::NonZeroI32;
1414
use core::num::TryFromIntError;
1515
use core::str::Utf8Error;
@@ -219,8 +219,8 @@ impl From<LayoutError> for Error {
219219
}
220220
}
221221

222-
impl From<core::fmt::Error> for Error {
223-
fn from(_: core::fmt::Error) -> Error {
222+
impl From<fmt::Error> for Error {
223+
fn from(_: fmt::Error) -> Error {
224224
code::EINVAL
225225
}
226226
}

rust/kernel/print.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
99
use crate::{
1010
ffi::{c_char, c_void},
11+
fmt,
1112
prelude::*,
1213
str::RawFormatter,
1314
};
14-
use core::fmt;
1515

1616
// Called from `vsprintf` with format specifier `%pA`.
1717
#[expect(clippy::missing_safety_doc)]
@@ -149,7 +149,7 @@ macro_rules! print_macro (
149149
// takes borrows on the arguments, but does not extend the scope of temporaries.
150150
// Therefore, a `match` expression is used to keep them around, since
151151
// the scrutinee is kept until the end of the `match`.
152-
match format_args!($($arg)+) {
152+
match $crate::prelude::fmt!($($arg)+) {
153153
// SAFETY: This hidden macro should only be called by the documented
154154
// printing macros which ensure the format string is one of the fixed
155155
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
@@ -168,7 +168,7 @@ macro_rules! print_macro (
168168
// The `CONT` case.
169169
($format_string:path, true, $($arg:tt)+) => (
170170
$crate::print::call_printk_cont(
171-
format_args!($($arg)+),
171+
$crate::prelude::fmt!($($arg)+),
172172
);
173173
);
174174
);

rust/kernel/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! String representations.
44
55
use crate::alloc::{flags::*, AllocError, KVec};
6-
use core::fmt::{self, Write};
6+
use crate::fmt::{self, Write};
77
use core::ops::{self, Deref, DerefMut, Index};
88

99
use crate::prelude::*;

samples/rust/rust_print_main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn arc_print() -> Result {
4040
// behaviour, contract or protocol on both `i32` and `&str` into a single `Arc` of
4141
// type `Arc<dyn Display>`.
4242

43-
use core::fmt::Display;
43+
use kernel::fmt::Display;
4444
fn arc_dyn_print(arc: &Arc<dyn Display>) {
4545
pr_info!("Arc<dyn Display> says {arc}");
4646
}

0 commit comments

Comments
 (0)