rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andreas Hindborg <a.hindborg@kernel.org>
To: "Boqun Feng" <boqun.feng@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Jens Axboe" <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Andreas Hindborg <a.hindborg@kernel.org>
Subject: [PATCH 3/9] rust: block,core: rename `RawWriter` to `BufferWriter`
Date: Mon, 16 Jun 2025 15:23:53 +0200	[thread overview]
Message-ID: <20250616-rnull-up-v6-16-v1-3-a4168b8e76b2@kernel.org> (raw)
In-Reply-To: <20250616-rnull-up-v6-16-v1-0-a4168b8e76b2@kernel.org>

Rename the `RawWriter` to `BufferWriter`, wihich is a more suitable name.
Also move the module from `block` to `str`.

The ability to format a string to a byte buffer is something that is not
specific to `block`, so there is no reason this code should live in
`block`.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

---

`BufferWriter` is used in `rnull` for interacting with `configfs`.
---
 rust/kernel/block/mq.rs                                 |  1 -
 rust/kernel/block/mq/gen_disk.rs                        |  9 +++++----
 rust/kernel/str.rs                                      |  3 +++
 .../{block/mq/raw_writer.rs => str/buffer_writer.rs}    | 17 +++++++++++------
 4 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs
index fb0f393c1cea..faa3ccb5a49a 100644
--- a/rust/kernel/block/mq.rs
+++ b/rust/kernel/block/mq.rs
@@ -89,7 +89,6 @@
 
 pub mod gen_disk;
 mod operations;
-mod raw_writer;
 mod request;
 mod tag_set;
 
diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs
index cd54cd64ea88..a04b709514ac 100644
--- a/rust/kernel/block/mq/gen_disk.rs
+++ b/rust/kernel/block/mq/gen_disk.rs
@@ -5,10 +5,11 @@
 //! C header: [`include/linux/blkdev.h`](srctree/include/linux/blkdev.h)
 //! C header: [`include/linux/blk_mq.h`](srctree/include/linux/blk_mq.h)
 
-use crate::block::mq::{raw_writer::RawWriter, Operations, TagSet};
+use crate::block::mq::{Operations, TagSet};
 use crate::{bindings, error::from_err_ptr, error::Result, sync::Arc};
 use crate::{error, static_lock_class};
 use core::fmt::{self, Write};
+use kernel::str::BufferWriter;
 
 /// A builder for [`GenDisk`].
 ///
@@ -139,14 +140,14 @@ pub fn build<T: Operations>(
         // SAFETY: `gendisk` is a valid pointer as we initialized it above
         unsafe { (*gendisk).fops = &TABLE };
 
-        let mut raw_writer = RawWriter::from_array(
+        let mut writer = BufferWriter::from_array(
             // SAFETY: `gendisk` points to a valid and initialized instance. We
             // have exclusive access, since the disk is not added to the VFS
             // yet.
             unsafe { &mut (*gendisk).disk_name },
         )?;
-        raw_writer.write_fmt(name)?;
-        raw_writer.write_char('\0')?;
+        writer.write_fmt(name)?;
+        writer.write_char('\0')?;
 
         // SAFETY: `gendisk` points to a valid and initialized instance of
         // `struct gendisk`. `set_capacity` takes a lock to synchronize this
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index a927db8e079c..050793fb7d3a 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -936,3 +936,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 macro_rules! fmt {
     ($($f:tt)*) => ( ::core::format_args!($($f)*) )
 }
+
+mod buffer_writer;
+pub use buffer_writer::BufferWriter;
diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/str/buffer_writer.rs
similarity index 77%
rename from rust/kernel/block/mq/raw_writer.rs
rename to rust/kernel/str/buffer_writer.rs
index 7e2159e4f6a6..364842a6cff8 100644
--- a/rust/kernel/block/mq/raw_writer.rs
+++ b/rust/kernel/str/buffer_writer.rs
@@ -10,14 +10,14 @@
 /// # Invariants
 ///
 /// `buffer` is always null terminated.
-pub(crate) struct RawWriter<'a> {
+pub struct BufferWriter<'a> {
     buffer: &'a mut [u8],
     pos: usize,
 }
 
-impl<'a> RawWriter<'a> {
-    /// Create a new `RawWriter` instance.
-    fn new(buffer: &'a mut [u8]) -> Result<RawWriter<'a>> {
+impl<'a> BufferWriter<'a> {
+    /// Create a new [`Self`] instance.
+    pub fn new(buffer: &'a mut [u8]) -> Result<BufferWriter<'a>> {
         *(buffer.last_mut().ok_or(EINVAL)?) = 0;
 
         // INVARIANT: We null terminated the buffer above.
@@ -26,16 +26,21 @@ fn new(buffer: &'a mut [u8]) -> Result<RawWriter<'a>> {
 
     pub(crate) fn from_array<const N: usize>(
         a: &'a mut [crate::ffi::c_char; N],
-    ) -> Result<RawWriter<'a>> {
+    ) -> Result<BufferWriter<'a>> {
         Self::new(
             // SAFETY: the buffer of `a` is valid for read and write as `u8` for
             // at least `N` bytes.
             unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::<u8>(), N) },
         )
     }
+
+    /// Return the position of the write pointer in the underlying buffer.
+    pub fn pos(&self) -> usize {
+        self.pos
+    }
 }
 
-impl Write for RawWriter<'_> {
+impl Write for BufferWriter<'_> {
     fn write_str(&mut self, s: &str) -> fmt::Result {
         let bytes = s.as_bytes();
         let len = bytes.len();

-- 
2.47.2



  parent reply	other threads:[~2025-06-16 13:26 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-16 13:23 [PATCH 0/9] rnull: add configfs, remote completion to rnull Andreas Hindborg
2025-06-16 13:23 ` [PATCH 1/9] rust: block: remove trait bound from `mq::Request` definition Andreas Hindborg
2025-06-16 13:23 ` [PATCH 2/9] rust: block: add block related constants Andreas Hindborg
2025-06-16 13:23 ` Andreas Hindborg [this message]
2025-07-07 14:18   ` [PATCH 3/9] rust: block,core: rename `RawWriter` to `BufferWriter` Miguel Ojeda
2025-07-07 14:58     ` Andreas Hindborg
2025-07-07 18:25       ` Miguel Ojeda
2025-07-08  8:44         ` Andreas Hindborg
2025-06-16 13:23 ` [PATCH 4/9] rnull: move driver to separate directory Andreas Hindborg
2025-06-16 13:23 ` [PATCH 5/9] rnull: enable configuration via `configfs` Andreas Hindborg
2025-06-16 13:23 ` [PATCH 6/9] rust: block: add `GenDisk` private data support Andreas Hindborg
2025-06-16 13:23 ` [PATCH 7/9] rust: block: mq: fix spelling in a safety comment Andreas Hindborg
2025-06-16 13:23 ` [PATCH 8/9] rust: block: add remote completion to `Request` Andreas Hindborg
2025-06-16 13:23 ` [PATCH 9/9] rnull: add soft-irq completion support Andreas Hindborg
2025-06-16 13:56   ` Johannes Thumshirn
2025-06-17  8:08     ` Andreas Hindborg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250616-rnull-up-v6-16-v1-3-a4168b8e76b2@kernel.org \
    --to=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=axboe@kernel.dk \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).