diff mbox series

[RFC,v2,19/20] rust: allow extracting the buffer from a CString

Message ID 20250227030952.2319050-20-alistair@alistair23.me
State New
Headers show
Series lib: Rust implementation of SPDM | expand

Commit Message

Alistair Francis Feb. 27, 2025, 3:09 a.m. UTC
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(+)

Comments

Alice Ryhl Feb. 27, 2025, 7:52 a.m. UTC | #1
On Thu, Feb 27, 2025 at 4:12 AM Alistair Francis <alistair@alistair23.me> wrote:
>
> 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>

I believe the idiomatic naming for a method like this is `into_vec`.

Alice
diff mbox series

Patch

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 28e2201604d6..76862e91b81a 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -855,6 +855,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 take_buffer(self) -> KVec<u8> {
+        self.buf
+    }
 }
 
 impl Deref for CString {