Skip to content

Commit 50a5ff0

Browse files
Andreas Hindborgojeda
authored andcommitted
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>
1 parent 3eff946 commit 50a5ff0

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

rust/kernel/str.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ impl PartialEq for BStr {
114114
}
115115
}
116116

117+
impl<Idx> Index<Idx> for BStr
118+
where
119+
[u8]: Index<Idx, Output = [u8]>,
120+
{
121+
type Output = Self;
122+
123+
fn index(&self, index: Idx) -> &Self::Output {
124+
BStr::from_bytes(&self.0[index])
125+
}
126+
}
127+
117128
/// Creates a new [`BStr`] from a string literal.
118129
///
119130
/// `b_str!` converts the supplied string literal to byte string, so non-ASCII

0 commit comments

Comments
 (0)