[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250601-borrow_impls-v1-4-e1caeb428db4@nvidia.com>
Date: Sun, 01 Jun 2025 12:00:42 +0900
From: Alexandre Courbot <acourbot@...dia.com>
To: Danilo Krummrich <dakr@...nel.org>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
Alexandre Courbot <acourbot@...dia.com>
Subject: [PATCH 4/4] rust: alloc: implement `Borrow` and `BorrowMut` for
`CString`
Implement these two common traits, which allow generic types to store
either an owned value or a reference to it, leveraging the `Deref`
implementation.
Signed-off-by: Alexandre Courbot <acourbot@...dia.com>
---
rust/kernel/str.rs | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index a927db8e079c3597860880947a03959e1d6d712e..f5da512ebcfef6c8d5227e8177ae32f6194ecffb 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -3,6 +3,7 @@
//! String representations.
use crate::alloc::{flags::*, AllocError, KVec};
+use core::borrow::{Borrow, BorrowMut};
use core::fmt::{self, Write};
use core::ops::{self, Deref, DerefMut, Index};
@@ -911,6 +912,18 @@ fn deref_mut(&mut self) -> &mut Self::Target {
}
}
+impl Borrow<CStr> for CString {
+ fn borrow(&self) -> &CStr {
+ self.deref()
+ }
+}
+
+impl BorrowMut<CStr> for CString {
+ fn borrow_mut(&mut self) -> &mut CStr {
+ self.deref_mut()
+ }
+}
+
impl<'a> TryFrom<&'a CStr> for CString {
type Error = AllocError;
--
2.49.0
Powered by blists - more mailing lists