From: Michal Rostecki <vadorovsky@gmail.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>,
Benno Lossin <benno.lossin@proton.me>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Brendan Higgins" <brendan.higgins@linux.dev>,
"David Gow" <davidgow@google.com>, "Rae Moar" <rmoar@google.com>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nick Desaulniers" <ndesaulniers@google.com>,
"Bill Wendling" <morbo@google.com>,
"Justin Stitt" <justinstitt@google.com>,
"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
"Finn Behrens" <me@kloenk.dev>,
"Manmohan Shukla" <manmshuk@gmail.com>,
"Valentin Obst" <kernel@valentinobst.de>,
"Laine Taffin Altman" <alexanderaltman@me.com>,
"Danilo Krummrich" <dakr@redhat.com>,
"Yutaro Ohno" <yutaro.ono.418@gmail.com>,
"Tiago Lam" <tiagolam@gmail.com>,
"Charalampos Mitrodimas" <charmitro@posteo.net>,
"Ben Gooding" <ben.gooding.dev@gmail.com>,
"Roland Xu" <mu001999@outlook.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
netdev@vger.kernel.org, llvm@lists.linux.dev
Subject: Re: [PATCH] rust: str: Use `core::CStr`, remove the custom `CStr` implementation
Date: Wed, 17 Jul 2024 17:22:39 +0200 [thread overview]
Message-ID: <604c64b6-2eff-42a0-91fe-211f7763ba6a@gmail.com> (raw)
In-Reply-To: <CANiq72nbbtNp4vGGHkXVSgSW+WU=5Z9uGRO_LLg7+ezTqrZ_tQ@mail.gmail.com>
On 16.07.24 09:44, Miguel Ojeda wrote:
> On Mon, Jul 15, 2024 at 6:12 PM Michal Rostecki <vadorovsky@gmail.com> wrote:
>>
>> @@ -71,11 +75,11 @@ macro_rules! kunit_assert {
>> //
>> // This mimics KUnit's failed assertion format.
>> $crate::kunit::err(format_args!(
>> - " # {}: ASSERTION FAILED at {FILE}:{LINE}\n",
>> + " # {:?}: ASSERTION FAILED at {FILE:?}:{LINE:?}\n",
>> $name
>> ));
>> $crate::kunit::err(format_args!(
>> - " Expected {CONDITION} to be true, but is false\n"
>> + " Expected {CONDITION:?} to be true, but is false\n"
>> ));
>>
>> The only practical difference in switching from `Display` to `Debug`
>> here is that the fallback kunit error messages are going to include
>> quotation marks around conditions, files and lines.
>
> That is a fairly important difference -- the messages are intended to
> match the C KUnit ones.
>
> Especially the file:line notation -- I don't think a user would expect
> to have quotes there (regardless of KUnit).
>
> In general, even if we didn't need it right now, I think it is
> something we will need sooner or later.
>
Alright, I will go with Trevor's suggestion and provide a `display()`
method via an extension trait.
>> wording. My general point is that I've never seen `&mut str` being
>> exposed in any core/std API to the external user, mutation usually
>> implies usage of an owned String.
>
> Not sure what you mean by exposed, but even if `&mut str`'s methods do
> not count (used via `String`), there is also
> `from_utf8_unchecked_mut()` that returns one, which is essentially the
> same idea as what we had here.
>
> So I am not sure about the "The rule of Rust std" part in the new
> commit messages. And, to be clear, while the Rust standard library is
> a good reference to look into, sometimes we want/need to do things
> differently anyway (which is not really the case here given
> `from_utf8_unchecked_mut()`, I would say).
>
> In this case, regardless of the standard library, personally I would
> have preferred to have a non-public function, but still have it (and
> properly documented), rather than open code the `unsafe` block with
> the casts.
>
Fair enough. I will provide `from_utf8_unchecked_mut()` as a part of
`CStrExt` in the next version.
>> I think the best solution would be leaving `c_str` macro for that. The
>> reason why I removed it is that the GitHub issue[0] mentions its
>> removal. But for that case, I think it makes sense to leave it. What do
>> you think?
>
> Perhaps the issue was only taking into account the C string literal
> case? Benno may know more.
>
> Generally speaking, replacing a clean line with a bigger `unsafe`
> block is something to be avoided.
>
> Maybe a `c_stringify!` is what we need :)
>
`stringify!` is not the only case where I ended up using `c_str!`. After
addressing Björn's suggestion about taking Rust strings as arguments in
`new_mutex!`, `new_condvar!` etc., `optional_name!` is also using
`c_str!` in the following way:
macro_rules! optional_name {
() => {
$crate::c_str!(::core::concat!(::core::file!(), ":",
::core::line!()))
};
($name:literal) => {
$crate::c_str!($name)
};
}
So I think that leaving `c_str!` still makes sense, unless you have
other suggestions, which are still easily applicable there. :)
> Cheers,
> Miguel
Cheers,
Michal
prev parent reply other threads:[~2024-07-17 15:22 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-14 16:02 [PATCH] rust: str: Use `core::CStr`, remove the custom `CStr` implementation Michal Rostecki
2024-07-14 17:01 ` Björn Roy Baron
2024-07-15 15:46 ` Michal Rostecki
2024-07-15 15:56 ` Björn Roy Baron
2024-07-15 16:15 ` Michal Rostecki
2024-07-14 17:30 ` Miguel Ojeda
2024-07-15 16:12 ` Michal Rostecki
2024-07-16 7:44 ` Miguel Ojeda
2024-07-17 15:22 ` Michal Rostecki [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=604c64b6-2eff-42a0-91fe-211f7763ba6a@gmail.com \
--to=vadorovsky@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=alexanderaltman@me.com \
--cc=aliceryhl@google.com \
--cc=ben.gooding.dev@gmail.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=brendan.higgins@linux.dev \
--cc=charmitro@posteo.net \
--cc=dakr@redhat.com \
--cc=davidgow@google.com \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=justinstitt@google.com \
--cc=kernel@valentinobst.de \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=manmshuk@gmail.com \
--cc=me@kloenk.dev \
--cc=miguel.ojeda.sandonis@gmail.com \
--cc=morbo@google.com \
--cc=mu001999@outlook.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rmoar@google.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tiagolam@gmail.com \
--cc=tmgross@umich.edu \
--cc=wedsonaf@gmail.com \
--cc=yakoyoku@gmail.com \
--cc=yutaro.ono.418@gmail.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).