Skip to contentSkip to conversation

rust: init: Implement Zeroable::zeroed()

PATCH In review Asahi Lina 3 participants 3 messages Jul 14, 2023 – Jul 15, 2023

Subsystems (2)
lore
Share & tools
lore

Asahi Lina

author
#0 lore raw

PATCH 1/1 rust: init: Implement Zeroable::zeroed()

By analogy to Default::default(), this just returns the zeroed representation of the type directly. init::zeroed() is the version that returns an initializer.

---
 rust/kernel/init.rs | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
rust/kernel/init.rs +8 -1
index b4332a4ec1f4..c300ce39ac10 100644
@@ -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

reviewer Reviewed-by on v1 1/1
#1 lore raw

rust/kernel/init.rs pub unsafe trait PinnedDrop: __internal::HasPinData { · line 1364

Quoted 8 lines · Asahi Lina
rust/kernel/init.rs +8 -0
+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

Alice

#2 lore raw
In reply to Asahi Lina
Quoted 14 lines

By analogy to Default::default(), this just returns the zeroed representation of the type directly. init::zeroed() is the version that returns an initializer.

---
 rust/kernel/init.rs | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
rust/kernel/init.rs +0 -0
index b4332a4ec1f4..c300ce39ac10 100644

rust/kernel/init.rs pub unsafe trait PinnedDrop: __internal::HasPinData { · line 1357

Quoted 5 lines
rust/kernel/init.rs +1 -1
@@ -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.

rust/kernel/init.rs pub unsafe trait PinnedDrop: __internal::HasPinData { · line 1360

Quoted 3 lines
rust/kernel/init.rs +3 -0
+    /// 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.

rust/kernel/init.rs pub unsafe trait PinnedDrop: __internal::HasPinData { · line 1362

Quoted 2 lines
rust/kernel/init.rs +2 -0
+    fn zeroed() -> Self {
+        unsafe { core::mem::zeroed() }

Missing SAFETY comment.

--
Cheers,
Benno

Quoted 13 lines
rust/kernel/init.rs +2 -1
+    }
+}
 
 /// Create a new zeroed T.
 ///
 
---

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

Thank you,
~~ Lina