rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Timur Tabi" <ttabi@nvidia.com>,
	"ojeda@kernel.org" <ojeda@kernel.org>,
	"aliceryhl@google.com" <aliceryhl@google.com>,
	"dakr@kernel.org" <dakr@kernel.org>,
	"John Hubbard" <jhubbard@nvidia.com>,
	"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v3] rust: introduce sfile macro for succinct code tracing
Date: Tue, 10 Jun 2025 10:31:01 +0200	[thread overview]
Message-ID: <DAIPOI4SKL6U.RY7HCY76AYOR@kernel.org> (raw)
In-Reply-To: <35ca9fa086626f11ea65eb25acaf22fadbbe8710.camel@nvidia.com>

On Tue Jun 10, 2025 at 5:40 AM CEST, Timur Tabi wrote:
> On Mon, 2025-06-09 at 19:33 -0700, John Hubbard wrote:
>> > +/// Returns the index of the last occurrence of `needle` in `haystack`, or zero.
>> > +///
>> > +/// Identical to [`str::rfind()`], but unlike that function, this one is const.
>> > +/// That is, the compiler can inline it when called with a string literal.
>> > +#[inline]
>> > +pub const fn rfind_const(haystack: &str, needle: char) -> Option<usize> {
>> > +    let bytes = haystack.as_bytes();
>> 
>> Silly nit: "bytes" is just adding an unnecessary variable that also has
>> a non-informative name. Just delete it...
>> 
>> > +    let mut i = haystack.len();
>> > +    while i > 0 {
>> > +        i -= 1;
>> > +        if bytes[i] == needle as u8 {
>> 
>> ...and write the above line accordingly:
>> 
>> 	if haystack.as_bytes()[i] == needle as u8 {
>> 
>> 
>> Yes?
>
> I find my version to be easier to read.  I like using temporary local variables to keep my code
> cleaner.
>
> If I post a v4, I can try to come up with a better name.

You can just reuse haystack:

    let haystack = haystack.as_bytes();

---
Cheers,
Benno

  reply	other threads:[~2025-06-10  8:31 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-09 22:36 [PATCH v3] rust: introduce sfile macro for succinct code tracing Timur Tabi
2025-06-10  2:33 ` John Hubbard
2025-06-10  3:40   ` Timur Tabi
2025-06-10  8:31     ` Benno Lossin [this message]
2025-06-10  8:45 ` Benno Lossin
2025-06-13 17:03   ` Timur Tabi
2025-06-13 17:57     ` Miguel Ojeda
2025-06-13 19:14       ` Timur Tabi
2025-06-13 20:08         ` Timur Tabi
2025-06-13 19:32   ` Timur Tabi
2025-06-13 20:06     ` Timur Tabi
2025-06-14 17:08       ` Benno Lossin
2025-06-16 15:37         ` Timur Tabi
2025-06-16 18:18           ` Miguel Ojeda
2025-06-13 22:46   ` Timur Tabi
2025-06-14 18:01     ` Benno Lossin
2025-06-10  8:47 ` Miguel Ojeda
2025-06-10 19:18   ` Timur Tabi
2025-06-10 19:29     ` Miguel Ojeda

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=DAIPOI4SKL6U.RY7HCY76AYOR@kernel.org \
    --to=lossin@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=jhubbard@nvidia.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 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).