rust: str: implement `AsRef<BStr>` for `[u8]` and `BStr`
authorAndreas Hindborg <a.hindborg@kernel.org>
Thu, 27 Feb 2025 14:38:09 +0000 (15:38 +0100)
committerMiguel Ojeda <ojeda@kernel.org>
Thu, 20 Mar 2025 20:44:47 +0000 (21:44 +0100)
Implement `AsRef<BStr>` for `[u8]` and `BStr` so these can be used
interchangeably for operations on `BStr`.

Reviewed-by: Gary Guo <gary@garyguo.net>
Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Tested-by: Daniel Gomez <da.gomez@samsung.com>
Link: https://lore.kernel.org/r/20250227-module-params-v3-v8-3-ceeee85d9347@kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
rust/kernel/str.rs

index ba6b1a5c4f99d28a1572c318f58920ca3adfac9a..c6bd2c69543dceeed6581d230cf643b7287532f1 100644 (file)
@@ -125,6 +125,18 @@ where
     }
 }
 
+impl AsRef<BStr> for [u8] {
+    fn as_ref(&self) -> &BStr {
+        BStr::from_bytes(self)
+    }
+}
+
+impl AsRef<BStr> for BStr {
+    fn as_ref(&self) -> &BStr {
+        self
+    }
+}
+
 /// Creates a new [`BStr`] from a string literal.
 ///
 /// `b_str!` converts the supplied string literal to byte string, so non-ASCII