summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorAndreas Hindborg <a.hindborg@kernel.org>2025-02-27 15:38:08 +0100
committerMiguel Ojeda <ojeda@kernel.org>2025-03-20 21:44:47 +0100
commit50a5ff0a95a54d5a710f1f2547ecf8af12b6b83a (patch)
tree7c3a63e5beeebba9dc0a2842e68cdae2f7761caf /rust
parent3eff946dfec732ca9e8585bd44f93acc12646d21 (diff)
rust: str: implement `Index` for `BStr`
The `Index` implementation on `BStr` was lost when we switched `BStr` from a type alias of `[u8]` to a newtype. Add back `Index` by implementing `Index` for `BStr` when `Index` would be implemented for `[u8]`. Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Tested-by: Daniel Almeida <daniel.almeida@collabora.com> Reviewed-by: Fiona Behrens <me@kloenk.dev> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Daniel Gomez <da.gomez@samsung.com> Link: https://lore.kernel.org/r/20250227-module-params-v3-v8-2-ceeee85d9347@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/str.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index 002dcddf7c76..ba6b1a5c4f99 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -114,6 +114,17 @@ impl PartialEq for BStr {
}
}
+impl<Idx> Index<Idx> for BStr
+where
+ [u8]: Index<Idx, 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