Re: [PATCH] rust: init: Implement Zeroable::zeroed()

From: Alice Ryhl
Date: Fri Jul 14 2023 - 05:44:14 EST


Asahi Lina <lina@xxxxxxxxxxxxx> writes:
> +pub unsafe trait Zeroable: core::marker::Sized {
> + /// Create a new zeroed T.
> + ///
> + /// Directly returns a zeroed T, analogous to Default::default().
> + fn zeroed() -> Self {
> + unsafe { core::mem::zeroed() }
> + }
> +}

I don't think this trait needs to require `Sized`. How about the
following alternate implementation?

pub unsafe trait Zeroable {
/// Create a new zeroed T.
///
/// Directly returns a zeroed T, analogous to Default::default().
fn zeroed() -> Self
where
Self: core::marker::Sized,
{
unsafe { core::mem::zeroed() }
}
}

Then types like [T] can also implement Zeroable when T does.

If you make the above change, then you may add my
Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>

Alice