cl-structures/stack: only drop if T needs drop

This commit is contained in:
John 2024-04-27 21:08:09 -05:00
parent e7c5a02afa
commit a8ef989084

View File

@ -164,9 +164,11 @@ unsafe impl<#[may_dangle] T, const N: usize> Drop for Stack<T, N> {
#[inline] #[inline]
fn drop(&mut self) { fn drop(&mut self) {
// Safety: We have ensured that all elements in the list are // Safety: We have ensured that all elements in the list are
if std::mem::needs_drop::<T>() {
unsafe { core::ptr::drop_in_place(self.as_mut_slice()) }; unsafe { core::ptr::drop_in_place(self.as_mut_slice()) };
} }
} }
}
impl<T, const N: usize> Extend<T> for Stack<T, N> { impl<T, const N: usize> Extend<T> for Stack<T, N> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
@ -617,7 +619,6 @@ mod tests {
assert_eq!(std::mem::size_of::<usize>(), std::mem::size_of_val(&v)) assert_eq!(std::mem::size_of::<usize>(), std::mem::size_of_val(&v))
} }
#[test] #[test]
#[cfg_attr(debug_assertions, ignore = "calls ().drop() usize::MAX times")]
fn from_usize_max_zst_array() { fn from_usize_max_zst_array() {
let mut v = Stack::from([(); usize::MAX]); let mut v = Stack::from([(); usize::MAX]);
assert_eq!(v.len(), usize::MAX); assert_eq!(v.len(), usize::MAX);