From e9cc4754ccfa8edf30d7576dd2fdce07181c062a Mon Sep 17 00:00:00 2001
From: Fabien Parent <fabien.parent@linaro.org>
Date: Tue, 5 Mar 2024 19:39:54 -0800
Subject: [PATCH] rust: str: allow dereferencing BStr in a const ctx

impl Deref doesn't work in const context. Add a function
that is similar to implementing `deref` but that can
be used in `const` context.

Signed-off-by: Fabien Parent <fabien.parent@linaro.org>
---
 rust/kernel/str.rs | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index bb8d4f41475b5..0c5b472065dce 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -32,6 +32,12 @@ impl BStr {
         // SAFETY: `BStr` is transparent to `[u8]`.
         unsafe { &*(bytes as *const [u8] as *const BStr) }
     }
+
+    /// Returns a reference to the inner [u8].
+    #[inline]
+    pub const fn deref_const(&self) -> &[u8] {
+        &self.0
+    }
 }
 
 impl fmt::Display for BStr {
@@ -103,7 +109,7 @@ impl Deref for BStr {
 
     #[inline]
     fn deref(&self) -> &Self::Target {
-        &self.0
+        self.deref_const()
     }
 }
 
-- 
GitLab