[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <6f4e1d68-f828-8990-4859-8ab24907fa46@proton.me>
Date: Sun, 25 Jun 2023 13:07:05 +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 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:
```
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