1 // SPDX-License-Identifier: Apache-2.0 OR MIT
3 //! This module contains API-internal items for pin-init.
5 //! These items must not be used outside of
7 //! - `macros/pin_data.rs`
8 //! - `macros/pinned_drop.rs`
12 /// See the [nomicon] for what subtyping is. See also [this table].
14 /// [nomicon]: https://doc.rust-lang.org/nomicon/subtyping.html
15 /// [this table]: https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns
16 pub(super) type Invariant<T> = PhantomData<fn(*mut T) -> *mut T>;
18 /// Module-internal type implementing `PinInit` and `Init`.
20 /// It is unsafe to create this type, since the closure needs to fulfill the same safety
21 /// requirement as the `__pinned_init`/`__init` functions.
22 pub(crate) struct InitClosure<F, T: ?Sized, E>(pub(crate) F, pub(crate) Invariant<(E, T)>);
24 // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
25 // `__init` invariants.
26 unsafe impl<T: ?Sized, F, E> Init<T, E> for InitClosure<F, T, E>
28 F: FnOnce(*mut T) -> Result<(), E>,
31 unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
36 // SAFETY: While constructing the `InitClosure`, the user promised that it upholds the
37 // `__pinned_init` invariants.
38 unsafe impl<T: ?Sized, F, E> PinInit<T, E> for InitClosure<F, T, E>
40 F: FnOnce(*mut T) -> Result<(), E>,
43 unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
48 /// This trait is only implemented via the `#[pin_data]` proc-macro. It is used to facilitate
49 /// the pin projections within the initializers.
53 /// Only the `init` module is allowed to use this trait.
54 pub unsafe trait HasPinData {
55 type PinData: PinData;
57 #[expect(clippy::missing_safety_doc)]
58 unsafe fn __pin_data() -> Self::PinData;
61 /// Marker trait for pinning data of structs.
65 /// Only the `init` module is allowed to use this trait.
66 pub unsafe trait PinData: Copy {
67 type Datee: ?Sized + HasPinData;
69 /// Type inference helper function.
70 fn make_closure<F, O, E>(self, f: F) -> F
72 F: FnOnce(*mut Self::Datee) -> Result<O, E>,
78 /// This trait is automatically implemented for every type. It aims to provide the same type
79 /// inference help as `HasPinData`.
83 /// Only the `init` module is allowed to use this trait.
84 pub unsafe trait HasInitData {
85 type InitData: InitData;
87 #[expect(clippy::missing_safety_doc)]
88 unsafe fn __init_data() -> Self::InitData;
91 /// Same function as `PinData`, but for arbitrary data.
95 /// Only the `init` module is allowed to use this trait.
96 pub unsafe trait InitData: Copy {
97 type Datee: ?Sized + HasInitData;
99 /// Type inference helper function.
100 fn make_closure<F, O, E>(self, f: F) -> F
102 F: FnOnce(*mut Self::Datee) -> Result<O, E>,
108 pub struct AllData<T: ?Sized>(PhantomData<fn(KBox<T>) -> KBox<T>>);
110 impl<T: ?Sized> Clone for AllData<T> {
111 fn clone(&self) -> Self {
116 impl<T: ?Sized> Copy for AllData<T> {}
119 unsafe impl<T: ?Sized> InitData for AllData<T> {
124 unsafe impl<T: ?Sized> HasInitData for T {
125 type InitData = AllData<T>;
127 unsafe fn __init_data() -> Self::InitData {
132 /// Stack initializer helper type. Use [`stack_pin_init`] instead of this primitive.
136 /// If `self.is_init` is true, then `self.value` is initialized.
138 /// [`stack_pin_init`]: kernel::stack_pin_init
139 pub struct StackInit<T> {
140 value: MaybeUninit<T>,
144 impl<T> Drop for StackInit<T> {
148 // SAFETY: As we are being dropped, we only call this once. And since `self.is_init` is
149 // true, `self.value` is initialized.
150 unsafe { self.value.assume_init_drop() };
155 impl<T> StackInit<T> {
156 /// Creates a new [`StackInit<T>`] that is uninitialized. Use [`stack_pin_init`] instead of this
159 /// [`stack_pin_init`]: kernel::stack_pin_init
161 pub fn uninit() -> Self {
163 value: MaybeUninit::uninit(),
168 /// Initializes the contents and returns the result.
170 pub fn init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {
171 // SAFETY: We never move out of `this`.
172 let this = unsafe { Pin::into_inner_unchecked(self) };
173 // The value is currently initialized, so it needs to be dropped before we can reuse
174 // the memory (this is a safety guarantee of `Pin`).
176 this.is_init = false;
177 // SAFETY: `this.is_init` was true and therefore `this.value` is initialized.
178 unsafe { this.value.assume_init_drop() };
180 // SAFETY: The memory slot is valid and this type ensures that it will stay pinned.
181 unsafe { init.__pinned_init(this.value.as_mut_ptr())? };
182 // INVARIANT: `this.value` is initialized above.
184 // SAFETY: The slot is now pinned, since we will never give access to `&mut T`.
185 Ok(unsafe { Pin::new_unchecked(this.value.assume_init_mut()) })
189 /// When a value of this type is dropped, it drops a `T`.
191 /// Can be forgotten to prevent the drop.
192 pub struct DropGuard<T: ?Sized> {
196 impl<T: ?Sized> DropGuard<T> {
197 /// Creates a new [`DropGuard<T>`]. It will [`ptr::drop_in_place`] `ptr` when it gets dropped.
201 /// `ptr` must be a valid pointer.
203 /// It is the callers responsibility that `self` will only get dropped if the pointee of `ptr`:
204 /// - has not been dropped,
205 /// - is not accessible by any other means,
206 /// - will not be dropped by any other means.
208 pub unsafe fn new(ptr: *mut T) -> Self {
213 impl<T: ?Sized> Drop for DropGuard<T> {
216 // SAFETY: A `DropGuard` can only be constructed using the unsafe `new` function
217 // ensuring that this operation is safe.
218 unsafe { ptr::drop_in_place(self.ptr) }
222 /// Token used by `PinnedDrop` to prevent calling the function without creating this unsafely
223 /// created struct. This is needed, because the `drop` function is safe, but should not be called
225 pub struct OnlyCallFromDrop(());
227 impl OnlyCallFromDrop {
230 /// This function should only be called from the [`Drop::drop`] function and only be used to
231 /// delegate the destruction to the pinned destructor [`PinnedDrop::drop`] of the same type.
232 pub unsafe fn new() -> Self {
237 /// Initializer that always fails.
239 /// Used by [`assert_pinned!`].
241 /// [`assert_pinned!`]: crate::assert_pinned
242 pub struct AlwaysFail<T: ?Sized> {
246 impl<T: ?Sized> AlwaysFail<T> {
247 /// Creates a new initializer that always fails.
248 pub fn new() -> Self {
249 Self { _t: PhantomData }
253 impl<T: ?Sized> Default for AlwaysFail<T> {
254 fn default() -> Self {
259 // SAFETY: `__pinned_init` always fails, which is always okay.
260 unsafe impl<T: ?Sized> PinInit<T, ()> for AlwaysFail<T> {
261 unsafe fn __pinned_init(self, _slot: *mut T) -> Result<(), ()> {