From ed9f23cb10b4964e6d7912e77b763980ca2aa833 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 25 Oct 2022 21:34:41 +0900 Subject: [PATCH] Do not copy data before dropping --- src/once_cell.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/once_cell.rs b/src/once_cell.rs index 079b9b4..463dc62 100644 --- a/src/once_cell.rs +++ b/src/once_cell.rs @@ -749,7 +749,11 @@ impl fmt::Debug for OnceCell { impl Drop for OnceCell { fn drop(&mut self) { - drop(self.take()); + if State::from(*self.state.get_mut()) == State::Initialized { + // SAFETY: We know that the value is initialized, so it is safe to + // drop it. + unsafe { self.value.get().cast::().drop_in_place() } + } } }