rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: init: Implement Zeroable::zeroed()
@ 2023-07-14  9:17 Asahi Lina
  2023-07-14  9:44 ` Alice Ryhl
  2023-07-15  9:58 ` Benno Lossin
  0 siblings, 2 replies; 3+ messages in thread
From: Asahi Lina @ 2023-07-14  9:17 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin
  Cc: rust-for-linux, linux-kernel, Asahi Lina

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 {
+    /// 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.
 ///

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

Thank you,
~~ Lina


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: init: Implement Zeroable::zeroed()
  2023-07-14  9:17 [PATCH] rust: init: Implement Zeroable::zeroed() Asahi Lina
@ 2023-07-14  9:44 ` Alice Ryhl
  2023-07-15  9:58 ` Benno Lossin
  1 sibling, 0 replies; 3+ messages in thread
From: Alice Ryhl @ 2023-07-14  9:44 UTC (permalink / raw)
  To: lina
  Cc: alex.gaynor, benno.lossin, bjorn3_gh, boqun.feng, gary,
	linux-kernel, ojeda, rust-for-linux, wedsonaf

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: init: Implement Zeroable::zeroed()
  2023-07-14  9:17 [PATCH] rust: init: Implement Zeroable::zeroed() Asahi Lina
  2023-07-14  9:44 ` Alice Ryhl
@ 2023-07-15  9:58 ` Benno Lossin
  1 sibling, 0 replies; 3+ messages in thread
From: Benno Lossin @ 2023-07-15  9:58 UTC (permalink / raw)
  To: Asahi Lina
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, rust-for-linux, linux-kernel

> 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
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-07-15  9:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-14  9:17 [PATCH] rust: init: Implement Zeroable::zeroed() Asahi Lina
2023-07-14  9:44 ` Alice Ryhl
2023-07-15  9:58 ` Benno Lossin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).