Skip to content

Commit b644bd3

Browse files
committed
refactor: add unfinished safety comments for initialization and pinning macros
1 parent f2451d9 commit b644bd3

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/__internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ impl<T: ?Sized> Clone for AllData<T> {
116116

117117
impl<T: ?Sized> Copy for AllData<T> {}
118118

119-
// SAFETY: TODO.
119+
// SAFETY: `AllData<T>` is a zero-sized type that carries no runtime data, only type information.
120120
unsafe impl<T: ?Sized> InitData for AllData<T> {
121121
type Datee = T;
122122
}
123123

124-
// SAFETY: TODO.
124+
// SAFETY: `__init_data` returns only phantom data, it performs no memory operations.
125125
unsafe impl<T: ?Sized> HasInitData for T {
126126
type InitData = AllData<T>;
127127

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ macro_rules! stack_try_pin_init {
763763
///
764764
/// let init = pin_init!(&this in Buf {
765765
/// buf: [0; 64],
766-
/// // SAFETY: TODO.
766+
/// // SAFETY: The closure properly initializes to the target memory location,
767+
/// // and error handling ensures memory is left ina valid state if initization fails.
767768
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
768769
/// pin: PhantomPinned,
769770
/// });
@@ -1393,7 +1394,7 @@ where
13931394
// SAFETY: Every type can be initialized by-value.
13941395
unsafe impl<T, E> Init<T, E> for T {
13951396
unsafe fn __init(self, slot: *mut T) -> Result<(), E> {
1396-
// SAFETY: TODO.
1397+
// SAFETY: `slot` points to a valid, uninitialized memory of the correct size and alignment for type `T`.
13971398
unsafe { slot.write(self) };
13981399
Ok(())
13991400
}
@@ -1402,7 +1403,7 @@ unsafe impl<T, E> Init<T, E> for T {
14021403
// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `__init`.
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+
// SAFETY: `self` is a valid value of type `T`, and all requirements for `__init` are met.
14061407
unsafe { self.__init(slot) }
14071408
}
14081409
}

src/macros.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ macro_rules! __pinned_drop {
518518
}
519519
),
520520
) => {
521-
// SAFETY: TODO.
521+
// SAFETY: The `OnlyCallFromDrop` token ensures the drop function can only be called
522+
// when the struct is properly pinned.
522523
unsafe $($impl_sig)* {
523524
// Inherit all attributes and the type/ident tokens for the signature.
524525
$(#[$($attr)*])*
@@ -878,7 +879,8 @@ macro_rules! __pin_data {
878879
}
879880
}
880881

881-
// SAFETY: TODO.
882+
// SAFETY: `__ThePinData` correctly represents the pinning structure of the original type
883+
// and the associated type `Datee` correctly refers to the original struct type.
882884
unsafe impl<$($impl_generics)*>
883885
$crate::__internal::PinData for __ThePinData<$($ty_generics)*>
884886
where $($whr)*
@@ -1005,7 +1007,7 @@ macro_rules! __pin_data {
10051007
slot: *mut $p_type,
10061008
init: impl $crate::PinInit<$p_type, E>,
10071009
) -> ::core::result::Result<(), E> {
1008-
// SAFETY: TODO.
1010+
// SAFETY: `slot` points to valid, uninitialized memory for a `$p_type`.
10091011
unsafe { $crate::PinInit::__pinned_init(init, slot) }
10101012
}
10111013
)*
@@ -1016,7 +1018,7 @@ macro_rules! __pin_data {
10161018
slot: *mut $type,
10171019
init: impl $crate::Init<$type, E>,
10181020
) -> ::core::result::Result<(), E> {
1019-
// SAFETY: TODO.
1021+
// SAFETY: `slot` points to valid, uninitialized memory for a `$type`.
10201022
unsafe { $crate::Init::__init(init, slot) }
10211023
}
10221024
)*
@@ -1132,7 +1134,8 @@ macro_rules! __init_internal {
11321134
struct __InitOk;
11331135
// Get the data about fields from the supplied type.
11341136
//
1135-
// SAFETY: TODO.
1137+
// SAFETY: The `$get_data()` function only returns metadata about the type's pinning structure.
1138+
// No memory is accessed, only type-level information is retrieved.
11361139
let data = unsafe {
11371140
use $crate::__internal::$has_data;
11381141
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
@@ -1188,7 +1191,8 @@ macro_rules! __init_internal {
11881191
let init = move |slot| -> ::core::result::Result<(), $err> {
11891192
init(slot).map(|__InitOk| ())
11901193
};
1191-
// SAFETY: TODO.
1194+
// SAFETY: The closure property initializes the target memory location. Error handling
1195+
// ensures memory is left in a valid state if initialization fails.
11921196
let init = unsafe { $crate::$construct_closure::<_, $err>(init) };
11931197
init
11941198
}};
@@ -1338,7 +1342,8 @@ macro_rules! __init_internal {
13381342
// Since we are in the closure that is never called, this will never get executed.
13391343
// We abuse `slot` to get the correct type inference here:
13401344
//
1341-
// SAFETY: TODO.
1345+
// SAFETY: This is unreachable code that is used solely for compile-time type checking,
1346+
// it is never executed.
13421347
unsafe {
13431348
// Here we abuse `paste!` to retokenize `$t`. Declarative macros have some internal
13441349
// information that is associated to already parsed fragments, so a path fragment

0 commit comments

Comments
 (0)