rust: init: Implement Zeroable::zeroed()

Message ID 20230714-zeroed-v1-1-494d6820d61b@asahilina.net
State New
Headers
Series rust: init: Implement Zeroable::zeroed() |

Commit Message

Asahi Lina July 14, 2023, 9:17 a.m. UTC
  By analogy to Default::default(), this just returns the zeroed
representation of the type directly. init::zeroed() is the version that
returns an initializer.

Signed-off-by: Asahi Lina <lina@asahilina.net>
---
 rust/kernel/init.rs | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)


---
base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
change-id: 20230714-zeroed-dd05bc737f85

Thank you,
~~ Lina
  

Comments

Alice Ryhl July 14, 2023, 9:44 a.m. UTC | #1
Asahi Lina <lina@asahilina.net> 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@google.com>

Alice
  
Benno Lossin July 15, 2023, 9:58 a.m. UTC | #2
> By analogy to Default::default(), this just returns the zeroed
> representation of the type directly. init::zeroed() is the version that
> returns an initializer.
> 
> Signed-off-by: Asahi Lina <lina@asahilina.net>
> ---
>  rust/kernel/init.rs | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
> index b4332a4ec1f4..c300ce39ac10 100644
> --- a/rust/kernel/init.rs
> +++ b/rust/kernel/init.rs
> @@ -1354,7 +1354,14 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
>  /// ```rust,ignore
>  /// let val: Self = unsafe { core::mem::zeroed() };
>  /// ```
> -pub unsafe trait Zeroable {}
> +pub unsafe trait Zeroable: core::marker::Sized {

Note that `Sized` is in the prelude so you do not need the full path
`core::marker::`. Also same concern as Alice.

> +    /// Create a new zeroed T.
> +    ///
> +    /// Directly returns a zeroed T, analogous to Default::default().

Please add a link to `init::zeroed()` and explain that that is the
initializer version of this function.

> +    fn zeroed() -> Self {
> +        unsafe { core::mem::zeroed() }

Missing `SAFETY` comment.

--
Cheers,
Benno

> +    }
> +}
> 
>  /// Create a new zeroed T.
>  ///
> 
> ---
> base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
> change-id: 20230714-zeroed-dd05bc737f85
> 
> Thank you,
> ~~ Lina
>
  

Patch

diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
index b4332a4ec1f4..c300ce39ac10 100644
--- a/rust/kernel/init.rs
+++ b/rust/kernel/init.rs
@@ -1354,7 +1354,14 @@  pub unsafe trait PinnedDrop: __internal::HasPinData {
 /// ```rust,ignore
 /// let val: Self = unsafe { core::mem::zeroed() };
 /// ```
-pub unsafe trait Zeroable {}
+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() }
+    }
+}
 
 /// Create a new zeroed T.
 ///