Skip to content

Commit b6f1efe

Browse files
hoshinolinajannau
authored andcommitted
rust: init: Add default() utility function
Initializer for types with Default::default() implementations in init context. This, by nature, only works for types which are not pinned. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 0f88741 commit b6f1efe

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

rust/kernel/init.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,21 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
13581358
fn drop(self: Pin<&mut Self>, only_call_from_drop: __internal::OnlyCallFromDrop);
13591359
}
13601360

1361+
/// Create a new default T.
1362+
///
1363+
/// The returned initializer will use Default::default to initialize the `slot`.
1364+
#[inline]
1365+
pub fn default<T: Default>() -> impl Init<T> {
1366+
// SAFETY: Because `T: Default`, T cannot require pinning and
1367+
// we can just move the data into the slot.
1368+
unsafe {
1369+
init_from_closure(|slot: *mut T| {
1370+
*slot = Default::default();
1371+
Ok(())
1372+
})
1373+
}
1374+
}
1375+
13611376
/// Marker trait for types that can be initialized by writing just zeroes.
13621377
///
13631378
/// # Safety

0 commit comments

Comments
 (0)