[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <0f99bc49-5280-c300-719f-86c138f48eaf@proton.me>
Date: Sun, 25 Jun 2023 16:46:19 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Björn Roy Baron <bjorn3_gh@...tonmail.com>
Cc: Miguel Ojeda <ojeda@...nel.org>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Alice Ryhl <aliceryhl@...gle.com>,
Andreas Hindborg <nmi@...aspace.dk>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
patches@...ts.linux.dev, Asahi Lina <lina@...hilina.net>
Subject: Re: [PATCH 5/7] rust: init: add `..Zeroable::zeroed()` syntax for zeroing all missing fields
On 6/25/23 16:17, Björn Roy Baron wrote:
> On Sunday, June 25th, 2023 at 15:07, Benno Lossin <benno.lossin@...ton.me> wrote:
>> On 25.06.23 14:56, Björn Roy Baron wrote:
>>> On Saturday, June 24th, 2023 at 23:14, Benno Lossin <benno.lossin@...ton.me> wrote:
>>
>>>>>> + // Ensure that the struct is indeed `Zeroable`.
>>>>>> + is_zeroable(slot);
>>>>>> + // SAFETY: The type implements `Zeroable` by the check above.
>>>>>> + unsafe { ::core::ptr::write_bytes(slot, 0, 1) };
>>>>>> + $init_zeroed // this will be `()` if set.
>>>>>
>>>>> How does this work? Shouldn't there be a ; after $init_zeroed to consume the () value?
>>>>
>>>> It is the last expression of a block and since it is `()` it is ok
>>>> (adding a ; would also be ok, but it is not necessary).
>>>
>>> I'm surprised it is considered the last expression of a block. Unlike with {} using $()? will still
>>> allow variables defined inside this as if they were outside of it. Also I can't reproduce this
>>> behavior with:
>>>
>>> macro_rules! foo {
>>> ($($a:expr)?) => {
>>> $($a)?
>>> bar();
>>> }
>>> }
>>>
>>> fn main() {
>>> foo!(());
>>> }
>>>
>>> Is there something I'm missing?
>>>
>>> Cheers,
>>> Björn
>>
>> Not sure what you mean with "allow variables defined inside this
>> as if they were outside of it". But note that in the macro `$init_zeroed`
>> is the last expression of a block. Here is a small example:
>
> $(let $this = unsafe { ::core::ptr::NonNull::new_unchecked(slot) };)? comes after
> this code in the same block that contains struct __InitOk;. And after that another
> $crate::__init_internal!() invocation. That is why I don't get that this is allowed
> at all.
>
Oh I see the issue now, I thought I wrote
```
$({
fn assert_zeroable<T: Zeroable>(ptr: *mut T) {}
// Ensure that the struct is indeed `Zeroable`.
assert_zeroable(slot);
// SAFETY: The type implements `Zeroable` by the check above.
unsafe { ::core::ptr::write_bytes(slot, 0, 1) };
$init_zeroed // this will be `()` if set.
})?
```
But I forgot the inner `{}`. Good catch!
--
Cheers,
Benno
>>
>> ```
>> macro_rules! foo {
>> ($($a:expr)?) => {{
>> $(
>> bar();
>> $a
>> )?
>> }};
>> }
>>
>> fn bar() {}
>>
>> fn main() {
>> foo!(());
>> foo!();
>> }
>> ```
>>
>> it expands to this:
>> ```
>> fn main() {
>> {
>> bar();
>> ()
>> };
>> {};
>> }
>> ```
>>
>> --
>> Cheers,
>> Benno
>>
Powered by blists - more mailing lists