From 7c840df93b210ceabc0fc244b1f5bc804cc2643c Mon Sep 17 00:00:00 2001 From: panstromek Date: Wed, 22 Jul 2026 22:18:08 +0200 Subject: [PATCH 1/2] refactor: move drop_non_singleton out of the Drop impl --- src/lib.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index af72fc5..b41971a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1920,23 +1920,23 @@ impl ThinVec { } } -impl Drop for ThinVec { - #[inline] - fn drop(&mut self) { - #[cold] - #[inline(never)] - fn drop_non_singleton(this: &mut ThinVec) { - unsafe { - ptr::drop_in_place(&mut this[..]); - - if this.uses_stack_allocated_buffer() { - return; - } +#[cold] +#[inline(never)] +fn drop_non_singleton(this: &mut ThinVec) { + unsafe { + ptr::drop_in_place(&mut this[..]); - dealloc(this.ptr() as *mut u8, layout::(this.capacity())) - } + if this.uses_stack_allocated_buffer() { + return; } + dealloc(this.ptr() as *mut u8, layout::(this.capacity())) + } +} + +impl Drop for ThinVec { + #[inline] + fn drop(&mut self) { if !self.is_singleton() { drop_non_singleton(self); } From b63bb5781ce71dcafe53353456e4decfe153cde0 Mon Sep 17 00:00:00 2001 From: panstromek Date: Wed, 22 Jul 2026 22:26:29 +0200 Subject: [PATCH 2/2] feat: add may_dangle Drop impl under unstable feature --- src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b41971a..390cd9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,6 +151,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "unstable", feature(trusted_len))] +#![cfg_attr(feature = "unstable", feature(dropck_eyepatch))] #![allow(clippy::comparison_chain, clippy::missing_safety_doc)] extern crate alloc; @@ -1934,6 +1935,7 @@ fn drop_non_singleton(this: &mut ThinVec) { } } +#[cfg(not(feature = "unstable"))] impl Drop for ThinVec { #[inline] fn drop(&mut self) { @@ -1943,6 +1945,16 @@ impl Drop for ThinVec { } } +#[cfg(feature = "unstable")] +unsafe impl<#[may_dangle] T> Drop for ThinVec { + #[inline] + fn drop(&mut self) { + if !self.is_singleton() { + drop_non_singleton(self); + } + } +} + impl Deref for ThinVec { type Target = [T];