Skip to content

Commit eae8e61

Browse files
committed
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 <[email protected]>
1 parent 632bcff commit eae8e61

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

rust/kernel/str.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ impl BStr {
3131
// SAFETY: `BStr` is transparent to `[u8]`.
3232
unsafe { &*(bytes as *const [u8] as *const BStr) }
3333
}
34+
35+
/// Returns a reference to the inner [u8].
36+
#[inline]
37+
pub const fn deref_const(&self) -> &[u8] {
38+
&self.0
39+
}
3440
}
3541

3642
impl fmt::Display for BStr {
@@ -102,7 +108,7 @@ impl Deref for BStr {
102108

103109
#[inline]
104110
fn deref(&self) -> &Self::Target {
105-
&self.0
111+
self.deref_const()
106112
}
107113
}
108114

0 commit comments

Comments
 (0)