From e4518a09dc5ba194653799dd1c41ba40d9a761bb Mon Sep 17 00:00:00 2001 From: Maciej Hirsz <1096222+maciejhirsz@users.noreply.github.com> Date: Sun, 14 Apr 2024 10:12:23 +0200 Subject: [PATCH] Added missing `Drop` impl for `BoundedVec` (#103) --- crates/kobold/src/list/bounded.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/kobold/src/list/bounded.rs b/crates/kobold/src/list/bounded.rs index 228ce1c..ae43e28 100644 --- a/crates/kobold/src/list/bounded.rs +++ b/crates/kobold/src/list/bounded.rs @@ -158,24 +158,22 @@ impl Deref for BoundedVec { type Target = [T]; fn deref(&self) -> &Self::Target { - if self.len > N { - unsafe { std::hint::unreachable_unchecked() } - } - - let ptr = &self.data[..self.len] as *const [MaybeUninit] as *const [T]; + debug_assert!(self.len <= N); - unsafe { &*ptr } + unsafe { &*(self.data.get_unchecked(..self.len) as *const [_] as *const [T]) } } } impl DerefMut for BoundedVec { fn deref_mut(&mut self) -> &mut Self::Target { - if self.len > N { - unsafe { std::hint::unreachable_unchecked() } - } + debug_assert!(self.len <= N); - let ptr = &mut self.data[..self.len] as *mut [MaybeUninit] as *mut [T]; + unsafe { &mut *(self.data.get_unchecked_mut(..self.len) as *mut [_] as *mut [T]) } + } +} - unsafe { &mut *ptr } +impl Drop for BoundedVec { + fn drop(&mut self) { + unsafe { std::ptr::drop_in_place(&mut **self as *mut [T]) } } }