Skip to content

Commit 9ead9fd

Browse files
Andreas Hindborgkawasaki
authored andcommitted
rust: block: use NullBorrowFormatter
Use the new `NullBorrowFormatter` to write the name of a `GenDisk` to the name buffer. This new formatter automatically adds a trailing null marker after the written characters, so we don't need to append that at the call site any longer. Signed-off-by: Andreas Hindborg <[email protected]>
1 parent 9e0220e commit 9ead9fd

3 files changed

Lines changed: 5 additions & 11 deletions

File tree

rust/kernel/block/mq/gen_disk.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
88
use crate::{
99
bindings,
10-
block::mq::{raw_writer::RawWriter, Operations, TagSet},
10+
block::mq::{Operations, TagSet},
1111
error::{self, from_err_ptr, Result},
1212
static_lock_class,
13+
str::NullBorrowFormatter,
1314
sync::Arc,
1415
};
1516
use core::fmt::{self, Write};
@@ -143,14 +144,13 @@ impl GenDiskBuilder {
143144
// SAFETY: `gendisk` is a valid pointer as we initialized it above
144145
unsafe { (*gendisk).fops = &TABLE };
145146

146-
let mut raw_writer = RawWriter::from_array(
147+
let mut writer = NullBorrowFormatter::from_array(
147148
// SAFETY: `gendisk` points to a valid and initialized instance. We
148149
// have exclusive access, since the disk is not added to the VFS
149150
// yet.
150151
unsafe { &mut (*gendisk).disk_name },
151152
)?;
152-
raw_writer.write_fmt(name)?;
153-
raw_writer.write_char('\0')?;
153+
writer.write_fmt(name)?;
154154

155155
// SAFETY: `gendisk` points to a valid and initialized instance of
156156
// `struct gendisk`. `set_capacity` takes a lock to synchronize this

rust/kernel/block/mq/raw_writer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl<'a> RawWriter<'a> {
2424
Ok(Self { buffer, pos: 0 })
2525
}
2626

27+
#[expect(dead_code)]
2728
pub(crate) fn from_array<const N: usize>(
2829
a: &'a mut [crate::ffi::c_char; N],
2930
) -> Result<RawWriter<'a>> {

rust/kernel/str.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,6 @@ impl<'a> NullBorrowFormatter<'a> {
881881
Ok(Self { buffer, pos: 0 })
882882
}
883883

884-
#[expect(dead_code)]
885884
pub(crate) fn from_array<const N: usize>(
886885
a: &'a mut [crate::ffi::c_char; N],
887886
) -> Result<NullBorrowFormatter<'a>> {
@@ -891,12 +890,6 @@ impl<'a> NullBorrowFormatter<'a> {
891890
unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::<u8>(), N) },
892891
)
893892
}
894-
895-
/// Return the position of the write pointer in the underlying buffer.
896-
#[expect(dead_code)]
897-
pub(crate) fn pos(&self) -> usize {
898-
self.pos
899-
}
900893
}
901894

902895
impl Write for NullBorrowFormatter<'_> {

0 commit comments

Comments
 (0)