Skip to content

Commit 363b248

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 f880ea1 commit 363b248

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

1343+
/// Create a new default T.
1344+
///
1345+
/// The returned initializer will use Default::default to initialize the `slot`.
1346+
#[inline]
1347+
pub fn default<T: Default>() -> impl Init<T> {
1348+
// SAFETY: Because `T: Default`, T cannot require pinning and
1349+
// we can just move the data into the slot.
1350+
unsafe {
1351+
init_from_closure(|slot: *mut T| {
1352+
*slot = Default::default();
1353+
Ok(())
1354+
})
1355+
}
1356+
}
1357+
13431358
/// Marker trait for types that can be initialized by writing just zeroes.
13441359
///
13451360
/// # Safety

0 commit comments

Comments
 (0)