rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel Almeida" <daniel.almeida@collabora.com>
To: "Nicolas Dufresne" <nicolas@ndufresne.ca>
Cc: wedsonaf@gmail.com, ojeda@kernel.org, mchehab@kernel.org,
	hverkuil@xs4all.nl, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
	kernel@collabora.com
Subject: Re: [RFC PATCH v2 2/2] media: rkvdec: rewrite parts of the driver in Rust
Date: Thu, 07 Mar 2024 21:45:33 +0000	[thread overview]
Message-ID: <144b27-65ea3580-f-61876d80@130641868> (raw)
In-Reply-To: <c70935815ffc29ae5256b94c0e4880952abad79c.camel@ndufresne.ca>

Hi Nicolas,

> struct reg123 {
>   val1 :3  // bit 31-29
>   val2 :20 // bit 28-9
>   val3 :9  // bit 8-0
> };

What you're describing can be modeled as Ranges in Rust:

```
use core::ops::Range;

struct Foo {
    reg1: Range<u32>,
    reg2: Range<u32>,
    reg3: Range<u32>
}

const FOO_REGMAP: Foo = Foo {
        reg1: 0..3,
        reg2: 3..24,
        reg3: 24..32
};
```

It becomes more useful when you pair that with a bit writer. For an example of previous art, see Faith's work: [0]

This has asserts in the right places so that you do not shoot yourself in the foot. IMHO, such a data structure can be shared with the whole Rust code in the kernel.

You can then describe your writes using the ranges, e.g.: [1] 

But as we've established, instead of writing the ranges down directly, you can simply refer to them as FOO_REGMAP.reg1, FOO_REGMAP.reg2 and so on.

I believe this is both more readable and safer.

[0]: https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/nouveau/compiler/bitview/lib.rs?ref_type=heads

[1]: https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/nouveau/compiler/nak/encode_sm70.rs?ref_type=heads#L228


  reply	other threads:[~2024-03-07 21:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-27 21:51 [RFC PATCH 0/1] Rewrite the VP9 codec library in Rust Daniel Almeida
2024-02-27 21:51 ` [RFC PATCH 1/1] v4l2-core: rewrite the VP9 " Daniel Almeida
2024-02-28 14:13   ` Alice Ryhl
2024-02-28 17:59     ` Daniel Almeida
2024-03-07 19:08 ` [RFC PATCH v2 0/2] Rewrite parts of rkvdec driver and the VP9 codec " Daniel Almeida
2024-03-07 19:08   ` [RFC PATCH v2 1/2] v4l2-core: rewrite the VP9 " Daniel Almeida
2024-03-07 19:08   ` [RFC PATCH v2 2/2] media: rkvdec: rewrite parts of the driver " Daniel Almeida
2024-03-07 20:56     ` Nicolas Dufresne
2024-03-07 21:45       ` Daniel Almeida [this message]
2024-03-07 20:04   ` [RFC PATCH v2 0/2] Rewrite parts of rkvdec driver and the VP9 codec library " Deborah Brouwer

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=144b27-65ea3580-f-61876d80@130641868 \
    --to=daniel.almeida@collabora.com \
    --cc=hverkuil@xs4all.nl \
    --cc=kernel@collabora.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=nicolas@ndufresne.ca \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).