Re: [RFC PATCH v2 2/2] media: rkvdec: rewrite parts of the driver in Rust

From: Daniel Almeida
Date: Thu Mar 07 2024 - 16:46:55 EST


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