Skip to content

Commit e94d0e8

Browse files
committed
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 e3012f8 commit e94d0e8

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

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

0 commit comments

Comments
 (0)