[PATCH 0/2] rust: num: casts: replace const type narrowing methods with a macro

Alexandre Courbot posted 2 patches 1 month ago
drivers/gpu/nova-core/falcon.rs                    |  12 +-
drivers/gpu/nova-core/falcon/fsp.rs                |   4 +-
drivers/gpu/nova-core/fb.rs                        |   2 +-
drivers/gpu/nova-core/fb/hal/gb100.rs              |   9 +-
drivers/gpu/nova-core/firmware.rs                  |   4 +-
drivers/gpu/nova-core/firmware/booter.rs           |   4 +-
drivers/gpu/nova-core/firmware/fwsec.rs            |   2 +-
drivers/gpu/nova-core/firmware/fwsec/bootloader.rs |   4 +-
drivers/gpu/nova-core/firmware/gsp.rs              |   9 +-
drivers/gpu/nova-core/firmware/tlv.rs              |  11 +-
drivers/gpu/nova-core/fsp.rs                       |   8 +-
drivers/gpu/nova-core/gsp.rs                       |   4 +-
drivers/gpu/nova-core/gsp/cmdq.rs                  |  22 +--
drivers/gpu/nova-core/gsp/fw.rs                    |  50 ++---
drivers/gpu/nova-core/gsp/fw/commands.rs           |   4 +-
drivers/gpu/nova-core/gsp/sequencer.rs             |   2 +-
drivers/gpu/nova-core/mctp.rs                      |   8 +-
drivers/gpu/nova-core/num.rs                       | 211 ---------------------
drivers/gpu/nova-core/vbios.rs                     |   2 +-
rust/kernel/num/casts.rs                           | 129 ++++++++-----
20 files changed, 163 insertions(+), 338 deletions(-)
[PATCH 0/2] rust: num: casts: replace const type narrowing methods with a macro
Posted by Alexandre Courbot 1 month ago
The `casts` module includes lossless const converter functions, as it is
sometimes necessary to cast a bindgen-generated constant into a narrower
type. Such conversions are known to be lossless at compile-time, but
using `as` for them still requires a `CAST` comment and carries the risk
of the conversion becoming lossy should the value of the constant
change.

Since these functions need to be evaluated at compile-time, they take
the expression to narrow as a generic const argument, requiring the use
of the turbofish syntax by callers. Also, a dedicated function is needed
per type conversion, resulting in 9 functions generated by a single
macro. All these factors make them hard to discover and a bit cumbersome
to use.

This series replaces these functions with a single `const_as` macro that
ensures an `as` conversion is lossless at build-time. Since it is unique
and not generated, it is easy to discover, and its syntax also looks
more accessible than the turbofish one.

Patch 2 then performs the conversion of nova-core to use these
conversion helpers in the process of removing its own superseded `num`
module.

This series is based on today's `master`.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
Alexandre Courbot (2):
      rust: num: casts: replace const type narrowing methods with a macro
      gpu: nova-core: use kernel lossless integer conversion module

 drivers/gpu/nova-core/falcon.rs                    |  12 +-
 drivers/gpu/nova-core/falcon/fsp.rs                |   4 +-
 drivers/gpu/nova-core/fb.rs                        |   2 +-
 drivers/gpu/nova-core/fb/hal/gb100.rs              |   9 +-
 drivers/gpu/nova-core/firmware.rs                  |   4 +-
 drivers/gpu/nova-core/firmware/booter.rs           |   4 +-
 drivers/gpu/nova-core/firmware/fwsec.rs            |   2 +-
 drivers/gpu/nova-core/firmware/fwsec/bootloader.rs |   4 +-
 drivers/gpu/nova-core/firmware/gsp.rs              |   9 +-
 drivers/gpu/nova-core/firmware/tlv.rs              |  11 +-
 drivers/gpu/nova-core/fsp.rs                       |   8 +-
 drivers/gpu/nova-core/gsp.rs                       |   4 +-
 drivers/gpu/nova-core/gsp/cmdq.rs                  |  22 +--
 drivers/gpu/nova-core/gsp/fw.rs                    |  50 ++---
 drivers/gpu/nova-core/gsp/fw/commands.rs           |   4 +-
 drivers/gpu/nova-core/gsp/sequencer.rs             |   2 +-
 drivers/gpu/nova-core/mctp.rs                      |   8 +-
 drivers/gpu/nova-core/num.rs                       | 211 ---------------------
 drivers/gpu/nova-core/vbios.rs                     |   2 +-
 rust/kernel/num/casts.rs                           | 129 ++++++++-----
 20 files changed, 163 insertions(+), 338 deletions(-)
---
base-commit: 66498c75b4f8017f62d720d9b59675bdf3abce91
change-id: 20260825-const_as-a792dad39943

Best regards,
--  
Alexandre Courbot <acourbot@nvidia.com>