[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZKMQW4UGPQZ8yF39@boqun-archlinux>
Date: Mon, 3 Jul 2023 11:15:55 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Benno Lossin <benno.lossin@...ton.me>
Cc: Miguel Ojeda <ojeda@...nel.org>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Alex Gaynor <alex.gaynor@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
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 Sat, Jun 24, 2023 at 09:25:19AM +0000, Benno Lossin wrote:
[...]
(this is `init_slot`)
> @@ -1064,7 +1152,7 @@ macro_rules! __init_internal {
> @data($data:ident),
> @slot($slot:ident),
> @guards($($guards:ident,)*),
> - @munch_fields($(,)?),
> + @munch_fields($(..Zeroable::zeroed())? $(,)?),
since you append an unconditional comma ',' to init_slot and
make_initializer when "calling" them in with_update_parsed, shouldn't
this be:
+ @munch_fields($(..Zeroable::zeroed(),)? $(,)?),
, and..
> ) => {
> // Endpoint of munching, no fields are left. If execution reaches this point, all fields
> // have been initialized. Therefore we can now dismiss the guards by forgetting them.
> @@ -1157,6 +1245,30 @@ macro_rules! __init_internal {
> @munch_fields($($rest)*),
> );
> };
> + (make_initializer:
> + @slot($slot:ident),
> + @type_name($t:ident),
> + @munch_fields(..Zeroable::zeroed() $(,)?),
this should be:
+ @munch_fields(..Zeroable::zeroed() , $(,)?),
Otherwise the example before `pin_init!()` wouldn't compile:
/// pin_init!(Buf {
/// buf: [1; 64],
/// ..Zeroable::zeroed(),
/// });
Regards,
Boqun
> + @acc($($acc:tt)*),
> + ) => {
> + // Endpoint, nothing more to munch, create the initializer. Since the users specified
> + // `..Zeroable::zeroed()`, the slot will already have been zeroed and all field that have
> + // not been overwritten are thus zero and initialized. We still check that all fields are
> + // actually accessible by using the struct update syntax ourselves.
> + // Since we are in the `if false` branch, this will never get executed. We abuse `slot` to
> + // get the correct type inference here:
> + unsafe {
> + let mut zeroed = ::core::mem::zeroed();
> + // We have to use type inference her to make zeroed have the correct type. This does
> + // not get executed, so it has no effect.
> + ::core::ptr::write($slot, zeroed);
> + zeroed = ::core::mem::zeroed();
> + ::core::ptr::write($slot, $t {
> + $($acc)*
> + ..zeroed
> + });
> + }
> + };
> (make_initializer:
> @slot($slot:ident),
> @type_name($t:ident),
> --
> 2.41.0
>
>
Powered by blists - more mailing lists