From: Michal Rostecki <vadorovsky@gmail.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
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>,
"Benno Lossin" <benno.lossin@proton.me>,
"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: Mon, 15 Jul 2024 18:12:05 +0200 [thread overview]
Message-ID: <52676577-372c-4a7f-aace-4cf100f93bfb@gmail.com> (raw)
In-Reply-To: <CANiq72=kchSt5XjAJRVgNWG-iNXbc2E64ojwsQYnB2pshULK1Q@mail.gmail.com>
On 14.07.24 19:30, Miguel Ojeda wrote:
> Hi Michal,
>
> Thanks for the patch! Some notes below...
>
> On Sun, Jul 14, 2024 at 6:02 PM Michal Rostecki <vadorovsky@gmail.com> wrote:
>>
>> `CStr` became a part of `core` library in Rust 1.75, therefore there is
>> no need to keep the custom implementation.
>
> It would depend on the differences, right? i.e. for a reader, is this
> meant to imply there is no meaningful difference in what you point out
> below?
>
Alright, I will remove the second part of the sentence.
>> - It does not implement `Display` (but implements `Debug`).
>
> One question that comes up when reading this is: are we losing
> `Display`'s output form?
>
Yes, we are losing the `Display` trait implementation by switching to
`core::ffi::CStr`.
I was thinking whether I should keep `kernel::str::CStr` as a wrapper,
just to keep the `Display` implementation. I could still do that if you
want. I'm also open for other solutions.
The reason why I decided to not do that and go ahead without `Display`
is that it was used only in rust/kernel/kunit.rs inside `kunit_assert`,
for formatting the file and path the error message. This diff:
@@ -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.
> Also, for clarity, please mention if there is a difference in the
> output of the `Debug` ones.
>
There isn't any difference, I will mention that.
>> - Otherwise, having such a method is not really desirable. `CStr` is
>> a reference type
>> or `str` are usually not supposed to be modified.
>
> The sentence seems to be cut, and it should probably try to explain
> better why it is undesirable, i.e. if it is needed for something like
> `DerefMut`, then it seems better to have a method.
>
Regarding `DerefMut` implementation for `CString` - we don't need it. Or
at least - removing it (after my CStr patch), does not break anything.
If that's fine for you, I'm going to remove it in v2 all together.
About why having `&mut CStr` is undesirable - I will try to find better
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.
>> - static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition));
>> + static CONDITION: &'static core::ffi::CStr = unsafe {
>> + core::ffi::CStr::from_bytes_with_nul_unchecked(
>> + core::concat!(stringify!($condition), "\0").as_bytes()
>> + )
>> + };
>
> This looks worse after the change and requires `unsafe`. Can we do
> something to improve it?
>
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?
[0] https://github.com/Rust-for-Linux/linux/issues/1075
>> + // SAFETY: Casting to CStr is safe because its internal representation
>> + // is a [u8] too.
>> + unsafe { &mut *(self.buf.as_mut_slice() as *mut [u8] as *mut CStr) }
>
> I see Björn commented on this already -- `CStr`'s layout is not
> guaranteed (and is a `[c_char]` instead).
>
> Also, the casting is not what is unsafe, so perhaps it may be clearer
> to reword the comment.
>
> In addition, please format comments as Markdown.
>
Good point, I will fix the comment.
>> -//! work <- new_work!("MyStruct::work"),
>> +//! work <- new_work!(c"MyStruct::work"),
>
> I agree as well that it may make sense to simplify the callers as much
> as possible, unless there is a need to have that flexibility.
>
I already replied to Björn - names passed to `new_work!`, `new_mutex!`
are immediatelly passed to FFI calls and are not used in the Rust code
internally, so I prefer to keep them as C strings rather than Rust strings.
> Cheers,
> Miguel
Cheers,
Michal
next prev parent reply other threads:[~2024-07-15 16:12 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 [this message]
2024-07-16 7:44 ` Miguel Ojeda
2024-07-17 15:22 ` Michal Rostecki
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=52676577-372c-4a7f-aace-4cf100f93bfb@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).