All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Miguel Ojeda" <miguel.ojeda.sandonis@gmail.com>,
	"Timur Tabi" <ttabi@nvidia.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	<rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v4] rust: introduce sfile macro for succinct code tracing
Date: Tue, 17 Jun 2025 09:29:08 +0200	[thread overview]
Message-ID: <DAOMQXNTLSGR.U0QOV8BU1I5V@kernel.org> (raw)
In-Reply-To: <CANiq72=cDk5rVEiAoudaQZUhNL_rne1EEjSEfiNuo_gkL3HOZw@mail.gmail.com>

On Mon Jun 16, 2025 at 11:03 PM CEST, Miguel Ojeda wrote:
> On Mon, Jun 16, 2025 at 9:35 PM Timur Tabi <ttabi@nvidia.com> wrote:
>> +#[macro_export]
>> +macro_rules! sfile {
>> +    () => {{
>> +        const {
>
> Could we avoid the double block?

We should be able to.

>> +            let start = match ::kernel::str::rfind_const(FILE, '/') {
>> +                Some(slash) => slash + 1,
>> +                None => 0,
>> +            };
>> +            let end = match ::kernel::str::rfind_const(FILE, '.') {
>> +                Some(dot) => dot,
>> +                None => FILE.len(),
>> +            };
>
> I wonder if we should just panic if they are not found. Maybe I am not
> creative enough, but do you have in mind a use case for either?

Sounds like a good idea.

> We already remove some cases anyway with the `assert!` below, so it is
> not usable in every case anyway.

>> +            let ptr: *const u8 = unsafe { base_ptr.add(start) };
>
> Benno: do you want here (and above) qualified calls too? (I would move
> everything to a `const fn` though -- please see below)

Yeah we should do that. Some of my macros don't do it (eg use `Ok` and
`Err` without qualifying them), but we should make all macros always
exclusively use absolute paths for everything. (`Ok` and `Err` are
probably never going to be different ones in scope, but you never know)

@Timur for this case, you can qualify the call like this:

    let ptr = unsafe { <*const ::core::primitives::u8>::add(base_ptr, start) };

Maybe the stdlib should have a re-export of `*const` and `*mut` in
primitives...

> I would just call both pointers `p`, by the way. No need to keep the
> base one around.
>
>> +            // that FILE is ASCII (via is_ascii() above).
>
> `FILE`, `is_ascii()`
>
>> +/// Returns the index of the last occurrence of `needle` in `haystack`, or None.
>
> [`None`]
>
>> +/// use kernel::print::rfind_const;
>
> Consider hiding this one too.
>
>> +/// let l = rfind_const("when will then be now?", 'l');
>> +/// assert!(l == Some(8));
>
> Please add an example for the `None` case too.
>
> Finally, should we put most of this logic in a `const fn` too for the
> usual benefits (docs and tests)? Then the macro would just be
> essentially a const `basename(file!())` call, possibly fallible, and
> you can even avoid having all those qualified calls :)

A function sounds like a good idea.

But I don't see the value in the macro being fallible. Then the macro is
less convenient to use. The function can be fallible, but then the macro
should use `Result::expect()`.

---
Cheers,
Benno

  reply	other threads:[~2025-06-17  7:29 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-16 19:34 [PATCH v4] rust: introduce sfile macro for succinct code tracing Timur Tabi
2025-06-16 21:03 ` Miguel Ojeda
2025-06-17  7:29   ` Benno Lossin [this message]
2025-06-17  7:54     ` Miguel Ojeda
2025-06-18 20:05       ` Timur Tabi
2025-06-21 16:59         ` Miguel Ojeda
2025-06-26 16:30           ` Timur Tabi
2025-06-26 20:57             ` Miguel Ojeda
2025-06-18 20:07   ` Timur Tabi
2025-06-21 16:47     ` Miguel Ojeda
2025-06-18 11:20 ` kernel test robot

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=DAOMQXNTLSGR.U0QOV8BU1I5V@kernel.org \
    --to=lossin@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=ttabi@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.