Skip to content

Commit 12d90aa

Browse files
committed
put the MaybeUninit inside the UnsafeCell
1 parent a4f1234 commit 12d90aa

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

src/libcore/mem.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,9 +1106,6 @@ impl<T> MaybeUninit<T> {
11061106
///
11071107
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
11081108
/// state, otherwise this will immediately cause undefined behavior.
1109-
// FIXME: We currently rely on the above being incorrect, i.e., we have references
1110-
// to uninitialized data (e.g. in `libstd/sys/windows/mutex.rs`). We should make
1111-
// a final decision about the rules before stabilization.
11121109
#[unstable(feature = "maybe_uninit", issue = "53491")]
11131110
#[inline(always)]
11141111
pub unsafe fn get_ref(&self) -> &T {

src/libstd/sys/windows/mutex.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,37 +157,34 @@ fn kind() -> Kind {
157157
return ret;
158158
}
159159

160-
pub struct ReentrantMutex { inner: MaybeUninit<UnsafeCell<c::CRITICAL_SECTION>> }
160+
pub struct ReentrantMutex { inner: UnsafeCell<MaybeUninit<c::CRITICAL_SECTION>> }
161161

162162
unsafe impl Send for ReentrantMutex {}
163163
unsafe impl Sync for ReentrantMutex {}
164164

165165
impl ReentrantMutex {
166166
pub fn uninitialized() -> ReentrantMutex {
167-
ReentrantMutex { inner: MaybeUninit::uninitialized() }
167+
ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninitialized()) }
168168
}
169169

170170
pub unsafe fn init(&mut self) {
171-
// FIXME: Technically, this is calling `get_ref` on an uninitialized
172-
// `MaybeUninit`. Revisit this once we decided whether that is valid
173-
// or not.
174-
c::InitializeCriticalSection(self.inner.get_ref().get());
171+
c::InitializeCriticalSection(self.inner.get().as_mut_ptr());
175172
}
176173

177174
pub unsafe fn lock(&self) {
178-
c::EnterCriticalSection(self.inner.get_ref().get());
175+
c::EnterCriticalSection(self.inner.get().get_ref());
179176
}
180177

181178
#[inline]
182179
pub unsafe fn try_lock(&self) -> bool {
183-
c::TryEnterCriticalSection(self.inner.get_ref().get()) != 0
180+
c::TryEnterCriticalSection(self.inner.get().get_ref()) != 0
184181
}
185182

186183
pub unsafe fn unlock(&self) {
187-
c::LeaveCriticalSection(self.inner.get_ref().get());
184+
c::LeaveCriticalSection(self.inner.get().get_ref());
188185
}
189186

190187
pub unsafe fn destroy(&self) {
191-
c::DeleteCriticalSection(self.inner.get_ref().get());
188+
c::DeleteCriticalSection(self.inner.get().get_ref());
192189
}
193190
}

0 commit comments

Comments
 (0)