Skip to content

Commit 929b862

Browse files
Andreas Hindborgkawasaki
authored andcommitted
rust: block: use NullTerminatedFormatter
Use the new `NullTerminatedFormatter` 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 2ed4b26 commit 929b862

3 files changed

Lines changed: 7 additions & 6 deletions

File tree

rust/kernel/block/mq/gen_disk.rs

Lines changed: 6 additions & 5 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::NullTerminatedFormatter,
1314
sync::Arc,
1415
};
1516
use core::fmt::{self, Write};
@@ -143,14 +144,14 @@ 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 = NullTerminatedFormatter::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 },
151-
)?;
152-
raw_writer.write_fmt(name)?;
153-
raw_writer.write_char('\0')?;
152+
)
153+
.ok_or(error::code::EINVAL)?;
154+
writer.write_fmt(name)?;
154155

155156
// SAFETY: `gendisk` points to a valid and initialized instance of
156157
// `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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,6 @@ impl<'a> NullTerminatedFormatter<'a> {
858858
Some(Self { buffer })
859859
}
860860

861-
#[expect(dead_code)]
862861
pub(crate) fn from_array<const N: usize>(
863862
buffer: &'a mut [crate::ffi::c_char; N],
864863
) -> Option<NullTerminatedFormatter<'a>> {

0 commit comments

Comments
 (0)