Skip to content

Commit df925b2

Browse files
committed
improve safety documentation for impl<T> [Pin]Init<T> for T
The inner SAFETY comments were missing since commit 5cfe7be ("rust: enable `clippy::undocumented_unsafe_blocks` lint"). Also rework the implementation of `__pinned_init` to better justify the SAFETY comment. Signed-off-by: Benno Lossin <[email protected]>
1 parent f2451d9 commit df925b2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,20 +1390,22 @@ where
13901390
unsafe { pin_init_from_closure(init) }
13911391
}
13921392

1393-
// SAFETY: Every type can be initialized by-value.
1393+
// SAFETY: the `__init` function always returns `Ok(())` and initializes every field of `slot`.
13941394
unsafe impl<T, E> Init<T, E> for T {
13951395
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
1396-
// SAFETY: TODO.
1396+
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
13971397
unsafe { slot.write(self) };
13981398
Ok(())
13991399
}
14001400
}
14011401

1402-
// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
1402+
// SAFETY: the `__pinned_init` function always returns `Ok(())` and initializes every field of
1403+
// `slot`. Additionally, all pinning invariants of `T` are upheld.
14031404
unsafe impl<T, E> PinInit<T, E> for T {
14041405
unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
1405-
// SAFETY: TODO.
1406-
unsafe { self.__init(slot) }
1406+
// SAFETY: `slot` is valid for writes by the safety requirements of this function.
1407+
unsafe { slot.write(self) };
1408+
Ok(())
14071409
}
14081410
}
14091411

0 commit comments

Comments
 (0)