From aee0d00899772c09ddf5c22c2e354653c84fcfb8 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Mon, 5 Oct 2026 14:24:38 +0200 Subject: [PATCH 1/6] feat: allocator extension for splice --- src/iterators/splice/allocref.rs | 32 ++++++++++++++++++++++ src/iterators/{splice.rs => splice/mod.rs} | 26 ++++++++++-------- 2 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/iterators/splice/allocref.rs rename src/iterators/{splice.rs => splice/mod.rs} (88%) diff --git a/src/iterators/splice/allocref.rs b/src/iterators/splice/allocref.rs new file mode 100644 index 00000000..a61e22b6 --- /dev/null +++ b/src/iterators/splice/allocref.rs @@ -0,0 +1,32 @@ +use { + super::Allocator, + core::{ + alloc::Layout, + ptr::NonNull + } +}; + +pub struct Allocref<'valid, Heap: Allocator>(pub &'valid Heap); + +impl<'valid, Heap: Allocator> Allocator for Allocref<'valid, Heap> { + fn allocate(&self, layout: Layout) -> Option> { + self.0.allocate(layout) + } + + unsafe fn deallocate(&self, pointer: NonNull, layout: Layout) { + unsafe { self.0.deallocate(pointer, layout) }; + } + + unsafe fn grow(&self, pointer: NonNull, old: Layout, new: Layout) -> Option> { + unsafe { self.0.grow(pointer, old, new) } + } + + unsafe fn shrink( + &self, + pointer: NonNull, + old: Layout, + new: Layout + ) -> Option> { + unsafe { self.0.shrink(pointer, old, new) } + } +} diff --git a/src/iterators/splice.rs b/src/iterators/splice/mod.rs similarity index 88% rename from src/iterators/splice.rs rename to src/iterators/splice/mod.rs index 2f231d17..7cd407a0 100644 --- a/src/iterators/splice.rs +++ b/src/iterators/splice/mod.rs @@ -1,8 +1,15 @@ -use crate::{ - Allocator, - Drain, - Global, - SmallVec +mod allocref; + +use { + allocref::Allocref, + super::{ + super::{ + Allocator, + Global, + SmallVec + }, + drain::Drain + } }; pub struct Splice<'a, I: Iterator + 'a, const N: usize, A: Allocator = Global> { @@ -81,12 +88,9 @@ impl Drop for Splice<'_, I, N, A> { } } - // Collect any remaining elements. - let mut collected = self - .replace_with - .by_ref() - .collect::>() - .into_iter(); + let mut smallvec = SmallVec::<_, N, _>::new_in(Allocref(&self.drain.vec.as_ref().allocator)); + smallvec.extend(self.replace_with.by_ref()); + let mut collected = smallvec.into_iter(); // Now we have an exact count. if collected.len() > 0 { From f457c6b644a18f9e61563a6497c577341b63f1ef Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Mon, 5 Oct 2026 14:27:52 +0200 Subject: [PATCH 2/6] chore: inline methods --- src/iterators/splice/allocref.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/iterators/splice/allocref.rs b/src/iterators/splice/allocref.rs index a61e22b6..1959b78b 100644 --- a/src/iterators/splice/allocref.rs +++ b/src/iterators/splice/allocref.rs @@ -9,18 +9,22 @@ use { pub struct Allocref<'valid, Heap: Allocator>(pub &'valid Heap); impl<'valid, Heap: Allocator> Allocator for Allocref<'valid, Heap> { + #[inline] fn allocate(&self, layout: Layout) -> Option> { self.0.allocate(layout) } + #[inline] unsafe fn deallocate(&self, pointer: NonNull, layout: Layout) { unsafe { self.0.deallocate(pointer, layout) }; } + #[inline] unsafe fn grow(&self, pointer: NonNull, old: Layout, new: Layout) -> Option> { unsafe { self.0.grow(pointer, old, new) } } + #[inline] unsafe fn shrink( &self, pointer: NonNull, From 54f15078a9ac6e0844c84c4f27e43620aae61cfb Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Mon, 5 Oct 2026 14:28:31 +0200 Subject: [PATCH 3/6] style: formatting --- src/iterators/splice/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/iterators/splice/mod.rs b/src/iterators/splice/mod.rs index 7cd407a0..281a61aa 100644 --- a/src/iterators/splice/mod.rs +++ b/src/iterators/splice/mod.rs @@ -1,7 +1,6 @@ mod allocref; use { - allocref::Allocref, super::{ super::{ Allocator, @@ -9,7 +8,8 @@ use { SmallVec }, drain::Drain - } + }, + allocref::Allocref }; pub struct Splice<'a, I: Iterator + 'a, const N: usize, A: Allocator = Global> { @@ -88,7 +88,8 @@ impl Drop for Splice<'_, I, N, A> { } } - let mut smallvec = SmallVec::<_, N, _>::new_in(Allocref(&self.drain.vec.as_ref().allocator)); + let mut smallvec = + SmallVec::<_, N, _>::new_in(Allocref(&self.drain.vec.as_ref().allocator)); smallvec.extend(self.replace_with.by_ref()); let mut collected = smallvec.into_iter(); From 594a982206dac2ed73106c9d06573c89a4065dc0 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Mon, 5 Oct 2026 14:32:43 +0200 Subject: [PATCH 4/6] chore: add repr transparent --- src/iterators/splice/allocref.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/iterators/splice/allocref.rs b/src/iterators/splice/allocref.rs index 1959b78b..ca5acd0a 100644 --- a/src/iterators/splice/allocref.rs +++ b/src/iterators/splice/allocref.rs @@ -6,6 +6,7 @@ use { } }; +#[repr(transparent)] pub struct Allocref<'valid, Heap: Allocator>(pub &'valid Heap); impl<'valid, Heap: Allocator> Allocator for Allocref<'valid, Heap> { From 3f91a9d39e3945005ef87a185588aaa1fcfe7d68 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Tue, 6 Oct 2026 12:34:00 +0200 Subject: [PATCH 5/6] refactor: rename to proxy --- src/allocator/mod.rs | 1 + .../splice/allocref.rs => allocator/proxy.rs} | 4 ++-- src/iterators/{splice/mod.rs => splice.rs} | 10 ++++------ 3 files changed, 7 insertions(+), 8 deletions(-) rename src/{iterators/splice/allocref.rs => allocator/proxy.rs} (85%) rename src/iterators/{splice/mod.rs => splice.rs} (95%) diff --git a/src/allocator/mod.rs b/src/allocator/mod.rs index 2e212219..5e67a3a8 100644 --- a/src/allocator/mod.rs +++ b/src/allocator/mod.rs @@ -4,6 +4,7 @@ mod base; mod external; #[cfg(all(feature = "allocator-api", not(feature = "allocator-api2")))] mod native; +pub mod proxy; use core::{ alloc::Layout, diff --git a/src/iterators/splice/allocref.rs b/src/allocator/proxy.rs similarity index 85% rename from src/iterators/splice/allocref.rs rename to src/allocator/proxy.rs index ca5acd0a..9628da89 100644 --- a/src/iterators/splice/allocref.rs +++ b/src/allocator/proxy.rs @@ -7,9 +7,9 @@ use { }; #[repr(transparent)] -pub struct Allocref<'valid, Heap: Allocator>(pub &'valid Heap); +pub struct Proxy<'valid, Heap: Allocator>(pub &'valid Heap); -impl<'valid, Heap: Allocator> Allocator for Allocref<'valid, Heap> { +impl<'valid, Heap: Allocator> Allocator for Proxy<'valid, Heap> { #[inline] fn allocate(&self, layout: Layout) -> Option> { self.0.allocate(layout) diff --git a/src/iterators/splice/mod.rs b/src/iterators/splice.rs similarity index 95% rename from src/iterators/splice/mod.rs rename to src/iterators/splice.rs index 281a61aa..0cee6c1c 100644 --- a/src/iterators/splice/mod.rs +++ b/src/iterators/splice.rs @@ -1,15 +1,13 @@ -mod allocref; - use { super::{ super::{ Allocator, Global, - SmallVec + SmallVec, + allocator::proxy::Proxy }, drain::Drain - }, - allocref::Allocref + } }; pub struct Splice<'a, I: Iterator + 'a, const N: usize, A: Allocator = Global> { @@ -89,7 +87,7 @@ impl Drop for Splice<'_, I, N, A> { } let mut smallvec = - SmallVec::<_, N, _>::new_in(Allocref(&self.drain.vec.as_ref().allocator)); + SmallVec::<_, N, _>::new_in(Proxy(&self.drain.vec.as_ref().allocator)); smallvec.extend(self.replace_with.by_ref()); let mut collected = smallvec.into_iter(); From 6b6ade17cfd3446f9f84198401d9b534ed50383b Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Tue, 6 Oct 2026 12:57:45 +0200 Subject: [PATCH 6/6] style: formatting --- src/iterators/splice.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/iterators/splice.rs b/src/iterators/splice.rs index 0cee6c1c..f827b6f0 100644 --- a/src/iterators/splice.rs +++ b/src/iterators/splice.rs @@ -1,13 +1,11 @@ -use { +use super::{ super::{ - super::{ - Allocator, - Global, - SmallVec, - allocator::proxy::Proxy - }, - drain::Drain - } + Allocator, + Global, + SmallVec, + allocator::proxy::Proxy + }, + drain::Drain }; pub struct Splice<'a, I: Iterator + 'a, const N: usize, A: Allocator = Global> {