@@ -134,15 +134,19 @@ pub struct StaticInit<T> {
134
134
inner : MaybeUninit < UnsafeCell < T > > ,
135
135
}
136
136
137
+ // SAFETY: Need to implement Send/Sync, because of the `UnsafeCell`. One can only get a `&T` from a
138
+ // `StaticInit<T>`. Except when calling `init` which is unsafe and only done before other code can
139
+ // access the `StaticInit<T>`.
137
140
unsafe impl < T : Sync > Sync for StaticInit < T > { }
141
+ // SAFETY: same as above.
138
142
unsafe impl < T : Send > Send for StaticInit < T > { }
139
143
140
144
impl < T > StaticInit < T > {
141
145
/// Creates a new `StaticInit` that is uninitialized.
142
146
///
143
147
/// # Safety
144
148
///
145
- /// The caller calls `Self::init` exactly once before using this value.
149
+ /// The caller calls `Self::init` exactly once before using this value in any way .
146
150
pub const unsafe fn uninit ( ) -> Self {
147
151
Self {
148
152
inner : MaybeUninit :: uninit ( ) ,
@@ -154,11 +158,13 @@ impl<T> StaticInit<T> {
154
158
/// # Safety
155
159
///
156
160
/// The caller calls this function exactly once and before any other function (even implicitly
157
- /// derefing) of `self` is called.
161
+ /// derefing) of `self` is called. `self` stays pinned indefinetly.
158
162
pub unsafe fn init < E > ( & self , init : impl PinInit < T , E > )
159
163
where
160
164
E : Into < core:: convert:: Infallible > ,
161
165
{
166
+ // SAFETY: This function has unique access to `self` because of the unsafety contract.
167
+ // `self` is also pinned indefinetly and `inner` is structurally pinned.
162
168
unsafe {
163
169
let ptr = UnsafeCell :: raw_get ( self . inner . as_ptr ( ) ) ;
164
170
match init. __pinned_init ( ptr) . map_err ( |e| e. into ( ) ) {
@@ -172,6 +178,7 @@ impl<T> StaticInit<T> {
172
178
impl < T > core:: ops:: Deref for StaticInit < T > {
173
179
type Target = T ;
174
180
fn deref ( & self ) -> & Self :: Target {
181
+ // SAFETY: self.inner has been initialized because of the contract of `Self::uninit()`
175
182
unsafe { & * self . inner . assume_init_ref ( ) . get ( ) }
176
183
}
177
184
}
0 commit comments