Skip to content

Commit 3537543

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 e63c184 commit 3537543

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

1488+
/// Create a new default T.
1489+
///
1490+
/// The returned initializer will use Default::default to initialize the `slot`.
1491+
#[inline]
1492+
pub fn default<T: Default>() -> impl Init<T> {
1493+
// SAFETY: Because `T: Default`, T cannot require pinning and
1494+
// we can just move the data into the slot.
1495+
unsafe {
1496+
init_from_closure(|slot: *mut T| {
1497+
*slot = Default::default();
1498+
Ok(())
1499+
})
1500+
}
1501+
}
1502+
14881503
/// Marker trait for types that can be initialized by writing just zeroes.
14891504
///
14901505
/// # Safety

0 commit comments

Comments
 (0)