]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
rust: str: implement `Index` for `BStr`
authorAndreas Hindborg <a.hindborg@kernel.org>
Thu, 27 Feb 2025 14:38:08 +0000 (15:38 +0100)
committerMiguel Ojeda <ojeda@kernel.org>
Thu, 20 Mar 2025 20:44:47 +0000 (21:44 +0100)
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>
rust/kernel/str.rs

index 002dcddf7c7687522a0acbff3ce6f65fa890e216..ba6b1a5c4f99d28a1572c318f58920ca3adfac9a 100644 (file)
@@ -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