Skip to content

Commit

Permalink
Use MaybeUninit::assume_init_{drop,read}
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Dec 23, 2023
1 parent 9a865c7 commit 89bdcb4
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 22 deletions.
3 changes: 1 addition & 2 deletions crossbeam-channel/src/flavors/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,7 @@ impl<T> Drop for Channel<T> {
unsafe {
debug_assert!(index < self.buffer.len());
let slot = self.buffer.get_unchecked_mut(index);
let msg = &mut *slot.msg.get();
msg.as_mut_ptr().drop_in_place();
(*slot.msg.get()).assume_init_drop();
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crossbeam-channel/src/flavors/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,7 @@ impl<T> Channel<T> {
// Drop the message in the slot.
let slot = (*block).slots.get_unchecked(offset);
slot.wait_write();
let p = &mut *slot.msg.get();
p.as_mut_ptr().drop_in_place();
(*slot.msg.get()).assume_init_drop();
} else {
(*block).wait_next();
// Deallocate the block and move to the next one.
Expand Down Expand Up @@ -663,8 +662,7 @@ impl<T> Drop for Channel<T> {
if offset < BLOCK_CAP {
// Drop the message in the slot.
let slot = (*block).slots.get_unchecked(offset);
let p = &mut *slot.msg.get();
p.as_mut_ptr().drop_in_place();
(*slot.msg.get()).assume_init_drop();
} else {
// Deallocate the block and move to the next one.
let next = *(*block).next.get_mut();
Expand Down
3 changes: 1 addition & 2 deletions crossbeam-deque/src/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,8 +1986,7 @@ impl<T> Drop for Injector<T> {
if offset < BLOCK_CAP {
// Drop the task in the slot.
let slot = (*block).slots.get_unchecked(offset);
let p = &mut *slot.task.get();
p.as_mut_ptr().drop_in_place();
(*slot.task.get()).assume_init_drop();
} else {
// Deallocate the block and move to the next one.
let next = *(*block).next.get_mut();
Expand Down
5 changes: 2 additions & 3 deletions crossbeam-epoch/src/sync/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ impl<T> Queue<T> {
.compare_exchange(tail, next, Release, Relaxed, guard);
}
guard.defer_destroy(head);
// TODO: Replace with MaybeUninit::read when api is stable
Some(n.data.as_ptr().read())
Some(n.data.assume_init_read())
})
.map_err(|_| ())
},
Expand Down Expand Up @@ -165,7 +164,7 @@ impl<T> Queue<T> {
.compare_exchange(tail, next, Release, Relaxed, guard);
}
guard.defer_destroy(head);
Some(n.data.as_ptr().read())
Some(n.data.assume_init_read())
})
.map_err(|_| ())
},
Expand Down
3 changes: 1 addition & 2 deletions crossbeam-queue/src/array_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ impl<T> Drop for ArrayQueue<T> {
unsafe {
debug_assert!(index < self.buffer.len());
let slot = self.buffer.get_unchecked_mut(index);
let value = &mut *slot.value.get();
value.as_mut_ptr().drop_in_place();
(*slot.value.get()).assume_init_drop();
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crossbeam-queue/src/seg_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,7 @@ impl<T> Drop for SegQueue<T> {
if offset < BLOCK_CAP {
// Drop the value in the slot.
let slot = (*block).slots.get_unchecked(offset);
let p = &mut *slot.value.get();
p.as_mut_ptr().drop_in_place();
(*slot.value.get()).assume_init_drop();
} else {
// Deallocate the block and move to the next one.
let next = *(*block).next.get_mut();
Expand Down Expand Up @@ -525,8 +524,7 @@ impl<T> Iterator for IntoIter<T> {
// and this is a non-empty queue.
let item = unsafe {
let slot = (*block).slots.get_unchecked(offset);
let p = &mut *slot.value.get();
p.as_mut_ptr().read()
slot.value.get().read().assume_init()
};
if offset + 1 == BLOCK_CAP {
// Deallocate the block and move to the next one.
Expand Down
8 changes: 3 additions & 5 deletions crossbeam-utils/src/sync/once_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ impl<T> OnceLock<T> {
where
F: FnOnce() -> T,
{
let slot = self.value.get().cast::<T>();
let slot = self.value.get();

self.once.call_once(|| {
let value = f();
unsafe {
slot.write(value);
}
unsafe { slot.write(MaybeUninit::new(value)) }
});
}

Expand All @@ -84,7 +82,7 @@ impl<T> Drop for OnceLock<T> {
fn drop(&mut self) {
if self.once.is_completed() {
// SAFETY: The inner value has been initialized
unsafe { self.value.get().cast::<T>().drop_in_place() };
unsafe { (*self.value.get()).assume_init_drop() };
}
}
}

0 comments on commit 89bdcb4

Please sign in to comment.