Re: [PATCH] rust: cpu: use `unsafe_precondition_assert!`
From: Miguel Ojeda
Date: Mon May 18 2026 - 13:52:15 EST
On Mon, May 18, 2026 at 7:40 PM Ritvik Gupta <ritvikfoss@xxxxxxxxx> wrote:
>
> Replace `debug_assert!` invocations with
> `unsafe_precondition_assert!` in `from_i32_unchecked`
> and `from_u32_unchecked` unsafe functions.
Thanks for the patch!
This should mention that we are adding messages too. That could
actually be a second patch in the series to make it even clearer, but
it is not a big a deal either way (and perhaps `rustfmt` makes that
first patch not that clean due to the line wrapping anyway).
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Link: https://github.com/Rust-for-Linux/linux/issues/1232
> -use crate::{bindings, device::Device, error::Result, prelude::ENODEV};
> +use crate::{bindings, device::Device, error::Result, prelude::ENODEV, unsafe_precondition_assert};
We should probably put it in the prelude along the others -- the
easier it is to type, the better, to encourage its use.
Do you want to add a first patch in the series doing that?
> + "CpuId::from_i32_unchecked requires a valid 'id' (i.e., `0 <= id < nr_cpu_ids()`)"
This seems to mix Markdown backquotes with single quotes, and then
doesn't use backquotes in the first part. Could we make it consistent
if we are adding the messages? e.g.
"`CpuId::from_i32_unchecked` requires a valid `id` (i.e., `0 <= id
< nr_cpu_ids()`)"
Sashiko has also spotted a copy-paste error in the one below.
Cheers,
Miguel