From 265425846f36fd876f828e2d4ae47c6150bcc8b1 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Thu, 24 Sep 2026 15:43:21 +0200 Subject: [PATCH 1/2] refactor: manual location setting --- src/lib.rs | 30 +++++------------------------- src/taggedlen.rs | 7 +++++++ 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5434d44f..4914aad7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -593,26 +593,6 @@ impl SmallVec { impl SmallVec { const IS_ZST: bool = size_of::() == 0; - /// Sets the tag to be on the heap - /// - /// # Safety - /// - /// The active union member must be the self.raw.heap - #[inline] - unsafe fn set_on_heap(&mut self) { - self.len = TaggedLen::new(self.len(), true); - } - - /// Sets the tag to be inline - /// - /// # Safety - /// - /// The active union member must be the self.raw.inline - #[inline] - unsafe fn set_inline(&mut self) { - self.len = TaggedLen::new(self.len(), false); - } - /// Sets the length of a vector. /// /// This will explicitly set the size of the vector, without actually @@ -901,7 +881,7 @@ impl SmallVec { if result.is_ok() { // SAFETY: the allocation succeeded, so self.raw.heap is now // active - unsafe { self.set_on_heap() }; + self.len.set_location::(); } result } else { @@ -921,7 +901,7 @@ impl SmallVec { align: align_of::(), alloc: &self.allocator }); - self.set_inline(); + self.len.set_location::(); } } Ok(()) @@ -993,7 +973,7 @@ impl SmallVec { unsafe { let (ptr, capacity) = self.raw.heap; copy_nonoverlapping(ptr.as_ptr(), self.raw.as_mut_ptr_inline(), len); - self.set_inline(); + self.len.set_location::(); self.allocator.deallocate( ptr.cast(), Layout::from_size_align_unchecked(capacity * size_of::(), align_of::()) @@ -1026,7 +1006,7 @@ impl SmallVec { unsafe { let (ptr, capacity) = self.raw.heap; copy_nonoverlapping(ptr.as_ptr(), self.raw.as_mut_ptr_inline(), len); - self.set_inline(); + self.len.set_location::(); self.allocator.deallocate( ptr.cast(), Layout::from_size_align_unchecked( @@ -1603,7 +1583,7 @@ impl SmallVec { }?; // SAFETY: the allocation succeeded, so self.raw.heap is now active - unsafe { this.set_on_heap() }; + this.len.set_location::(); } Ok(this) } diff --git a/src/taggedlen.rs b/src/taggedlen.rs index c3571657..5a373c55 100644 --- a/src/taggedlen.rs +++ b/src/taggedlen.rs @@ -55,6 +55,13 @@ impl TaggedLen { (self.0 >> Self::SHIFT, (self.0 & Self::TAG) != 0) } + #[inline(always)] + pub const fn set_location(&mut self) { + if Self::TAG != 0 { + self.0 = (self.0 & !Self::TAG) | ON as usize; + } + } + /// # Safety /// /// current len+n must be smaller than MAX_LEN on 64-bit target From 50557bef95000b4220245c8e032409e5d2b3e53b Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Thu, 24 Sep 2026 15:58:58 +0200 Subject: [PATCH 2/2] fix: rename len to length --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9e96afad..f45b6a3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -703,7 +703,7 @@ impl SmallVec { if result.is_ok() { // SAFETY: the allocation succeeded, so self.raw.heap is now // active - self.len.set_location::(); + self.length.set_location::(); } result } else { @@ -723,7 +723,7 @@ impl SmallVec { align: align_of::(), allocator: &self.allocator }); - self.len.set_location::(); + self.length.set_location::(); } } Ok(()) @@ -780,7 +780,7 @@ impl SmallVec { unsafe { let (ptr, capacity) = self.raw.heap; copy_nonoverlapping(ptr.as_ptr(), self.raw.as_mut_ptr_inline(), length); - self.len.set_location::(); + self.length.set_location::(); self.allocator.deallocate( ptr.cast(), Layout::from_size_align_unchecked(capacity * size_of::(), align_of::()) @@ -813,7 +813,7 @@ impl SmallVec { unsafe { let (ptr, capacity) = self.raw.heap; copy_nonoverlapping(ptr.as_ptr(), self.raw.as_mut_ptr_inline(), length); - self.len.set_location::(); + self.length.set_location::(); self.allocator.deallocate( ptr.cast(), Layout::from_size_align_unchecked( @@ -1391,7 +1391,7 @@ impl SmallVec { }?; // SAFETY: the allocation succeeded, so self.raw.heap is now active - this.len.set_location::(); + this.length.set_location::(); } Ok(this) }