[RFC v3 23/27] rust: allow extracting the buffer from a CString

alistair23@gmail.com posted 27 patches 1 month, 2 weeks ago
[RFC v3 23/27] rust: allow extracting the buffer from a CString
Posted by alistair23@gmail.com 1 month, 2 weeks ago
From: Alistair Francis <alistair@alistair23.me>

The kernel CString is a wrapper aroud a KVec. This patch allows
retrieving the underlying buffer and consuming the CString. This allows
users to create a CString from a string and then retrieve the underlying
buffer.

Signed-off-by: Alistair Francis <alistair@alistair23.me>
---
 rust/kernel/str.rs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index fa87779d2253..17426db83c9f 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -844,6 +844,11 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> Result<Self, Error> {
         // exist in the buffer.
         Ok(Self { buf })
     }
+
+    /// Return the internal buffer while consuming the original [`CString`]
+    pub fn into_vec(self) -> KVec<u8> {
+        self.buf
+    }
 }
 
 impl Deref for CString {
-- 
2.52.0
Re: [RFC v3 23/27] rust: allow extracting the buffer from a CString
Posted by Gary Guo 1 month, 1 week ago
On 2026-02-11 03:29, alistair23@gmail.com wrote:
> From: Alistair Francis <alistair@alistair23.me>
> 
> The kernel CString is a wrapper aroud a KVec. This patch allows
> retrieving the underlying buffer and consuming the CString. This allows
> users to create a CString from a string and then retrieve the 
> underlying
> buffer.
> 
> Signed-off-by: Alistair Francis <alistair@alistair23.me>
> ---
>  rust/kernel/str.rs | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index fa87779d2253..17426db83c9f 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -844,6 +844,11 @@ pub fn try_from_fmt(args: fmt::Arguments<'_>) -> 
> Result<Self, Error> {
>          // exist in the buffer.
>          Ok(Self { buf })
>      }
> +
> +    /// Return the internal buffer while consuming the original 
> [`CString`]
> +    pub fn into_vec(self) -> KVec<u8> {
> +        self.buf
> +    }

`#[inline]` here, too.

Feel free to add my R-b after adding it.

Best,
Gary

>  }
> 
>  impl Deref for CString {