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/allocator/proxy.rs b/src/allocator/proxy.rs new file mode 100644 index 00000000..9628da89 --- /dev/null +++ b/src/allocator/proxy.rs @@ -0,0 +1,37 @@ +use { + super::Allocator, + core::{ + alloc::Layout, + ptr::NonNull + } +}; + +#[repr(transparent)] +pub struct Proxy<'valid, Heap: Allocator>(pub &'valid Heap); + +impl<'valid, Heap: Allocator> Allocator for Proxy<'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, + old: Layout, + new: Layout + ) -> Option> { + unsafe { self.0.shrink(pointer, old, new) } + } +} diff --git a/src/iterators/splice.rs b/src/iterators/splice.rs index 2f231d17..f827b6f0 100644 --- a/src/iterators/splice.rs +++ b/src/iterators/splice.rs @@ -1,8 +1,11 @@ -use crate::{ - Allocator, - Drain, - Global, - SmallVec +use super::{ + super::{ + Allocator, + Global, + SmallVec, + allocator::proxy::Proxy + }, + drain::Drain }; pub struct Splice<'a, I: Iterator + 'a, const N: usize, A: Allocator = Global> { @@ -81,12 +84,10 @@ 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(Proxy(&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 {