Skip to content

Commit 225e5a7

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 c3ac4f7 commit 225e5a7

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
@@ -1253,6 +1253,21 @@ pub unsafe trait PinnedDrop: __internal::HasPinData {
12531253
fn drop(self: Pin<&mut Self>, only_call_from_drop: __internal::OnlyCallFromDrop);
12541254
}
12551255

1256+
/// Create a new default T.
1257+
///
1258+
/// The returned initializer will use Default::default to initialize the `slot`.
1259+
#[inline]
1260+
pub fn default<T: Default>() -> impl Init<T> {
1261+
// SAFETY: Because `T: Default`, T cannot require pinning and
1262+
// we can just move the data into the slot.
1263+
unsafe {
1264+
init_from_closure(|slot: *mut T| {
1265+
*slot = Default::default();
1266+
Ok(())
1267+
})
1268+
}
1269+
}
1270+
12561271
/// Marker trait for types that can be initialized by writing just zeroes.
12571272
///
12581273
/// # Safety

0 commit comments

Comments
 (0)