diff mbox series

[v7,3/6] rust: str: implement `AsRef<BStr>` for `[u8]` and `BStr`

Message ID 20250218-module-params-v3-v7-3-5e1afabcac1b@kernel.org (mailing list archive)
State New
Headers show
Series rust: extend `module!` macro with integer parameter support | expand

Commit Message

Andreas Hindborg Feb. 18, 2025, 1 p.m. UTC
Implement `AsRef<BStr>` for `[u8]` and `BStr` so these can be used
interchangeably for operations on `BStr`.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 rust/kernel/str.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Daniel Almeida Feb. 21, 2025, 4:01 p.m. UTC | #1
> On 18 Feb 2025, at 10:00, Andreas Hindborg <a.hindborg@kernel.org> wrote:
> 
> Implement `AsRef<BStr>` for `[u8]` and `BStr` so these can be used
> interchangeably for operations on `BStr`.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
> ---
> rust/kernel/str.rs | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index ba6b1a5c4f99d..c6bd2c69543dc 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -125,6 +125,18 @@ fn index(&self, index: Idx) -> &Self::Output {
>     }
> }
> 
> +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
> +    }
> +}

Why do you need this last one?

> +
> /// Creates a new [`BStr`] from a string literal.
> ///
> /// `b_str!` converts the supplied string literal to byte string, so non-ASCII
> 
> -- 
> 2.47.0
> 
> 
>
Daniel Almeida Feb. 21, 2025, 4:15 p.m. UTC | #2
> On 21 Feb 2025, at 13:01, Daniel Almeida <daniel.almeida@collabora.com> wrote:
> 
> 
> 
>> On 18 Feb 2025, at 10:00, Andreas Hindborg <a.hindborg@kernel.org> wrote:
>> 
>> Implement `AsRef<BStr>` for `[u8]` and `BStr` so these can be used
>> interchangeably for operations on `BStr`.
>> 
>> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
>> ---
>> rust/kernel/str.rs | 12 ++++++++++++
>> 1 file changed, 12 insertions(+)
>> 
>> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
>> index ba6b1a5c4f99d..c6bd2c69543dc 100644
>> --- a/rust/kernel/str.rs
>> +++ b/rust/kernel/str.rs
>> @@ -125,6 +125,18 @@ fn index(&self, index: Idx) -> &Self::Output {
>>    }
>> }
>> 
>> +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
>> +    }
>> +}
> 
> Why do you need this last one?

I see that this is used by the following patch.

> 
>> +
>> /// Creates a new [`BStr`] from a string literal.
>> ///
>> /// `b_str!` converts the supplied string literal to byte string, so non-ASCII
>> 
>> -- 
>> 2.47.0

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Gary Guo Feb. 21, 2025, 6:52 p.m. UTC | #3
On Tue, 18 Feb 2025 14:00:45 +0100
Andreas Hindborg <a.hindborg@kernel.org> wrote:

> Implement `AsRef<BStr>` for `[u8]` and `BStr` so these can be used
> interchangeably for operations on `BStr`.
> 
> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/str.rs | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index ba6b1a5c4f99d..c6bd2c69543dc 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -125,6 +125,18 @@ fn index(&self, index: Idx) -> &Self::Output {
>      }
>  }
>  
> +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
>
diff mbox series

Patch

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index ba6b1a5c4f99d..c6bd2c69543dc 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -125,6 +125,18 @@  fn index(&self, index: Idx) -> &Self::Output {
     }
 }
 
+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