[PATCH v2 20/21] rust: allow extracting the buffer from a CString

From: alistair23

Date: Tue Jun 23 2026 - 01:04:20 EST


From: Alistair Francis <alistair@xxxxxxxxxxxxx>

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@xxxxxxxxxxxxx>
Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>
---
rust/kernel/str.rs | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index a556788bcc5e..3fd8a218547f 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -870,6 +870,12 @@ 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`]
+ #[inline]
+ pub fn into_vec(self) -> KVec<u8> {
+ self.buf
+ }
}

impl Deref for CString {
--
2.54.0