An `as` cast silently drops the bits that do not fit the target type,
and nothing at the cast shows whether that can happen. As part of a
larger effort to reduce the number of `as` casts in Tyr, derive the
conversions between `CacheMode` and `Bounded` instead of writing them
by hand. This removes the cast that converts `CacheMode` to its integer
value.

No functional change intended.

Signed-off-by: Daniel Almeida <[email protected]>
---
 drivers/gpu/drm/tyr/fw.rs | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)

diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index 2fa9852e0b83..cf880f685922 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -26,10 +26,11 @@
         poll,
         Io, //
     },
-    num::{
-        casts::FromSafeCast,
-        Bounded, //
+    macros::{
+        From,
+        Into, //
     },
+    num::casts::FromSafeCast,
     prelude::*,
     str::CString,
     sync::{
@@ -76,7 +77,8 @@
 
 pub(super) const CSF_MCU_SHARED_REGION_START: u32 = 0x04000000;
 
-#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq, From, Into)]
+#[convert(Bounded<u32, 2>)]
 #[repr(u8)]
 pub(super) enum CacheMode {
     None = 0,
@@ -85,26 +87,6 @@ pub(super) enum CacheMode {
     CachedCoherent = 3,
 }
 
-impl From<Bounded<u32, 2>> for CacheMode {
-    fn from(value: Bounded<u32, 2>) -> Self {
-        match value.get() {
-            0 => Self::None,
-            1 => Self::Cached,
-            2 => Self::UncachedCoherent,
-            3 => Self::CachedCoherent,
-            _ => unreachable!(),
-        }
-    }
-}
-
-impl From<CacheMode> for Bounded<u32, 2> {
-    #[expect(clippy::as_conversions)]
-    fn from(value: CacheMode) -> Self {
-        // CAST: `CacheMode` is `#[repr(u8)]`, so the cast is lossless.
-        Bounded::try_new(value as u32).unwrap()
-    }
-}
-
 bitfield! {
     pub(super) struct SectionFlags(u32) {
         0:0 read => bool;

-- 
2.56.0

Reply via email to