Re: [PATCH v6 4/9] rust: num: make Bounded::get const

From: Gary Guo

Date: Mon Feb 16 2026 - 04:18:15 EST


On 2026-02-16 08:56, Alice Ryhl wrote:
On Mon, Feb 16, 2026 at 05:04:40PM +0900, Alexandre Courbot wrote:
There is a need to access the inner value of a `Bounded` in const
context, notably for bitfields and registers. Remove the invariant check
of `Bounded::get`, which allows us to make it const.

Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
---
rust/kernel/num/bounded.rs | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 8e4c08924d96..b09b93cd0c5c 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -379,6 +379,9 @@ pub fn from_expr(expr: T) -> Self {

/// Returns the wrapped value as the backing type.
///
+ /// This is similar to the [`Deref`] implementation, but doesn't enforce the size invariant of
+ /// the [`Bounded`], which might produce slightly less optimal code.

The unreachable_unchecked() hint is const, so I see no reason you
couldn't add the same call in `fn get`.

The issue is that trait impls are not const, so the if condition cannot be represented in const yet.

Best,
Gary