]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: num: make Bounded::get const
authorAlexandre Courbot <acourbot@nvidia.com>
Sat, 14 Mar 2026 01:06:14 +0000 (10:06 +0900)
committerDanilo Krummrich <dakr@kernel.org>
Tue, 17 Mar 2026 19:04:11 +0000 (20:04 +0100)
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.

Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20260314-register-v9-4-86805b2f7e9d@nvidia.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/num/bounded.rs

index d28d118abd8ed49e3b30ffa3f032fde1a61a901c..bbab6bbcb3156bab2dbb0d4922becfcff3c2c6d5 100644 (file)
@@ -379,6 +379,9 @@ where
 
     /// 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.
+    ///
     /// # Examples
     ///
     /// ```
@@ -387,8 +390,8 @@ where
     /// let v = Bounded::<u32, 4>::new::<7>();
     /// assert_eq!(v.get(), 7u32);
     /// ```
-    pub fn get(self) -> T {
-        *self.deref()
+    pub const fn get(self) -> T {
+        self.0
     }
 
     /// Increases the number of bits usable for `self`.