From 0b9f504f7d358c194a79bffeef2ec396199cf55f Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Sat, 17 Oct 2026 16:06:20 +0200 Subject: [PATCH 1/3] refactor: complete import paths --- src/borsh.rs | 6 ++++-- src/comparisons.rs | 6 +++--- src/conversions.rs | 12 +++++++----- src/iterators/drain.rs | 14 +++++++++----- src/iterators/extractif.rs | 10 ++++++---- src/iterators/intoiter.rs | 14 ++++++++------ src/iterators/rayon.rs | 6 +++--- src/iterators/splice.rs | 10 ++++++---- src/lib.rs | 16 +++++++++------- src/mallocsizeof.rs | 2 +- src/rawsmallvec.rs | 4 ++-- src/references.rs | 2 +- src/serde.rs | 6 ++++-- src/specialization.rs | 10 ++++++---- 14 files changed, 69 insertions(+), 49 deletions(-) diff --git a/src/borsh.rs b/src/borsh.rs index dcce21ee..1e6c2e71 100644 --- a/src/borsh.rs +++ b/src/borsh.rs @@ -1,7 +1,9 @@ use { super::{ - Allocator, - Global, + allocator::{ + Allocator, + global::Global + }, SmallVec }, alloc::{ diff --git a/src/comparisons.rs b/src/comparisons.rs index bd8be173..6ab48b38 100644 --- a/src/comparisons.rs +++ b/src/comparisons.rs @@ -1,7 +1,7 @@ use { - crate::{ - Allocator, - SmallVec + super::{ + SmallVec, + allocator::Allocator }, alloc::{ borrow::Cow, diff --git a/src/conversions.rs b/src/conversions.rs index 0c4579f4..8c64d17b 100644 --- a/src/conversions.rs +++ b/src/conversions.rs @@ -1,10 +1,12 @@ use { - crate::{ - Allocator, - Box, - Global, + super::{ SmallVec, - Vec + allocator::{ + Allocator, + global::Global, + boxed::Box, + vec::Vec + } }, core::{ mem::ManuallyDrop, diff --git a/src/iterators/drain.rs b/src/iterators/drain.rs index 83732f65..216f5bdd 100644 --- a/src/iterators/drain.rs +++ b/src/iterators/drain.rs @@ -1,8 +1,12 @@ -use crate::{ - Allocator, - Global, - SmallVec, - SmallVecError +use { + super::super::{ + allocator::{ + Allocator, + global::Global + }, + SmallVec, + errors::SmallVecError + } }; /// An iterator that removes the items from a `SmallVec` and yields them by diff --git a/src/iterators/extractif.rs b/src/iterators/extractif.rs index 02d0be99..82455b2f 100644 --- a/src/iterators/extractif.rs +++ b/src/iterators/extractif.rs @@ -1,7 +1,9 @@ -use crate::{ - Allocator, - Global, - SmallVec +use super::super::{ + SmallVec, + allocator::{ + Allocator, + global::Global + } }; /// An iterator which uses a closure to determine if an element should be diff --git a/src/iterators/intoiter.rs b/src/iterators/intoiter.rs index a2bea0bf..a8dae970 100644 --- a/src/iterators/intoiter.rs +++ b/src/iterators/intoiter.rs @@ -1,11 +1,13 @@ use { - crate::{ - Allocator, + super::super::{ + SmallVec, DropDealloc, - Global, - LocatedLength, - RawSmallVec, - SmallVec + locatedlength::LocatedLength, + rawsmallvec::RawSmallVec, + allocator::{ + Allocator, + global::Global + } }, core::{ fmt::Debug, diff --git a/src/iterators/rayon.rs b/src/iterators/rayon.rs index efb24d0f..ba60b566 100644 --- a/src/iterators/rayon.rs +++ b/src/iterators/rayon.rs @@ -2,9 +2,9 @@ //! need, except it's all private use { - crate::{ - Allocator, - SmallVec + super::super::{ + SmallVec, + allocator::Allocator }, core::{ mem::take, diff --git a/src/iterators/splice.rs b/src/iterators/splice.rs index 7d50a3d1..a92092a5 100644 --- a/src/iterators/splice.rs +++ b/src/iterators/splice.rs @@ -1,9 +1,11 @@ use super::{ super::{ - Allocator, - Global, - SmallVec, - allocator::proxy::Proxy + allocator::{ + Allocator, + global::Global, + proxy::Proxy + }, + SmallVec }, drain::Drain }; diff --git a/src/lib.rs b/src/lib.rs index fa03ac5e..a303a997 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,12 +23,6 @@ mod comparisons; mod conversions; mod errors; mod iterators; -pub use iterators::{ - drain::Drain, - extractif::ExtractIf, - intoiter::IntoIter, - splice::Splice -}; mod locatedlength; mod macros; #[cfg(feature = "malloc_size_of")] @@ -52,7 +46,6 @@ use defmt::{ Formatter as DeFormatter, write as dewrite }; -pub use errors::SmallVecError; #[cfg(feature = "std")] use std::io; #[cfg(feature = "zeroize")] @@ -99,6 +92,15 @@ use { locatedlength::LocatedLength, rawsmallvec::RawSmallVec }; +pub use { + iterators::{ + drain::Drain, + extractif::ExtractIf, + intoiter::IntoIter, + splice::Splice + }, + errors::SmallVecError +}; #[repr(C)] pub struct SmallVec { diff --git a/src/mallocsizeof.rs b/src/mallocsizeof.rs index 151d09e2..0350e8ae 100644 --- a/src/mallocsizeof.rs +++ b/src/mallocsizeof.rs @@ -1,6 +1,6 @@ use { super::{ - Allocator, + allocator::Allocator, SmallVec }, malloc_size_of::{ diff --git a/src/rawsmallvec.rs b/src/rawsmallvec.rs index b10b4845..89346eb2 100644 --- a/src/rawsmallvec.rs +++ b/src/rawsmallvec.rs @@ -1,7 +1,7 @@ use { super::{ - Allocator, - SmallVecError, + allocator::Allocator, + errors::SmallVecError, locatedlength::LocatedLength }, core::{ diff --git a/src/references.rs b/src/references.rs index e278baaa..5a8f48b7 100644 --- a/src/references.rs +++ b/src/references.rs @@ -1,6 +1,6 @@ use { super::{ - Allocator, + allocator::Allocator, SmallVec }, core::{ diff --git a/src/serde.rs b/src/serde.rs index ff3272a9..af82f49d 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -1,7 +1,9 @@ use { super::{ - Allocator, - Global, + allocator::{ + Allocator, + global::Global + }, SmallVec }, core::marker::PhantomData, diff --git a/src/specialization.rs b/src/specialization.rs index 11041b30..9162f068 100644 --- a/src/specialization.rs +++ b/src/specialization.rs @@ -1,9 +1,11 @@ use { - crate::{ - Allocator, + super::{ + allocator::{ + Allocator, + global::Global + }, + iterators::intoiter::IntoIter, DropGuard, - Global, - IntoIter, SmallVec }, core::ptr::copy_nonoverlapping From 9adcec842c0ddfd912c21c34f18805137ea40a64 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Sat, 17 Oct 2026 16:12:42 +0200 Subject: [PATCH 2/3] style: formatting --- src/borsh.rs | 4 ++-- src/conversions.rs | 2 +- src/iterators/drain.rs | 16 +++++++--------- src/iterators/intoiter.rs | 8 ++++---- src/iterators/splice.rs | 4 ++-- src/lib.rs | 18 +++++++++--------- src/mallocsizeof.rs | 4 ++-- src/references.rs | 4 ++-- src/serde.rs | 4 ++-- src/specialization.rs | 6 +++--- 10 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/borsh.rs b/src/borsh.rs index 1e6c2e71..5a943940 100644 --- a/src/borsh.rs +++ b/src/borsh.rs @@ -1,10 +1,10 @@ use { super::{ + SmallVec, allocator::{ Allocator, global::Global - }, - SmallVec + } }, alloc::{ collections::BTreeMap as Map, diff --git a/src/conversions.rs b/src/conversions.rs index 8c64d17b..af8edc2b 100644 --- a/src/conversions.rs +++ b/src/conversions.rs @@ -3,8 +3,8 @@ use { SmallVec, allocator::{ Allocator, - global::Global, boxed::Box, + global::Global, vec::Vec } }, diff --git a/src/iterators/drain.rs b/src/iterators/drain.rs index 216f5bdd..0e17f69e 100644 --- a/src/iterators/drain.rs +++ b/src/iterators/drain.rs @@ -1,12 +1,10 @@ -use { - super::super::{ - allocator::{ - Allocator, - global::Global - }, - SmallVec, - errors::SmallVecError - } +use super::super::{ + SmallVec, + allocator::{ + Allocator, + global::Global + }, + errors::SmallVecError }; /// An iterator that removes the items from a `SmallVec` and yields them by diff --git a/src/iterators/intoiter.rs b/src/iterators/intoiter.rs index a8dae970..fd296aa4 100644 --- a/src/iterators/intoiter.rs +++ b/src/iterators/intoiter.rs @@ -1,13 +1,13 @@ use { super::super::{ - SmallVec, DropDealloc, - locatedlength::LocatedLength, - rawsmallvec::RawSmallVec, + SmallVec, allocator::{ Allocator, global::Global - } + }, + locatedlength::LocatedLength, + rawsmallvec::RawSmallVec }, core::{ fmt::Debug, diff --git a/src/iterators/splice.rs b/src/iterators/splice.rs index a92092a5..e77be6ee 100644 --- a/src/iterators/splice.rs +++ b/src/iterators/splice.rs @@ -1,11 +1,11 @@ use super::{ super::{ + SmallVec, allocator::{ Allocator, global::Global, proxy::Proxy - }, - SmallVec + } }, drain::Drain }; diff --git a/src/lib.rs b/src/lib.rs index a303a997..12cb5e4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,6 +82,15 @@ use { }, newrange::NewRange }; +pub use { + errors::SmallVecError, + iterators::{ + drain::Drain, + extractif::ExtractIf, + intoiter::IntoIter, + splice::Splice + } +}; #[cfg(feature = "internals")] pub use { locatedlength::LocatedLength, @@ -92,15 +101,6 @@ use { locatedlength::LocatedLength, rawsmallvec::RawSmallVec }; -pub use { - iterators::{ - drain::Drain, - extractif::ExtractIf, - intoiter::IntoIter, - splice::Splice - }, - errors::SmallVecError -}; #[repr(C)] pub struct SmallVec { diff --git a/src/mallocsizeof.rs b/src/mallocsizeof.rs index 0350e8ae..2b586e2a 100644 --- a/src/mallocsizeof.rs +++ b/src/mallocsizeof.rs @@ -1,7 +1,7 @@ use { super::{ - allocator::Allocator, - SmallVec + SmallVec, + allocator::Allocator }, malloc_size_of::{ MallocShallowSizeOf, diff --git a/src/references.rs b/src/references.rs index 5a8f48b7..e3e7c079 100644 --- a/src/references.rs +++ b/src/references.rs @@ -1,7 +1,7 @@ use { super::{ - allocator::Allocator, - SmallVec + SmallVec, + allocator::Allocator }, core::{ borrow::{ diff --git a/src/serde.rs b/src/serde.rs index af82f49d..c9f195fa 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -1,10 +1,10 @@ use { super::{ + SmallVec, allocator::{ Allocator, global::Global - }, - SmallVec + } }, core::marker::PhantomData, serde_core::{ diff --git a/src/specialization.rs b/src/specialization.rs index 9162f068..a9051444 100644 --- a/src/specialization.rs +++ b/src/specialization.rs @@ -1,12 +1,12 @@ use { super::{ + DropGuard, + SmallVec, allocator::{ Allocator, global::Global }, - iterators::intoiter::IntoIter, - DropGuard, - SmallVec + iterators::intoiter::IntoIter }, core::ptr::copy_nonoverlapping }; From caf96745c968fe4d25e171e3e93390940e03a9e9 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Sat, 17 Oct 2026 19:10:11 +0200 Subject: [PATCH 3/3] refactor: direct imports --- src/borsh.rs | 3 +- src/comparisons.rs | 7 +- src/conversions.rs | 4 +- src/iterators/drain.rs | 58 ++++++----- src/iterators/extractif.rs | 31 ++++-- src/iterators/intoiter.rs | 29 ++++-- src/iterators/rayon.rs | 20 ++-- src/iterators/splice.rs | 32 +++--- src/lib.rs | 205 ++++++++++++++++++++++--------------- src/references.rs | 8 +- src/serde.rs | 12 ++- src/specialization.rs | 24 +++-- tests/encase.rs | 7 +- tests/macro.rs | 12 ++- tests/main.rs | 19 +++- 15 files changed, 293 insertions(+), 178 deletions(-) diff --git a/src/borsh.rs b/src/borsh.rs index 5a943940..70b03899 100644 --- a/src/borsh.rs +++ b/src/borsh.rs @@ -17,6 +17,7 @@ use { io::{ Error, ErrorKind, + Read, Result as Serial, Write }, @@ -44,7 +45,7 @@ impl BorshSerialize impl BorshDeserialize for SmallVec { - fn deserialize_reader(reader: &mut R) -> Serial { + fn deserialize_reader(reader: &mut R) -> Serial { let length = u64::deserialize_reader(reader)?; repeat_with(|| Item::deserialize_reader(reader)) .take(length.try_into().map_err(|_| Error::new( diff --git a/src/comparisons.rs b/src/comparisons.rs index 6ab48b38..bc70e5ff 100644 --- a/src/comparisons.rs +++ b/src/comparisons.rs @@ -7,7 +7,8 @@ use { borrow::Cow, collections::VecDeque, vec::Vec - } + }, + core::cmp::Ordering }; macro_rules! __impl_slice_eq1 { @@ -62,7 +63,7 @@ impl PartialOrd for SmallVec) -> Option { + fn partial_cmp(&self, other: &SmallVec) -> Option { self.as_ref().partial_cmp(other.as_ref()) } } @@ -71,7 +72,7 @@ impl Ord for SmallVec) -> core::cmp::Ordering { + fn cmp(&self, other: &SmallVec) -> Ordering { self.as_ref().cmp(other.as_ref()) } } diff --git a/src/conversions.rs b/src/conversions.rs index af8edc2b..42719317 100644 --- a/src/conversions.rs +++ b/src/conversions.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "specialization")] +use super::specialization::SpecFromSlice; use { super::{ SmallVec, @@ -26,7 +28,7 @@ impl From<&[Item]> for SmallVec>::spec_from(slice) + >::spec_from(slice) } #[cfg(not(feature = "specialization"))] diff --git a/src/iterators/drain.rs b/src/iterators/drain.rs index 0e17f69e..49bed6e2 100644 --- a/src/iterators/drain.rs +++ b/src/iterators/drain.rs @@ -1,10 +1,27 @@ -use super::super::{ - SmallVec, - allocator::{ - Allocator, - global::Global +use { + super::super::{ + SmallVec, + allocator::{ + Allocator, + global::Global + }, + errors::SmallVecError }, - errors::SmallVecError + core::{ + fmt::{ + Debug, + Formatter, + Result as Format + }, + iter::FusedIterator, + mem::take, + ptr::{ + NonNull, + copy, + slice_from_raw_parts_mut + }, + slice::Iter + } }; /// An iterator that removes the items from a `SmallVec` and yields them by @@ -23,8 +40,8 @@ pub struct Drain<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator = Gl // even though vec has length < tail_start pub(crate) tail_start: usize, pub(crate) tail_len: usize, - pub(crate) iter: core::slice::Iter<'valid, Item>, - pub(crate) vec: core::ptr::NonNull> + pub(crate) iter: Iter<'valid, Item>, + pub(crate) vec: NonNull> } impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Iterator @@ -38,7 +55,7 @@ impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Iterator // items, and we can take ownership of them. self.iter .next() - .map(|reference| unsafe { core::ptr::read(reference) }) + .map(|reference| unsafe { (&raw const *reference).read() }) } #[inline] @@ -55,7 +72,7 @@ impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> DoubleEndedIter // SAFETY: see above self.iter .next_back() - .map(|reference| unsafe { core::ptr::read(reference) }) + .map(|reference| unsafe { (&raw const *reference).read() }) } } @@ -68,10 +85,7 @@ impl ExactSizeIterator } } -impl core::iter::FusedIterator - for Drain<'_, Item, INLINE, Heap> -{ -} +impl FusedIterator for Drain<'_, Item, INLINE, Heap> {} impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Drop for Drain<'valid, Item, INLINE, Heap> @@ -96,7 +110,7 @@ impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Drop let ptr = source_vec.as_mut_ptr(); let source = ptr.add(tail); let destination = ptr.add(start); - core::ptr::copy(source, destination, self.0.tail_len); + copy(source, destination, self.0.tail_len); } source_vec.length.add(self.0.tail_len); } @@ -104,7 +118,7 @@ impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Drop } } - let iter = core::mem::take(&mut self.iter); + let iter = take(&mut self.iter); let drop_len = iter.len(); let mut vec = self.vec; @@ -152,8 +166,8 @@ impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Drop // is >= 1.87. let drop_offset = // drop_ptr.offset_from_unsigned(vec_ptr); let drop_offset = drop_ptr.offset_from(vec_ptr) as usize; - let to_drop = core::ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); - core::ptr::drop_in_place(to_drop); + let to_drop = slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); + to_drop.drop_in_place(); } } } @@ -217,7 +231,7 @@ impl Drain<'_, Item, INLINE, Heap> { let new_tail_start = self.tail_start + additional; unsafe { let ptr = vec.as_mut_ptr(); - core::ptr::copy( + copy( ptr.add(self.tail_start), ptr.add(new_tail_start), self.tail_len @@ -227,10 +241,8 @@ impl Drain<'_, Item, INLINE, Heap> { } } -impl core::fmt::Debug - for Drain<'_, Item, INLINE, Heap> -{ - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { +impl Debug for Drain<'_, Item, INLINE, Heap> { + fn fmt(&self, f: &mut Formatter<'_>) -> Format { f.debug_tuple("Drain").field(&self.iter.as_slice()).finish() } } diff --git a/src/iterators/extractif.rs b/src/iterators/extractif.rs index 82455b2f..e0ee1b5f 100644 --- a/src/iterators/extractif.rs +++ b/src/iterators/extractif.rs @@ -1,8 +1,18 @@ -use super::super::{ - SmallVec, - allocator::{ - Allocator, - global::Global +use { + super::super::{ + SmallVec, + allocator::{ + Allocator, + global::Global + } + }, + core::{ + fmt::{ + Debug, + Formatter, + Result as Format + }, + ptr::copy_nonoverlapping } }; @@ -29,13 +39,12 @@ where F: FnMut(&mut Item) -> bool pub(crate) pred: F } -impl core::fmt::Debug - for ExtractIf<'_, Item, INLINE, F, Heap> +impl Debug for ExtractIf<'_, Item, INLINE, F, Heap> where F: FnMut(&mut Item) -> bool, - Item: core::fmt::Debug + Item: Debug { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> Format { f.debug_tuple("ExtractIf") .field(&self.vec.as_ref()) .finish() @@ -62,10 +71,10 @@ where F: FnMut(&mut Item) -> bool self.idx += 1; if drained { self.del += 1; - return Some(core::ptr::read(cur)); + return Some(cur.read()); } else if self.del > 0 { // SAFETY: `self.del <= i` therefore `i - self.del` is valid - core::ptr::copy_nonoverlapping(cur, cur.sub(self.del), 1); + copy_nonoverlapping(cur, cur.sub(self.del), 1); } } None diff --git a/src/iterators/intoiter.rs b/src/iterators/intoiter.rs index fd296aa4..9fb266eb 100644 --- a/src/iterators/intoiter.rs +++ b/src/iterators/intoiter.rs @@ -10,13 +10,25 @@ use { rawsmallvec::RawSmallVec }, core::{ - fmt::Debug, + fmt::{ + Debug, + Formatter, + Result as Format + }, + iter::FusedIterator, mem::{ ManuallyDrop, align_of, size_of }, - ptr::NonNull + ptr::{ + NonNull, + slice_from_raw_parts_mut + }, + slice::{ + from_raw_parts, + from_raw_parts_mut + } } }; @@ -58,7 +70,7 @@ impl AsRef<[Item]> for IntoIter AsMut<[Item]> for IntoIter ExactSizeIterator for IntoIter { } -impl core::iter::FusedIterator - for IntoIter -{ -} +impl FusedIterator for IntoIter {} impl Drop for IntoIter { fn drop(&mut self) { @@ -173,7 +182,7 @@ impl Drop for IntoIter IntoIterator for SmallVec Debug for IntoIter { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> Format { f.debug_tuple("IntoIter").field(&self.as_ref()).finish() } } diff --git a/src/iterators/rayon.rs b/src/iterators/rayon.rs index ba60b566..6f6c4cee 100644 --- a/src/iterators/rayon.rs +++ b/src/iterators/rayon.rs @@ -8,11 +8,11 @@ use { }, core::{ mem::take, - ptr::{ - self, - drop_in_place - }, - slice + ptr::drop_in_place, + slice::{ + IterMut, + from_raw_parts_mut + } }, rayon::{ iter::plumbing::{ @@ -24,19 +24,21 @@ use { } }; -struct SliceDrain<'valid, Item>(slice::IterMut<'valid, Item>); +struct SliceDrain<'valid, Item>(IterMut<'valid, Item>); impl Iterator for SliceDrain<'_, Item> { type Item = Item; fn next(&mut self) -> Option { - self.0.next().map(|val| unsafe { ptr::read(val) }) + self.0.next().map(|val| unsafe { (&raw const *val).read() }) } } impl DoubleEndedIterator for SliceDrain<'_, Item> { fn next_back(&mut self) -> Option { - self.0.next_back().map(|val| unsafe { ptr::read(val) }) + self.0 + .next_back() + .map(|val| unsafe { (&raw const *val).read() }) } } @@ -94,7 +96,7 @@ impl ParallelIterator // SAFETY: set_len didn't deallocate/drop the elements, so they // are still valid. - slice::from_raw_parts_mut(self.as_mut_ptr(), length) + from_raw_parts_mut(self.as_mut_ptr(), length) }), consumer ) diff --git a/src/iterators/splice.rs b/src/iterators/splice.rs index e77be6ee..a08086dd 100644 --- a/src/iterators/splice.rs +++ b/src/iterators/splice.rs @@ -1,13 +1,20 @@ -use super::{ +use { super::{ - SmallVec, - allocator::{ - Allocator, - global::Global, - proxy::Proxy - } + super::{ + SmallVec, + allocator::{ + Allocator, + global::Global, + proxy::Proxy + } + }, + drain::Drain }, - drain::Drain + core::fmt::{ + Debug, + Formatter, + Result as Format + } }; pub struct Splice<'valid, I: Iterator + 'valid, const INLINE: usize, Heap: Allocator = Global> { @@ -26,13 +33,12 @@ impl<'valid, I: Iterator + 'valid, const INLINE: usize, Heap: Allocator> } } -impl<'valid, I, const INLINE: usize, Heap: Allocator> core::fmt::Debug - for Splice<'valid, I, INLINE, Heap> +impl<'valid, I, const INLINE: usize, Heap: Allocator> Debug for Splice<'valid, I, INLINE, Heap> where - I: core::fmt::Debug + Iterator + 'valid, - ::Item: core::fmt::Debug + I: Debug + Iterator + 'valid, + ::Item: Debug { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> Format { f.debug_tuple("Splice").field(&self.drain).finish() } } diff --git a/src/lib.rs b/src/lib.rs index 12cb5e4d..03558576 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,8 +35,16 @@ mod serde; #[cfg(feature = "specialization")] mod specialization; +#[cfg(feature = "arbitrary")] +use arbitrary::{ + Arbitrary, + Result as Random, + Unstructured, + size_hint::and +}; #[cfg(feature = "bytes")] use bytes::{ + Buf, BufMut, buf::UninitSlice }; @@ -46,8 +54,34 @@ use defmt::{ Formatter as DeFormatter, write as dewrite }; +#[cfg(feature = "encase")] +use encase::impl_rts_array; +#[cfg(feature = "proptest")] +use proptest::{ + arbitrary::Arbitrary as Proptest, + collection::{ + SizeRange, + VecStrategy, + vec as create_vec + }, + strategy::{ + Map as StrategyMap, + Strategy + } +}; +#[cfg(feature = "specialization")] +use specialization::{ + SpecCloneFrom, + SpecExtend, + SpecExtendFromWithin, + SpecFromElem, + SpecFromIterator +}; #[cfg(feature = "std")] -use std::io; +use std::io::{ + Result as IoResult, + Write +}; #[cfg(feature = "zeroize")] use zeroize::Zeroize; use { @@ -62,22 +96,43 @@ use { }, core::{ alloc::Layout, - fmt::Debug, + fmt::{ + Debug, + Formatter, + Result as Formatted + }, hash::{ Hash, Hasher }, + iter::{ + repeat_n, + repeat_with + }, mem::{ ManuallyDrop, MaybeUninit, align_of, + forget, size_of }, + ops::{ + Range, + RangeBounds + }, ptr::{ NonNull, copy, copy_nonoverlapping, - drop_in_place + drop_in_place, + slice_from_raw_parts_mut, + swap + }, + slice::{ + Iter, + IterMut, + from_raw_parts, + from_raw_parts_mut } }, newrange::NewRange @@ -159,7 +214,7 @@ impl SmallVec { } // `elements` have been moved into buf and will be dropped by SmallVec - core::mem::forget(elements); + forget(elements); // SAFETY: all the members in 0..S are initialized Self { @@ -187,10 +242,7 @@ impl SmallVec { // SAFETY: the values are initialized, so dropping them here is // fine. - core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut( - remainder_ptr, - remainder_len - )); + slice_from_raw_parts_mut(remainder_ptr, remainder_len).drop_in_place(); } vec @@ -432,19 +484,19 @@ impl SmallVec { } pub fn drain(&mut self, range: R) -> Drain<'_, Item, INLINE, Heap> - where R: core::ops::RangeBounds { + where R: RangeBounds { let length = self.len(); - let core::ops::Range { + let Range { start, end - } = core::ops::Range::new(range, length); + } = Range::new(range, length); unsafe { // SAFETY: `start <= length` self.set_len(start); // SAFETY: all the elements in `start..end` are initialized - let range_slice = core::slice::from_raw_parts(self.as_ptr().add(start), end - start); + let range_slice = from_raw_parts(self.as_ptr().add(start), end - start); // SAFETY: all the elements in `end..length` are initialized Drain { @@ -453,7 +505,7 @@ impl SmallVec { iter: range_slice.iter(), // Since self is a &mut, passing it to a function would invalidate the slice // iterator. - vec: core::ptr::NonNull::new_unchecked(self as *mut _) + vec: NonNull::new_unchecked(self as *mut _) } } } @@ -545,13 +597,13 @@ impl SmallVec { pub fn extract_if(&mut self, range: R, filter: F) -> ExtractIf<'_, Item, INLINE, F, Heap> where F: FnMut(&mut Item) -> bool, - R: core::ops::RangeBounds + R: RangeBounds { let old_len = self.len(); - let core::ops::Range { + let Range { start, end - } = core::ops::Range::new(range, old_len); + } = Range::new(range, old_len); // Guard against us getting leaked (leak amplification) unsafe { @@ -574,7 +626,7 @@ impl SmallVec { replace_with: I ) -> Splice<'_, I::IntoIter, INLINE, Heap> where - R: core::ops::RangeBounds, + R: RangeBounds, I: IntoIterator { Splice::new(self.drain(range), replace_with.into_iter()) @@ -804,7 +856,7 @@ impl SmallVec { // SAFETY: the vector is on the heap let capacity = unsafe { self.raw.heap.1 }; if capacity > min_capacity { - let target = core::cmp::max(length, min_capacity); + let target = length.max(min_capacity); if target <= Self::inline_size() { // SAFETY: on_heap is true, so we're on the heap unsafe { @@ -841,10 +893,8 @@ impl SmallVec { // then we drop the previously initialized elements unsafe { self.set_len(length); - core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut( - self.as_mut_ptr().add(length), - old_len - length - )) + slice_from_raw_parts_mut(self.as_mut_ptr().add(length), old_len - length) + .drop_in_place(); } } } @@ -868,9 +918,9 @@ impl SmallVec { // We replace self[index] with the last element. Note that if the // bounds check above succeeds there must be a last element (which // can be self[index] itself). - let value = core::ptr::read(self.as_ptr().add(index)); + let value = self.as_ptr().add(index).read(); let base_ptr = self.as_mut_ptr(); - core::ptr::copy(base_ptr.add(new_len), base_ptr.add(index), 1); + copy(base_ptr.add(new_len), base_ptr.add(index), 1); self.length.sub(1); value } @@ -883,10 +933,7 @@ impl SmallVec { unsafe { let old_len = self.len(); self.set_len(0); - core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut( - self.as_mut_ptr(), - old_len - )); + slice_from_raw_parts_mut(self.as_mut_ptr(), old_len).drop_in_place(); } } @@ -1141,7 +1188,7 @@ impl SmallVec { // Commit the length change and forget the guard. // SAFETY: `write` is always less than or equal to original_len. unsafe { g.v.set_len(g.write) }; - core::mem::forget(g); + forget(g); } #[inline] @@ -1179,7 +1226,7 @@ impl SmallVec { if !same_bucket(&mut *p_r, &mut *p_wm1) { if r != w { let p_w = p_wm1.add(1); - core::ptr::swap(p_r, p_w); + swap(p_r, p_w); } w += 1; } @@ -1193,7 +1240,7 @@ impl SmallVec { where F: FnMut() -> Item { let old_len = self.len(); if old_len < new_len { - self.extend(core::iter::repeat_with(f).take(new_len - old_len)); + self.extend(repeat_with(f).take(new_len - old_len)); } else if old_len > new_len { self.truncate(new_len); } @@ -1207,7 +1254,7 @@ impl SmallVec { ); } let mut me = ManuallyDrop::new(self); - unsafe { core::slice::from_raw_parts_mut(me.raw.as_mut_ptr(true), length) } + unsafe { from_raw_parts_mut(me.raw.as_mut_ptr(true), length) } } /// Returns the remaining spare capacity of the vector as a slice of @@ -1221,7 +1268,7 @@ impl SmallVec { let (length, on_heap) = self.length.parts(); unsafe { let capacity = self.raw.capacity(on_heap); - core::slice::from_raw_parts_mut( + from_raw_parts_mut( self.raw.as_mut_ptr(on_heap).add(length) as *mut MaybeUninit, capacity - length ) @@ -1308,11 +1355,11 @@ impl SmallVec { pub fn extend_from_within_copy(&mut self, source: R) where - R: core::ops::RangeBounds, + R: RangeBounds, Item: Copy { - let source = core::ops::Range::new(source, self.len()); - let core::ops::Range { + let source = Range::new(source, self.len()); + let Range { start, end } = source; @@ -1384,7 +1431,7 @@ impl SmallVec old_len { - self.extend(core::iter::repeat_n(value, length - old_len)); + self.extend(repeat_n(value, length - old_len)); } else { self.truncate(length); } @@ -1396,8 +1443,8 @@ impl SmallVec(&mut self, source: R) - where R: core::ops::RangeBounds { - let source = core::ops::Range::new(source, self.len()); + where R: RangeBounds { + let source = Range::new(source, self.len()); self.reserve(source.len()); // SAFETY: The call to `reserve` ensures that the capacity is large @@ -1406,9 +1453,7 @@ impl SmallVec>::spec_extend_from_within( - self, source - ); + >::spec_extend_from_within(self, source); } #[cfg(not(feature = "specialization"))] @@ -1460,7 +1505,7 @@ impl SmallVec Drop for DropGuard { #[inline] fn drop(&mut self) { unsafe { - core::ptr::slice_from_raw_parts_mut(self.ptr, self.length).drop_in_place(); + slice_from_raw_parts_mut(self.ptr, self.length).drop_in_place(); } } } @@ -1518,7 +1563,7 @@ impl Drop for SmallVec( // SAFETY: The precondition is checked in the initial comparison // above. unsafe { - - as specialization::SpecFromElem>::spec_from_elem(elem, n) + as SpecFromElem>::spec_from_elem(elem, n) } } @@ -1582,7 +1626,7 @@ impl SmallVec { ptr.add(i).write(elem.clone()); guard.length += 1; } - core::mem::forget(guard); + forget(guard); ptr.add(n - 1).write(elem); } } @@ -1630,7 +1674,7 @@ impl SmallVec { destination.add(i).write(val); guard.length += 1; } - core::mem::forget(guard); + forget(guard); } // SAFETY: The elements were initialized in the loop above. @@ -1653,7 +1697,7 @@ impl SmallVec { self.reserve(lower.saturating_add(1)); } unsafe { - core::ptr::write(self.as_mut_ptr().add(length), element); + self.as_mut_ptr().add(length).write(element); // Since next() executes user code which can panic we have to // bump the length after each step. // NB can't overflow since we would have had to alloc the @@ -1672,7 +1716,7 @@ impl SmallVec { /// `source.len()`. /// /// [`extend_from_within`]: SmallVec::extend_from_within - unsafe fn extend_from_within_fallback(&mut self, source: core::ops::Range) + unsafe fn extend_from_within_fallback(&mut self, source: Range) where Item: Clone { let old_len = self.len(); @@ -1696,7 +1740,7 @@ impl SmallVec { destination.add(i).write(val); guard.length += 1; } - core::mem::forget(guard); + forget(guard); } // SAFETY: The elements were initialized in the loop above. @@ -1742,7 +1786,7 @@ impl Clone fn clone_from(&mut self, source: &Self) { #[cfg(feature = "specialization")] { - >::spec_clone_from(self, source); + >::spec_clone_from(self, source); } #[cfg(not(feature = "specialization"))] @@ -1757,7 +1801,7 @@ impl Extend for SmallVec>(&mut self, iter: I) { #[cfg(feature = "specialization")] { - specialization::SpecExtend::::spec_extend(self, iter.into_iter()); + SpecExtend::::spec_extend(self, iter.into_iter()); } #[cfg(not(feature = "specialization"))] @@ -1774,7 +1818,7 @@ impl<'valid, Item: Clone + 'valid, const INLINE: usize, Heap: Allocator> Extend< fn extend>(&mut self, iter: I) { #[cfg(feature = "specialization")] { - specialization::SpecExtend::<&'valid Item, _>::spec_extend(self, iter.into_iter()); + SpecExtend::<&'valid Item, _>::spec_extend(self, iter.into_iter()); } #[cfg(not(feature = "specialization"))] @@ -1784,12 +1828,12 @@ impl<'valid, Item: Clone + 'valid, const INLINE: usize, Heap: Allocator> Extend< } } -impl core::iter::FromIterator for SmallVec { +impl FromIterator for SmallVec { #[inline] fn from_iter>(iter: I) -> Self { #[cfg(feature = "specialization")] { - specialization::SpecFromIterator::::spec_from_iter(iter.into_iter()) + SpecFromIterator::::spec_from_iter(iter.into_iter()) } #[cfg(not(feature = "specialization"))] @@ -1802,7 +1846,7 @@ impl core::iter::FromIterator for SmallVec IntoIterator for &'valid SmallVec { - type IntoIter = core::slice::Iter<'valid, Item>; + type IntoIter = Iter<'valid, Item>; type Item = &'valid Item; fn into_iter(self) -> Self::IntoIter { @@ -1813,7 +1857,7 @@ impl<'valid, Item, const INLINE: usize, Heap: Allocator> IntoIterator impl<'valid, Item, const INLINE: usize, Heap: Allocator> IntoIterator for &'valid mut SmallVec { - type IntoIter = core::slice::IterMut<'valid, Item>; + type IntoIter = IterMut<'valid, Item>; type Item = &'valid mut Item; fn into_iter(self) -> Self::IntoIter { @@ -1828,7 +1872,7 @@ impl Hash for SmallVec Debug for SmallVec { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> Formatted { f.debug_list().entries(self.iter()).finish() } } @@ -1851,58 +1895,53 @@ impl Zeroize for SmallVec arbitrary::Arbitrary<'valid> - for SmallVec -where Item: arbitrary::Arbitrary<'valid> +impl<'valid, Item, const INLINE: usize> Arbitrary<'valid> for SmallVec +where Item: Arbitrary<'valid> { - fn arbitrary(u: &mut arbitrary::Unstructured<'valid>) -> arbitrary::Result { + fn arbitrary(u: &mut Unstructured<'valid>) -> Random { u.arbitrary_iter()?.collect() } - fn arbitrary_take_rest(u: arbitrary::Unstructured<'valid>) -> arbitrary::Result { + fn arbitrary_take_rest(u: Unstructured<'valid>) -> Random { u.arbitrary_take_rest_iter()?.collect() } fn size_hint(depth: usize) -> (usize, Option) { - arbitrary::size_hint::and(::size_hint(depth), (0, None)) + and(::size_hint(depth), (0, None)) } } -#[cfg(all(feature = "proptest", not(feature = "allocator-api2")))] +#[cfg(feature = "proptest")] #[cfg_attr(docsrs, doc(cfg(feature = "proptest")))] -impl proptest::arbitrary::Arbitrary for SmallVec -where Item: proptest::arbitrary::Arbitrary + Debug +impl Proptest for SmallVec +where Item: Proptest + Debug { - type Parameters = proptest::collection::SizeRange; - type Strategy = proptest::strategy::Map< - proptest::collection::VecStrategy, - fn(std::vec::Vec) -> Self - >; + type Parameters = SizeRange; + type Strategy = StrategyMap, fn(std::vec::Vec) -> Self>; fn arbitrary_with(args: Self::Parameters) -> Self::Strategy { - use proptest::strategy::Strategy; - proptest::collection::vec(Item::arbitrary(), args) - .prop_map(|v: std::vec::Vec| SmallVec::from(v)) + create_vec(Item::arbitrary(), args) + .prop_map(|v: std::vec::Vec| SmallVec::from_iter(v.into_iter())) } } #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] -impl io::Write for SmallVec { +impl Write for SmallVec { #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result { + fn write(&mut self, buf: &[u8]) -> IoResult { self.extend_from_slice(buf); Ok(buf.len()) } #[inline] - fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + fn write_all(&mut self, buf: &[u8]) -> IoResult<()> { self.extend_from_slice(buf); Ok(()) } #[inline] - fn flush(&mut self) -> io::Result<()> { + fn flush(&mut self) -> IoResult<()> { Ok(()) } } @@ -1947,7 +1986,7 @@ unsafe impl BufMut for SmallVec(&mut self, mut source: Item) + fn put(&mut self, mut source: Item) where Self: Sized { // In case the source isn't contiguous, reserve upfront. self.reserve(source.remaining()); @@ -1982,7 +2021,7 @@ impl Format for SmallVec; (T, const INLINE: usize, Heap: Allocator); using len truncate diff --git a/src/references.rs b/src/references.rs index e3e7c079..a8d307d2 100644 --- a/src/references.rs +++ b/src/references.rs @@ -11,6 +11,10 @@ use { ops::{ Deref, DerefMut + }, + slice::{ + from_raw_parts, + from_raw_parts_mut } } }; @@ -36,7 +40,7 @@ impl AsRef<[Item]> for SmallVec &[Item] { let (length, on_heap) = self.length.parts(); // SAFETY: all the elements in `..length` are initialized - unsafe { core::slice::from_raw_parts(self.raw.as_ptr(on_heap), length) } + unsafe { from_raw_parts(self.raw.as_ptr(on_heap), length) } } } @@ -45,7 +49,7 @@ impl AsMut<[Item]> for SmallVec &mut [Item] { let (length, on_heap) = self.length.parts(); // SAFETY: see above - unsafe { core::slice::from_raw_parts_mut(self.raw.as_mut_ptr(on_heap), length) } + unsafe { from_raw_parts_mut(self.raw.as_mut_ptr(on_heap), length) } } } diff --git a/src/serde.rs b/src/serde.rs index c9f195fa..4855d0a2 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -6,11 +6,18 @@ use { global::Global } }, - core::marker::PhantomData, + core::{ + fmt::{ + Formatter, + Result as Format + }, + marker::PhantomData + }, serde_core::{ de::{ Deserialize, Deserializer, + Error, SeqAccess, Visitor }, @@ -53,12 +60,11 @@ impl<'de, Item: Deserialize<'de>, const INLINE: usize> Visitor<'de> { type Value = SmallVec; - fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + fn expecting(&self, formatter: &mut Formatter<'_>) -> Format { formatter.write_str("a sequence") } fn visit_seq>(self, mut seq: B) -> Result { - use serde_core::de::Error; let length = seq.size_hint().unwrap_or(0); let mut values = SmallVec::new(); values.try_reserve(length).map_err(B::Error::custom)?; diff --git a/src/specialization.rs b/src/specialization.rs index a9051444..525da0c4 100644 --- a/src/specialization.rs +++ b/src/specialization.rs @@ -8,7 +8,13 @@ use { }, iterators::intoiter::IntoIter }, - core::ptr::copy_nonoverlapping + core::{ + iter::TrustedLen, + mem::forget, + ops::Range, + ptr::copy_nonoverlapping, + slice::Iter + } }; /// A trait for specializing the implementation of [`from_elem`]. @@ -78,7 +84,7 @@ where I: Iterator impl SpecExtend for SmallVec -where I: core::iter::TrustedLen +where I: TrustedLen { fn spec_extend(&mut self, iter: I) { let (_, Some(additional)) = iter.size_hint() else { @@ -105,7 +111,7 @@ where I: core::iter::TrustedLen // The elements have been initialized in the loop above. self.set_len(length + guard.length); - core::mem::forget(guard); + forget(guard); } } } @@ -151,10 +157,10 @@ where } impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> - SpecExtend<&'valid Item, core::slice::Iter<'valid, Item>> for SmallVec + SpecExtend<&'valid Item, Iter<'valid, Item>> for SmallVec where Item: Copy { - fn spec_extend(&mut self, iter: core::slice::Iter<'valid, Item>) { + fn spec_extend(&mut self, iter: Iter<'valid, Item>) { let slice = iter.as_slice(); let length = slice.len(); let old_len = self.len(); @@ -189,13 +195,13 @@ pub trait SpecExtendFromWithin { /// `source.len()`. /// /// [`extend_from_within`]: SmallVec::extend_from_within - unsafe fn spec_extend_from_within(&mut self, source: core::ops::Range); + unsafe fn spec_extend_from_within(&mut self, source: Range); } impl SpecExtendFromWithin for SmallVec { - default unsafe fn spec_extend_from_within(&mut self, source: core::ops::Range) { + default unsafe fn spec_extend_from_within(&mut self, source: Range) { // SAFETY: Safety conditions are identical. unsafe { self.extend_from_within_fallback(source); @@ -206,7 +212,7 @@ impl SpecExtendFromWithin SpecExtendFromWithin for SmallVec { - unsafe fn spec_extend_from_within(&mut self, source: core::ops::Range) { + unsafe fn spec_extend_from_within(&mut self, source: Range) { let old_len = self.len(); let start = source.start; @@ -246,7 +252,7 @@ where I: Iterator } impl SpecFromIterator for SmallVec -where I: core::iter::TrustedLen +where I: TrustedLen { fn spec_from_iter(iter: I) -> Self { let mut v = match iter.size_hint() { diff --git a/tests/encase.rs b/tests/encase.rs index d871637f..d3d643f2 100644 --- a/tests/encase.rs +++ b/tests/encase.rs @@ -1,11 +1,14 @@ -use smallvec::SmallVec; +use { + encase::StorageBuffer, + smallvec::SmallVec +}; #[test] fn test_encase() { let v = SmallVec::::from([1.43_f32, 4.05, 4.55]); let mut buf = [255u8; 12]; - let mut s_buf = encase::StorageBuffer::new(&mut buf); + let mut s_buf = StorageBuffer::new(&mut buf); s_buf.write(&v).unwrap(); assert_eq!(&s_buf.as_ref()[0..][..4], &v[0].to_le_bytes()); diff --git a/tests/macro.rs b/tests/macro.rs index 913208a7..24074fa4 100644 --- a/tests/macro.rs +++ b/tests/macro.rs @@ -1,11 +1,17 @@ +#![allow(deprecated)] + +use smallvec::{ + SmallVec, + smallvec +}; + #[test] -#[allow(deprecated)] fn smallvec() { - let mut vec: smallvec::SmallVec; + let mut vec: SmallVec; macro_rules! check { ($init:tt) => { - vec = smallvec::smallvec! $init; + vec = smallvec! $init; assert_eq!(*vec, *vec! $init); } } diff --git a/tests/main.rs b/tests/main.rs index dc412596..4918fa92 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -19,12 +19,21 @@ use { Hash, Hasher }, - iter::FromIterator + iter::{ + FromIterator, + repeat_n, + repeat_with + }, + mem::forget }, smallvec::SmallVec, std::{ borrow::ToOwned, hash::DefaultHasher, + panic::{ + AssertUnwindSafe, + catch_unwind + }, rc::Rc } }; @@ -191,7 +200,7 @@ fn drain_rev() { #[test] fn drain_forget() { let mut v: SmallVec = SmallVec::from([0, 1, 2, 3, 4, 5, 6, 7]); - std::mem::forget(v.drain(2..5)); + forget(v.drain(2..5)); assert_eq!(v.len(), 2); } @@ -251,10 +260,10 @@ fn splice_reserve_panic() { let mut v: SmallVec>, 4> = SmallVec::with_capacity(capacity); v.push(Box::new(CountDrop(&drops))); - let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let result = catch_unwind(AssertUnwindSafe(|| { drop(v.splice( 0..0, - std::iter::repeat_with(|| Box::new(CountDrop(&drops))).take(additional) + repeat_with(|| Box::new(CountDrop(&drops))).take(additional) )); })); @@ -1127,7 +1136,7 @@ fn collect_from_iter() { const ELEMENTS: usize = 1000; #[cfg(not(miri))] const ELEMENTS: usize = 1_000_000; - let iter = IterNoHint(std::iter::repeat_n(1u8, ELEMENTS)); + let iter = IterNoHint(repeat_n(1u8, ELEMENTS)); let _y: SmallVec = SmallVec::from_iter(iter); }