Skip to content

Commit 772e79c

Browse files
committed
Removed direct implementors of [Pin]Init
1 parent 8b7dadc commit 772e79c

File tree

1 file changed

+37
-56
lines changed

1 file changed

+37
-56
lines changed

rust/kernel/init.rs

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ macro_rules! pin_init {
336336
let init = move |slot: *mut $ty| -> ::core::result::Result<(), _> {
337337
init(slot).map(|__InitOk| ())
338338
};
339-
let init: $crate::init::PinInitClosure<_, $t $(<$($generics),*>)?, _> = unsafe { $crate::init::PinInitClosure::from_closure(init) };
339+
let init = unsafe { $crate::init::pin_init_from_closure::<$t $(<$($generics),*>)?, _>(init) };
340340
init
341341
}}
342342
}
@@ -509,7 +509,7 @@ macro_rules! init {
509509
let init = move |slot: *mut $t $(<$($generics),*>)?| -> ::core::result::Result<(), _> {
510510
init(slot).map(|__InitOk| ())
511511
};
512-
let init: $crate::init::InitClosure<_, $t $(<$($generics),*>)?, _> = unsafe { $crate::init::InitClosure::from_closure(init) };
512+
let init = unsafe { $crate::init::init_from_closure::<$t $(<$($generics),*>)?, _>(init) };
513513
init
514514
}}
515515
}
@@ -569,27 +569,39 @@ pub unsafe trait Init<T, E = Infallible>: PinInit<T, E> {
569569

570570
type Invariant<T> = PhantomData<fn(T) -> T>;
571571

572-
/// A closure initializer.
573-
pub struct InitClosure<F, T, E>(F, Invariant<(T, E)>);
572+
struct InitClosure<F, T, E>(F, Invariant<(T, E)>);
574573

575-
impl<T, E, F> InitClosure<F, T, E>
576-
where
577-
F: FnOnce(*mut T) -> Result<(), E>,
578-
{
579-
/// Creates a new Init from the given closure
580-
///
581-
/// # Safety
582-
///
583-
/// The closure
584-
/// - returns `Ok(())` iff it initialized every field of slot,
585-
/// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
586-
/// - slot can be deallocated without UB ocurring,
587-
/// - slot does not need to be dropped,
588-
/// - slot is not partially initialized.
589-
/// - slot may move after initialization
590-
pub const unsafe fn from_closure(f: F) -> Self {
591-
Self(f, PhantomData)
592-
}
574+
/// Creates a new Init from the given closure
575+
///
576+
/// # Safety
577+
///
578+
/// The closure
579+
/// - returns `Ok(())` iff it initialized every field of slot,
580+
/// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
581+
/// - slot can be deallocated without UB ocurring,
582+
/// - slot does not need to be dropped,
583+
/// - slot is not partially initialized.
584+
/// - slot may move after initialization
585+
pub const unsafe fn init_from_closure<T, E>(
586+
f: impl FnOnce(*mut T) -> Result<(), E>,
587+
) -> impl Init<T, E> {
588+
InitClosure(f, PhantomData)
589+
}
590+
/// Creates a new PinInit from the given closure
591+
///
592+
/// # Safety
593+
///
594+
/// The closure
595+
/// - returns `Ok(())` iff it initialized every field of slot,
596+
/// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
597+
/// - slot can be deallocated without UB ocurring,
598+
/// - slot does not need to be dropped,
599+
/// - slot is not partially initialized.
600+
/// - may assume that the slot does not move if `T: !Unpin`
601+
pub const unsafe fn pin_init_from_closure<T, E>(
602+
f: impl FnOnce(*mut T) -> Result<(), E>,
603+
) -> impl PinInit<T, E> {
604+
InitClosure(f, PhantomData)
593605
}
594606

595607
unsafe impl<T, F, E> PinInit<T, E> for InitClosure<F, T, E>
@@ -610,37 +622,6 @@ where
610622
}
611623
}
612624

613-
/// A closure initializer for pinned data.
614-
pub struct PinInitClosure<F, T, E>(F, Invariant<(T, E)>);
615-
616-
impl<T, E, F> PinInitClosure<F, T, E>
617-
where
618-
F: FnOnce(*mut T) -> Result<(), E>,
619-
{
620-
/// Creates a new Init from the given closure
621-
///
622-
/// # Safety
623-
///
624-
/// The closure
625-
/// - returns `Ok(())` iff it initialized every field of slot,
626-
/// - returns `Err(err)` iff it encountered an error and then cleaned slot, this means:
627-
/// - slot can be deallocated without UB ocurring,
628-
/// - slot does not need to be dropped,
629-
/// - slot is not partially initialized.
630-
pub const unsafe fn from_closure(f: F) -> Self {
631-
Self(f, PhantomData)
632-
}
633-
}
634-
635-
unsafe impl<T, F, E> PinInit<T, E> for PinInitClosure<F, T, E>
636-
where
637-
F: FnOnce(*mut T) -> Result<(), E>,
638-
{
639-
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
640-
(self.0)(slot)
641-
}
642-
}
643-
644625
/// Trait facilitating pinned destruction.
645626
///
646627
/// Use [`pinned_drop`] to implement this trait safely:
@@ -756,7 +737,7 @@ pub unsafe trait Zeroable {}
756737
/// Create a new zeroed T
757738
pub fn zeroed<T: Zeroable + Unpin>() -> impl Init<T> {
758739
unsafe {
759-
InitClosure::from_closure(|slot: *mut T| {
740+
init_from_closure(|slot: *mut T| {
760741
slot.write_bytes(0, 1);
761742
Ok(())
762743
})
@@ -765,13 +746,13 @@ pub fn zeroed<T: Zeroable + Unpin>() -> impl Init<T> {
765746

766747
/// An initializer that leaves the memory uninitialized.
767748
pub fn uninit<T>() -> impl Init<MaybeUninit<T>> {
768-
unsafe { InitClosure::from_closure(|_| Ok(())) }
749+
unsafe { init_from_closure(|_| Ok(())) }
769750
}
770751

771752
/// Convert a value into an initializer
772753
pub fn from_value<T>(value: T) -> impl Init<T> {
773754
unsafe {
774-
InitClosure::from_closure(move |slot: *mut T| {
755+
init_from_closure(move |slot: *mut T| {
775756
slot.write(value);
776757
Ok(())
777758
})

0 commit comments

Comments
 (0)