diff mbox series

[v5,2/5] rust: str: implement `Index` for `BStr`

Message ID 20250204-module-params-v3-v5-2-bf5ec2041625@kernel.org (mailing list archive)
State New
Headers show
Series rust: extend `module!` macro with integer parameter support | expand

Commit Message

Andreas Hindborg Feb. 4, 2025, 12:02 p.m. UTC
The `Index` implementation on `BStr` was lost when we switched `BStr` from
a type alias of `[u8]` to a newtype. This patch adds back `Index` by
implementing `Index` for `BStr` when `Index` would be implemented for
`[u8]`.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/str.rs | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff mbox series

Patch

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 002dcddf7c7687522a0acbff3ce6f65fa890e216..1eb945bed77d6592216cf30678fcca70d4c0b3b3 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -114,6 +114,17 @@  fn eq(&self, other: &Self) -> bool {
     }
 }
 
+impl<Idx> Index<Idx> for BStr
+where
+    Idx: core::slice::SliceIndex<[u8], Output = [u8]>,
+{
+    type Output = Self;
+
+    fn index(&self, index: Idx) -> &Self::Output {
+        BStr::from_bytes(&self.0[index])
+    }
+}
+
 /// Creates a new [`BStr`] from a string literal.
 ///
 /// `b_str!` converts the supplied string literal to byte string, so non-ASCII