Repository: tokio-rs/tokio
File: tokio/src/util/wake_list.rs (Rust)

Code before:
            start: usize,
            end: usize,
        }

        impl Drop for DropGuard {
            fn drop(&mut self) {
                let slice = ptr::slice_from_raw_parts_mut(
                    unsafe { self.wakers.add(self.start) },
                    self.end - self.start,
                );
                // SAFETY: Elements in `start..end` are initialized, so we can drop them.
                unsafe { ptr::drop_in_place(slice) };
            }
        }

        assert!(self.curr <= NUM_WAKERS);

        let mut guard = DropGuard {
            wakers: self.inner.as_mut_ptr().cast::<Waker>(),
            start: 0,
            end: mem::take(&mut self.curr),
        };
        while guard.start < guard.end {
            // SAFETY: Elements between `start..end` are initialized.
            let waker = unsafe { ptr::read(guard.wakers.add(guard.start)) };
            guard.start += 1;
            waker.wake();
        }
    }
}

impl Drop for WakeList {
    fn drop(&mut self) {
        let slice =
            ptr::slice_from_raw_parts_mut(self.inner.as_mut_ptr().cast::<Waker>(), self.curr);
        // SAFETY: The first `curr` elements are initialized, so we can drop them.
        unsafe { ptr::drop_in_place(slice) };
    }
}


Review comment (suggestion):
The reason why this `ptr::read` operation is safe is that by incrementing `guard.start` by one, the guard gives up ownership of the waker, so we can transfer ownership of it to the local variable.

Code after:
            end: *mut Waker,
        }

        impl Drop for DropGuard {
            fn drop(&mut self) {
                // SAFETY: Both pointers are part of the same object, with `start <= end`.
                let len = unsafe { self.end.offset_from(self.start) } as usize;
                let slice = ptr::slice_from_raw_parts_mut(self.start, len);
                // SAFETY: All elements in `start..len` are initialized, so we can drop them.
                unsafe { ptr::drop_in_place(slice) };
            }
        }

        debug_assert!(self.curr <= NUM_WAKERS);

        let mut guard = {
            let start = self.inner.as_mut_ptr().cast::<Waker>();
            // SAFETY: The resulting pointer is in bounds or one after the length of the same object.
            let end = unsafe { start.add(self.curr) };
            // Transfer ownership of the wakers in `inner` to `DropGuard`.
            self.curr = 0;
            DropGuard { start, end }
        };
        while !ptr::eq(guard.start, guard.end) {
            // SAFETY: `start` is always initialized if `start != end`.
            let waker = unsafe { ptr::read(guard.start) };
            // SAFETY: The resulting pointer is in bounds or one after the length of the same object.
            guard.start = unsafe { guard.start.add(1) };
            // If this panics, then `guard` will clean up the remaining wakers.
            waker.wake();
        }
    }
}

impl Drop for WakeList {
    fn drop(&mut self) {
        let slice =
            ptr::slice_from_raw_parts_mut(self.inner.as_mut_ptr().cast::<Waker>(), self.curr);
        // SAFETY: The first `curr` elements are initialized, so we can drop them.
        unsafe { ptr::drop_in_place(slice) };
    }
}