mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: Daniel Almeida <daniel.almeida@collabora.com>,
	 Alice Ryhl <aliceryhl@google.com>,
	Danilo Krummrich <dakr@kernel.org>,
	 David Airlie <airlied@gmail.com>,
	Simona Vetter <simona@ffwll.ch>
Cc: Kaiqi Guo <chaoji_xinren@163.com>,
	 Alexandre Courbot <acourbot@nvidia.com>,
	dri-devel@lists.freedesktop.org,  rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/4] drm/tyr: use the conversion derives for `CacheMode`
Date: Thu, 26 Nov 2026 17:10:45 -0300	[thread overview]
Message-ID: <20261126-tyr-enum-derives-v1-3-a487b0e1bee3@collabora.com> (raw)
In-Reply-To: <20261126-tyr-enum-derives-v1-0-a487b0e1bee3@collabora.com>

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 <daniel.almeida@collabora.com>
---
 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


  parent reply	other threads:[~2026-11-26 20:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 20:10 [PATCH 0/4] drm/tyr: use the conversion derives for the enums Daniel Almeida
2026-11-26 20:10 ` [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums Daniel Almeida
2026-11-26 20:10 ` [PATCH 2/4] drm/tyr: use the conversion derives for the MMU_AS_CONTROL enums Daniel Almeida
2026-11-26 20:10 ` Daniel Almeida [this message]
2026-11-26 20:10 ` [PATCH 4/4] drm/tyr: use the `TryFrom` derive for `EntryType` Daniel Almeida

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126-tyr-enum-derives-v1-3-a487b0e1bee3@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=chaoji_xinren@163.com \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®