[PATCH v3 08/16] rust: block: remove `RawWriter`

Andreas Hindborg posted 16 patches 2 months, 3 weeks ago
There is a newer version of this series
[PATCH v3 08/16] rust: block: remove `RawWriter`
Posted by Andreas Hindborg 2 months, 3 weeks ago
`RawWriter` is now dead code, so remove it.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/block/mq.rs            |  1 -
 rust/kernel/block/mq/raw_writer.rs | 56 --------------------------------------
 2 files changed, 57 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/raw_writer.rs b/rust/kernel/block/mq/raw_writer.rs
deleted file mode 100644
index 0aef55703e71..000000000000
--- a/rust/kernel/block/mq/raw_writer.rs
+++ /dev/null
@@ -1,56 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-use core::fmt::{self, Write};
-
-use crate::error::Result;
-use crate::prelude::EINVAL;
-
-/// A mutable reference to a byte buffer where a string can be written into.
-///
-/// # Invariants
-///
-/// `buffer` is always null terminated.
-pub(crate) struct RawWriter<'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>> {
-        *(buffer.last_mut().ok_or(EINVAL)?) = 0;
-
-        // INVARIANT: We null terminated the buffer above.
-        Ok(Self { buffer, pos: 0 })
-    }
-
-    #[expect(dead_code)]
-    pub(crate) fn from_array<const N: usize>(
-        a: &'a mut [crate::ffi::c_char; N],
-    ) -> Result<RawWriter<'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) },
-        )
-    }
-}
-
-impl Write for RawWriter<'_> {
-    fn write_str(&mut self, s: &str) -> fmt::Result {
-        let bytes = s.as_bytes();
-        let len = bytes.len();
-
-        // We do not want to overwrite our null terminator
-        if self.pos + len > self.buffer.len() - 1 {
-            return Err(fmt::Error);
-        }
-
-        // INVARIANT: We are not overwriting the last byte
-        self.buffer[self.pos..self.pos + len].copy_from_slice(bytes);
-
-        self.pos += len;
-
-        Ok(())
-    }
-}

-- 
2.47.2
Re: [PATCH v3 08/16] rust: block: remove `RawWriter`
Posted by Daniel Almeida 2 months ago

> On 11 Jul 2025, at 08:43, Andreas Hindborg <a.hindborg@kernel.org> wrote:
> 
> `RawWriter` is now dead code, so remove it.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/block/mq.rs            |  1 -
> rust/kernel/block/mq/raw_writer.rs | 56 --------------------------------------
> 2 files changed, 57 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/raw_writer.rs b/rust/kernel/block/mq/raw_writer.rs
> deleted file mode 100644
> index 0aef55703e71..000000000000
> --- a/rust/kernel/block/mq/raw_writer.rs
> +++ /dev/null
> @@ -1,56 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0
> -
> -use core::fmt::{self, Write};
> -
> -use crate::error::Result;
> -use crate::prelude::EINVAL;
> -
> -/// A mutable reference to a byte buffer where a string can be written into.
> -///
> -/// # Invariants
> -///
> -/// `buffer` is always null terminated.
> -pub(crate) struct RawWriter<'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>> {
> -        *(buffer.last_mut().ok_or(EINVAL)?) = 0;
> -
> -        // INVARIANT: We null terminated the buffer above.
> -        Ok(Self { buffer, pos: 0 })
> -    }
> -
> -    #[expect(dead_code)]
> -    pub(crate) fn from_array<const N: usize>(
> -        a: &'a mut [crate::ffi::c_char; N],
> -    ) -> Result<RawWriter<'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) },
> -        )
> -    }
> -}
> -
> -impl Write for RawWriter<'_> {
> -    fn write_str(&mut self, s: &str) -> fmt::Result {
> -        let bytes = s.as_bytes();
> -        let len = bytes.len();
> -
> -        // We do not want to overwrite our null terminator
> -        if self.pos + len > self.buffer.len() - 1 {
> -            return Err(fmt::Error);
> -        }
> -
> -        // INVARIANT: We are not overwriting the last byte
> -        self.buffer[self.pos..self.pos + len].copy_from_slice(bytes);
> -
> -        self.pos += len;
> -
> -        Ok(())
> -    }
> -}
> 
> -- 
> 2.47.2
> 
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Re: [PATCH v3 08/16] rust: block: remove `RawWriter`
Posted by Alice Ryhl 2 months, 3 weeks ago
On Fri, Jul 11, 2025 at 01:43:09PM +0200, Andreas Hindborg wrote:
> `RawWriter` is now dead code, so remove it.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

Could be squashed into previous commit IMO, but no big deal.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>