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>
This commit is contained in:
Andreas Hindborg
2025-02-27 15:38:08 +01:00
committed by Miguel Ojeda
parent 3eff946dfe
commit 50a5ff0a95

View 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