From: Ritvik Gupta <ritvikfoss@gmail.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH v4] rust: kernel: introduce `unsafe_precondition_assert!` macro
Date: Wed, 13 Aug 2025 02:56:20 +0530 [thread overview]
Message-ID: <aJuxfHaslGAlBJTR@fedora> (raw)
In-Reply-To: <20250808192005.209188-1-ritvikfoss@gmail.com>
> +/// # Examples
> +///
> +/// ```no_run
> +/// # use kernel::unsafe_precondition_assert;
> +/// # use kernel::cpu::{nr_cpu_ids, CpuId};
> +/// /// Creates a [`CpuId`] from the given `id` without bound checks.
> +/// ///
> +/// /// # Safety
> +/// ///
> +/// /// The caller must ensure that `id` is a valid CPU ID (i.e, `0 <= id < nr_cpu_ids()`).
> +/// unsafe fn new_cpu_id_unchecked(id: i32) -> CpuId {
> +/// let max_cpus = nr_cpu_ids();
> +///
> +/// unsafe_precondition_assert!(id >= 0, "id ({}) must be positive", id);
> +///
> +/// unsafe_precondition_assert!(
> +/// id < max_cpus, "id ({}) must be less than total CPUs ({})", id, max_cpus
> +/// );
> +///
> +/// CpuId(id)
> +/// }
> +/// ```
The example I've included is incorrect because `CpuId` inner field is private,
so it can't be constructed this way.
`CpuId::from_i32_unchecked()` exists, using it here would make the example a
trivial wrapper around that method, that would be unnecessary and confusing.
Also, I wasn't sure about the doctest config when I wrote it.
I'll send v5 with correct and much better example.
Plz ignore it. Sorry for the noise.
prev parent reply other threads:[~2025-08-12 21:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-08 19:17 [PATCH v4] rust: kernel: introduce `unsafe_precondition_assert!` macro Ritvik Gupta
2025-08-11 9:19 ` Alice Ryhl
2025-08-11 11:40 ` Ritvik Gupta
2025-08-12 15:52 ` kernel test robot
2025-08-12 18:08 ` Ritvik Gupta
2025-08-12 21:26 ` Ritvik Gupta [this message]
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=aJuxfHaslGAlBJTR@fedora \
--to=ritvikfoss@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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).