Skip to content

Commit ab4eafc

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 63ac0a8 commit ab4eafc

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

rust/pin-init/src/lib.rs

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

1464+
/// Create a new default T.
1465+
///
1466+
/// The returned initializer will use Default::default to initialize the `slot`.
1467+
#[inline]
1468+
pub fn default<T: Default>() -> impl Init<T> {
1469+
// SAFETY: Because `T: Default`, T cannot require pinning and
1470+
// we can just move the data into the slot.
1471+
unsafe {
1472+
init_from_closure(|slot: *mut T| {
1473+
*slot = Default::default();
1474+
Ok(())
1475+
})
1476+
}
1477+
}
1478+
14641479
/// Marker trait for types that can be initialized by writing just zeroes.
14651480
///
14661481
/// # Safety

0 commit comments

Comments
 (0)