From 8c44dad38c03d2afed6b48eb2f61d6e03c74ba77 Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Sat, 29 Aug 2026 16:01:35 +0200 Subject: [PATCH 1/3] fix: refined style --- rustfmt.toml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/rustfmt.toml b/rustfmt.toml index 5171db1..0235f78 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,4 +1,18 @@ wrap_comments = true -imports_granularity = "Preserve" +imports_granularity = "One" group_imports = "One" -format_code_in_doc_comments = true \ No newline at end of file +format_code_in_doc_comments = true +error_on_line_overflow = true +error_on_unformatted = true +blank_lines_lower_bound = 0 +blank_lines_upper_bound = 1 +float_literal_trailing_zero = "IfNoPostfix" +fn_single_line = true +imports_layout = "Vertical" +normalize_comments = true +reorder_impl_items = true +struct_lit_single_line = false +style_edition = "2024" +trailing_comma = "Never" +use_try_shorthand = true +where_single_line = true \ No newline at end of file From e83548fc4e147947cfbb4e729148854357df8cbc Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Sat, 29 Aug 2026 16:08:44 +0200 Subject: [PATCH 2/3] refactor: new style --- benches/bench.rs | 133 +++++----- src/lib.rs | 608 +++++++++++++++++++-------------------------- src/rawsmallvec.rs | 11 +- src/tests.rs | 129 +++++----- 4 files changed, 393 insertions(+), 488 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index e881130..3d3001e 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,9 +1,21 @@ #![allow(deprecated)] -use criterion::{criterion_group, criterion_main, Bencher, Criterion}; -use smallvec::{smallvec, SmallVec}; -use std::hint::black_box; -use std::time::Duration; +use { + criterion::{ + Bencher, + Criterion, + criterion_group, + criterion_main + }, + smallvec::{ + SmallVec, + smallvec + }, + std::{ + hint::black_box, + time::Duration + } +}; const VEC_SIZE: usize = 16; const SPILLED_SIZE: usize = 100; @@ -18,72 +30,51 @@ trait Vector: for<'a> From<&'a [T]> + Extend { fn from_elems(val: &[T]) -> Self; fn extend_from_slice(&mut self, other: &[T]); fn retain_mut(&mut self, f: F) - where - F: FnMut(&mut T) -> bool; + where F: FnMut(&mut T) -> bool; } impl Vector for Vec { - fn new() -> Self { - Self::with_capacity(VEC_SIZE) - } - fn push(&mut self, val: T) { - self.push(val) - } - fn pop(&mut self) -> Option { - self.pop() - } - fn remove(&mut self, p: usize) -> T { - self.remove(p) - } - fn insert(&mut self, n: usize, val: T) { - self.insert(n, val) - } - fn from_elem(val: T, n: usize) -> Self { - vec![val; n] - } - fn from_elems(val: &[T]) -> Self { - val.to_owned() - } - fn extend_from_slice(&mut self, other: &[T]) { - Vec::extend_from_slice(self, other) - } + fn new() -> Self { Self::with_capacity(VEC_SIZE) } + + fn push(&mut self, val: T) { self.push(val) } + + fn pop(&mut self) -> Option { self.pop() } + + fn remove(&mut self, p: usize) -> T { self.remove(p) } + + fn insert(&mut self, n: usize, val: T) { self.insert(n, val) } + + fn from_elem(val: T, n: usize) -> Self { vec![val; n] } + + fn from_elems(val: &[T]) -> Self { val.to_owned() } + + fn extend_from_slice(&mut self, other: &[T]) { Vec::extend_from_slice(self, other) } + fn retain_mut(&mut self, f: F) - where - F: FnMut(&mut T) -> bool, - { + where F: FnMut(&mut T) -> bool { self.retain_mut(f) } } impl Vector for SmallVec { - fn new() -> Self { - Self::new() - } - fn push(&mut self, val: T) { - self.push(val) - } - fn pop(&mut self) -> Option { - self.pop() - } - fn remove(&mut self, p: usize) -> T { - self.remove(p) - } - fn insert(&mut self, n: usize, val: T) { - self.insert(n, val) - } - fn from_elem(val: T, n: usize) -> Self { - smallvec![val; n] - } - fn from_elems(val: &[T]) -> Self { - SmallVec::from(val) - } - fn extend_from_slice(&mut self, other: &[T]) { - SmallVec::extend_from_slice(self, other) - } + fn new() -> Self { Self::new() } + + fn push(&mut self, val: T) { self.push(val) } + + fn pop(&mut self) -> Option { self.pop() } + + fn remove(&mut self, p: usize) -> T { self.remove(p) } + + fn insert(&mut self, n: usize, val: T) { self.insert(n, val) } + + fn from_elem(val: T, n: usize) -> Self { smallvec![val; n] } + + fn from_elems(val: &[T]) -> Self { SmallVec::from(val) } + + fn extend_from_slice(&mut self, other: &[T]) { SmallVec::extend_from_slice(self, other) } + fn retain_mut(&mut self, f: F) - where - F: FnMut(&mut T) -> bool, - { + where F: FnMut(&mut T) -> bool { self.retain_mut(f) } } @@ -100,8 +91,8 @@ macro_rules! make_benches { } } -/* ---------- Bench generation (same list, just using the new macro) - * ---------- */ +// ---------- Bench generation (same list, just using the new macro) +// ---------- make_benches! { SmallVec { bench_push => gen_push(SPILLED_SIZE as _), @@ -168,9 +159,7 @@ make_benches! { fn gen_push>(n: u64, b: &mut Bencher) { #[inline(never)] - fn push_noinline>(vec: &mut V, x: u64) { - vec.push(black_box(x)); - } + fn push_noinline>(vec: &mut V, x: u64) { vec.push(black_box(x)); } b.iter(|| { let n = black_box(n); @@ -216,15 +205,13 @@ fn gen_insert>(n: u64, b: &mut Bencher) { insert_noinline(&mut vec, 0, x); } vec - }, + } ); } fn gen_remove>(n: usize, b: &mut Bencher) { #[inline(never)] - fn remove_noinline>(vec: &mut V, p: usize) -> u64 { - vec.remove(black_box(p)) - } + fn remove_noinline>(vec: &mut V, p: usize) -> u64 { vec.remove(black_box(p)) } b.iter_with_setup( || V::from_elem(0, black_box(n)), @@ -233,7 +220,7 @@ fn gen_remove>(n: usize, b: &mut Bencher) { black_box(remove_noinline(&mut vec, 0)); } vec - }, + } ); } @@ -309,7 +296,7 @@ fn gen_retain_mut_half>(n: usize, b: &mut Bencher) { |mut vec| { vec.retain_mut(|x| black_box(*x) % 2 == 0); vec - }, + } ); } @@ -319,7 +306,7 @@ fn gen_retain_mut_all>(n: usize, b: &mut Bencher) { |mut vec| { vec.retain_mut(|_| true); vec - }, + } ); } @@ -329,7 +316,7 @@ fn gen_retain_mut_none>(n: usize, b: &mut Bencher) { |mut vec| { vec.retain_mut(|_| false); vec - }, + } ); } diff --git a/src/lib.rs b/src/lib.rs index 5018c7b..0726579 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,37 +69,68 @@ mod rawsmallvec; #[cfg(test)] mod tests; -use alloc::alloc::Layout; -use alloc::boxed::Box; -use alloc::vec; -use alloc::vec::Vec; #[cfg(feature = "bytes")] -use bytes::{buf::UninitSlice, BufMut}; -use core::borrow::Borrow; -use core::borrow::BorrowMut; -use core::fmt::Debug; -use core::hash::{Hash, Hasher}; -use core::marker::PhantomData; -use core::mem::align_of; -use core::mem::size_of; -use core::mem::ManuallyDrop; -use core::mem::MaybeUninit; -use core::ptr::copy; -use core::ptr::copy_nonoverlapping; -use core::ptr::NonNull; +use bytes::{ + BufMut, + buf::UninitSlice +}; #[cfg(feature = "malloc_size_of")] -use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; +use malloc_size_of::{ + MallocShallowSizeOf, + MallocSizeOf, + MallocSizeOfOps +}; #[cfg(feature = "internals")] pub use rawsmallvec::RawSmallVec; #[cfg(not(feature = "internals"))] use rawsmallvec::RawSmallVec; #[cfg(feature = "serde")] use serde_core::{ - de::{Deserialize, Deserializer, SeqAccess, Visitor}, - ser::{Serialize, SerializeSeq, Serializer}, + de::{ + Deserialize, + Deserializer, + SeqAccess, + Visitor + }, + ser::{ + Serialize, + SerializeSeq, + Serializer + } }; #[cfg(feature = "std")] use std::io; +use { + alloc::{ + alloc::Layout, + boxed::Box, + vec::Vec + }, + core::{ + borrow::{ + Borrow, + BorrowMut + }, + fmt::Debug, + hash::{ + Hash, + Hasher + }, + marker::PhantomData, + mem::{ + ManuallyDrop, + MaybeUninit, + align_of, + size_of + }, + ptr::{ + NonNull, + copy, + copy_nonoverlapping + }, + iter::repeat_n + } +}; /// Error type for APIs with fallible heap allocation #[derive(Debug)] @@ -109,8 +140,8 @@ pub enum CollectionAllocErr { /// The allocator return an error AllocErr { /// The layout that was passed to the allocator - layout: Layout, - }, + layout: Layout + } } impl core::fmt::Display for CollectionAllocErr { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -125,23 +156,21 @@ fn infallible(result: Result) -> T { match result { Ok(x) => x, Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), - Err(CollectionAllocErr::AllocErr { layout }) => alloc::alloc::handle_alloc_error(layout), + Err(CollectionAllocErr::AllocErr { + layout + }) => alloc::alloc::handle_alloc_error(layout) } } /// Helper function to check if a type is a ZST. #[inline] -const fn is_zst() -> bool { - const { size_of::() == 0 } -} +const fn is_zst() -> bool { const { size_of::() == 0 } } #[inline] /// A local copy of [`core::slice::range`]. The latter function is unstable /// and thus cannot be used yet. fn slice_range(range: R, bounds: core::ops::RangeTo) -> core::ops::Range -where - R: core::ops::RangeBounds, -{ +where R: core::ops::RangeBounds { let len = bounds.end; let start = match range.start_bound() { @@ -149,7 +178,7 @@ where core::ops::Bound::Excluded(start) => start .checked_add(1) .unwrap_or_else(|| panic!("attempted to index slice from after maximum usize")), - core::ops::Bound::Unbounded => 0, + core::ops::Bound::Unbounded => 0 }; let end = match range.end_bound() { @@ -157,7 +186,7 @@ where .checked_add(1) .unwrap_or_else(|| panic!("attempted to index slice up to maximum usize")), core::ops::Bound::Excluded(&end) => end, - core::ops::Bound::Unbounded => len, + core::ops::Bound::Unbounded => len }; if start > end { @@ -167,26 +196,29 @@ where panic!("range end index {end} out of range for slice of length {len}"); } - core::ops::Range { start, end } + core::ops::Range { + start, + end + } } impl RawSmallVec { const IS_ZST: bool = is_zst::(); #[inline] - const fn new() -> Self { - Self::new_inline(MaybeUninit::uninit()) - } + const fn new() -> Self { Self::new_inline(MaybeUninit::uninit()) } + #[inline] const fn new_inline(inline: MaybeUninit<[T; N]>) -> Self { Self { - inline: ManuallyDrop::new(inline), + inline: ManuallyDrop::new(inline) } } + #[inline] const fn new_heap(ptr: NonNull, capacity: usize) -> Self { Self { - heap: (ptr, capacity), + heap: (ptr, capacity) } } @@ -208,17 +240,13 @@ impl RawSmallVec { /// /// The vector must be on the heap #[inline] - const unsafe fn as_ptr_heap(&self) -> *const T { - self.heap.0.as_ptr() - } + const unsafe fn as_ptr_heap(&self) -> *const T { self.heap.0.as_ptr() } /// # Safety /// /// The vector must be on the heap #[inline] - const unsafe fn as_mut_ptr_heap(&mut self) -> *mut T { - self.heap.0.as_ptr() - } + const unsafe fn as_mut_ptr_heap(&mut self) -> *mut T { self.heap.0.as_ptr() } /// # Safety /// @@ -227,9 +255,12 @@ impl RawSmallVec { unsafe fn try_grow_raw( &mut self, len: TaggedLen, - new_capacity: usize, + new_capacity: usize ) -> Result<(), CollectionAllocErr> { - use alloc::alloc::{alloc, realloc}; + use alloc::alloc::{ + alloc, + realloc + }; debug_assert!(!Self::IS_ZST); debug_assert!(new_capacity > 0); debug_assert!(new_capacity >= len.value()); @@ -251,8 +282,9 @@ impl RawSmallVec { let new_ptr = if !was_on_heap { // get a fresh allocation let new_ptr = alloc(new_layout) as *mut T; // `new_layout` has nonzero size. - let new_ptr = - NonNull::new(new_ptr).ok_or(CollectionAllocErr::AllocErr { layout: new_layout })?; + let new_ptr = NonNull::new(new_ptr).ok_or(CollectionAllocErr::AllocErr { + layout: new_layout + })?; copy_nonoverlapping(ptr, new_ptr.as_ptr(), len); new_ptr } else { @@ -269,7 +301,9 @@ impl RawSmallVec { // does not overflow when rounded up to alignment. since it was constructed // with Layout::array let new_ptr = realloc(ptr as *mut u8, old_layout, new_layout.size()) as *mut T; - NonNull::new(new_ptr).ok_or(CollectionAllocErr::AllocErr { layout: new_layout })? + NonNull::new(new_ptr).ok_or(CollectionAllocErr::AllocErr { + layout: new_layout + })? }; *self = Self::new_heap(new_ptr, new_capacity); Ok(()) @@ -292,20 +326,17 @@ struct TaggedLen(usize, PhantomData); // with the derive attribute implementations. impl Clone for TaggedLen { #[inline] - fn clone(&self) -> Self { - Self(self.0, PhantomData) - } + fn clone(&self) -> Self { Self(self.0, PhantomData) } #[inline] - fn clone_from(&mut self, source: &Self) { - self.0 = source.0; - } + fn clone_from(&mut self, source: &Self) { self.0 = source.0; } } impl Copy for TaggedLen {} impl TaggedLen { const IS_ZST: bool = is_zst::(); + #[inline] pub const fn new(len: usize, on_heap: bool) -> Self { if Self::IS_ZST { @@ -328,20 +359,14 @@ impl TaggedLen { } #[inline] - pub const fn value(self) -> usize { - if Self::IS_ZST { - self.0 - } else { - self.0 >> 1 - } - } + pub const fn value(self) -> usize { if Self::IS_ZST { self.0 } else { self.0 >> 1 } } } #[repr(C)] pub struct SmallVec { len: TaggedLen, raw: RawSmallVec, - _marker: PhantomData, + _marker: PhantomData } unsafe impl Send for SmallVec {} @@ -349,9 +374,7 @@ unsafe impl Sync for SmallVec {} impl Default for SmallVec { #[inline] - fn default() -> Self { - Self::new() - } + fn default() -> Self { Self::new() } } /// An iterator that removes the items from a `SmallVec` and yields them by @@ -371,7 +394,7 @@ pub struct Drain<'a, T: 'a, const N: usize> { tail_start: usize, tail_len: usize, iter: core::slice::Iter<'a, T>, - vec: core::ptr::NonNull>, + vec: core::ptr::NonNull> } impl<'a, T: 'a, const N: usize> Iterator for Drain<'a, T, N> { @@ -387,9 +410,7 @@ impl<'a, T: 'a, const N: usize> Iterator for Drain<'a, T, N> { } #[inline] - fn size_hint(&self) -> (usize, Option) { - self.iter.size_hint() - } + fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, T: 'a, const N: usize> DoubleEndedIterator for Drain<'a, T, N> { @@ -404,9 +425,7 @@ impl<'a, T: 'a, const N: usize> DoubleEndedIterator for Drain<'a, T, N> { impl ExactSizeIterator for Drain<'_, T, N> { #[inline] - fn len(&self) -> usize { - self.iter.len() - } + fn len(&self) -> usize { self.iter.len() } } impl core::iter::FusedIterator for Drain<'_, T, N> {} @@ -477,7 +496,7 @@ impl<'a, T: 'a, const N: usize> Drop for Drain<'a, T, N> { // raw pointers to it which some unsafe code might rely on. let vec_ptr = vec.as_mut().as_mut_ptr(); // May be replaced with the line below later, once this crate's MSRV is >= 1.87. - //let drop_offset = drop_ptr.offset_from_unsigned(vec_ptr); + // 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); @@ -487,9 +506,7 @@ impl<'a, T: 'a, const N: usize> Drop for Drain<'a, T, N> { impl Drain<'_, T, N> { #[must_use] - pub fn as_slice(&self) -> &[T] { - self.iter.as_slice() - } + pub fn as_slice(&self) -> &[T] { self.iter.as_slice() } /// The range from `self.vec.len` to `self.tail_start` contains elements /// that have been moved out. @@ -503,7 +520,7 @@ impl Drain<'_, T, N> { let range_slice = unsafe { core::slice::from_raw_parts_mut( vec.as_mut_ptr().add(range_start), - range_end - range_start, + range_end - range_start ) }; @@ -547,8 +564,7 @@ impl Drain<'_, T, N> { /// /// [1]: struct.SmallVec.html#method.extract_if pub struct ExtractIf<'a, T, const N: usize, F> -where - F: FnMut(&mut T) -> bool, +where F: FnMut(&mut T) -> bool { vec: &'a mut SmallVec, /// The index of the item that will be inspected by the next call to `next`. @@ -561,13 +577,13 @@ where /// The original length of `vec` prior to draining. old_len: usize, /// The filter test predicate. - pred: F, + pred: F } impl core::fmt::Debug for ExtractIf<'_, T, N, F> where F: FnMut(&mut T) -> bool, - T: core::fmt::Debug, + T: core::fmt::Debug { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_tuple("ExtractIf") @@ -577,8 +593,7 @@ where } impl Iterator for ExtractIf<'_, T, N, F> -where - F: FnMut(&mut T) -> bool, +where F: FnMut(&mut T) -> bool { type Item = T; @@ -606,14 +621,11 @@ where } } - fn size_hint(&self) -> (usize, Option) { - (0, Some(self.end - self.idx)) - } + fn size_hint(&self) -> (usize, Option) { (0, Some(self.end - self.idx)) } } impl Drop for ExtractIf<'_, T, N, F> -where - F: FnMut(&mut T) -> bool, +where F: FnMut(&mut T) -> bool { fn drop(&mut self) { unsafe { @@ -637,13 +649,13 @@ where pub struct Splice<'a, I: Iterator + 'a, const N: usize> { drain: Drain<'a, I::Item, N>, - replace_with: I, + replace_with: I } impl<'a, I, const N: usize> core::fmt::Debug for Splice<'a, I, N> where I: Debug + Iterator + 'a, - ::Item: Debug, + ::Item: Debug { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_tuple("Splice").field(&self.drain).finish() @@ -653,19 +665,13 @@ where impl Iterator for Splice<'_, I, N> { type Item = I::Item; - fn next(&mut self) -> Option { - self.drain.next() - } + fn next(&mut self) -> Option { self.drain.next() } - fn size_hint(&self) -> (usize, Option) { - self.drain.size_hint() - } + fn size_hint(&self) -> (usize, Option) { self.drain.size_hint() } } impl DoubleEndedIterator for Splice<'_, I, N> { - fn next_back(&mut self) -> Option { - self.drain.next_back() - } + fn next_back(&mut self) -> Option { self.drain.next_back() } } impl ExactSizeIterator for Splice<'_, I, N> {} @@ -734,7 +740,7 @@ pub struct IntoIter { raw: RawSmallVec, begin: usize, end: TaggedLen, - _marker: PhantomData, + _marker: PhantomData } // SAFETY: IntoIter has unique ownership of its contents. Sending (or sharing) @@ -838,7 +844,7 @@ impl SmallVec { Self { len: TaggedLen::new(0, false), raw: RawSmallVec::new(), - _marker: PhantomData, + _marker: PhantomData } } @@ -876,7 +882,7 @@ impl SmallVec { Self { len: TaggedLen::new(S, false), raw: RawSmallVec::new_inline(buf), - _marker: PhantomData, + _marker: PhantomData } } @@ -887,7 +893,7 @@ impl SmallVec { let mut vec = Self { len: TaggedLen::new(len, false), raw: RawSmallVec::new_inline(MaybeUninit::new(buf)), - _marker: PhantomData, + _marker: PhantomData }; // Deallocate the remaining elements so no memory is leaked. unsafe { @@ -899,7 +905,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, + remainder_len )); } @@ -913,8 +919,10 @@ impl SmallVec { /// # Examples /// /// ``` - /// use smallvec::SmallVec; - /// use std::mem::MaybeUninit; + /// use { + /// smallvec::SmallVec, + /// std::mem::MaybeUninit + /// }; /// /// let buf = [1, 2, 3, 4, 5, 0, 0, 0]; /// let small_vec = unsafe { SmallVec::from_buf_and_len_unchecked(MaybeUninit::new(buf), 5) }; @@ -931,7 +939,7 @@ impl SmallVec { Self { len: TaggedLen::new(len, false), raw: RawSmallVec::new_inline(buf), - _marker: PhantomData, + _marker: PhantomData } } } @@ -959,7 +967,7 @@ impl SmallVec { Self { len: TaggedLen::new(len, false), raw: RawSmallVec::new(), - _marker: PhantomData, + _marker: PhantomData } } else { let mut vec = ManuallyDrop::new(vec); @@ -972,7 +980,7 @@ impl SmallVec { Self { len: TaggedLen::new(len, true), raw: RawSmallVec::new_heap(ptr, cap), - _marker: PhantomData, + _marker: PhantomData } } } @@ -983,9 +991,7 @@ impl SmallVec { /// /// 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); - } + unsafe fn set_on_heap(&mut self) { self.len = TaggedLen::new(self.len(), true); } /// Sets the tag to be inline /// @@ -993,9 +999,7 @@ impl SmallVec { /// /// The active union member must be the self.raw.inline #[inline] - unsafe fn set_inline(&mut self) { - self.len = TaggedLen::new(self.len(), false); - } + unsafe fn set_inline(&mut self) { self.len = TaggedLen::new(self.len(), false); } /// Sets the length of a vector. /// @@ -1015,24 +1019,14 @@ impl SmallVec { } #[inline] - pub const fn inline_size() -> usize { - if Self::IS_ZST { - usize::MAX - } else { - N - } - } + pub const fn inline_size() -> usize { if Self::IS_ZST { usize::MAX } else { N } } #[inline] - pub const fn len(&self) -> usize { - self.len.value() - } + pub const fn len(&self) -> usize { self.len.value() } #[must_use] #[inline] - pub const fn is_empty(&self) -> bool { - self.len() == 0 - } + pub const fn is_empty(&self) -> bool { self.len() == 0 } #[inline] pub const fn capacity(&self) -> usize { @@ -1045,9 +1039,7 @@ impl SmallVec { } #[inline] - pub const fn spilled(&self) -> bool { - self.len.on_heap() - } + pub const fn spilled(&self) -> bool { self.len.on_heap() } /// Splits the collection into two at the given index. /// @@ -1094,11 +1086,12 @@ impl SmallVec { } pub fn drain(&mut self, range: R) -> Drain<'_, T, N> - where - R: core::ops::RangeBounds, - { + where R: core::ops::RangeBounds { let len = self.len(); - let core::ops::Range { start, end } = slice_range(range, ..len); + let core::ops::Range { + start, + end + } = slice_range(range, ..len); unsafe { // SAFETY: `start <= len` @@ -1114,8 +1107,8 @@ 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: core::ptr::NonNull::from(self), + vec: core::ptr::NonNull::new_unchecked(self as *mut _) + // vec: core::ptr::NonNull::from(self), } } } @@ -1207,10 +1200,13 @@ impl SmallVec { pub fn extract_if(&mut self, range: R, filter: F) -> ExtractIf<'_, T, N, F> where F: FnMut(&mut T) -> bool, - R: core::ops::RangeBounds, + R: core::ops::RangeBounds { let old_len = self.len(); - let core::ops::Range { start, end } = slice_range(range, ..old_len); + let core::ops::Range { + start, + end + } = slice_range(range, ..old_len); // Guard against us getting leaked (leak amplification) unsafe { @@ -1223,25 +1219,23 @@ impl SmallVec { end, del: 0, old_len, - pred: filter, + pred: filter } } pub fn splice(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, N> where R: core::ops::RangeBounds, - I: IntoIterator, + I: IntoIterator { Splice { drain: self.drain(range), - replace_with: replace_with.into_iter(), + replace_with: replace_with.into_iter() } } #[inline] - pub fn push(&mut self, value: T) { - _ = self.push_mut(value); - } + pub fn push(&mut self, value: T) { _ = self.push_mut(value); } #[inline] #[must_use] @@ -1293,11 +1287,7 @@ impl SmallVec { #[inline] pub fn pop_if(&mut self, predicate: impl FnOnce(&mut T) -> bool) -> Option { let last = self.last_mut()?; - if predicate(last) { - self.pop() - } else { - None - } + if predicate(last) { self.pop() } else { None } } #[inline] @@ -1321,9 +1311,7 @@ impl SmallVec { } #[inline] - pub fn grow(&mut self, new_capacity: usize) { - infallible(self.try_grow(new_capacity)); - } + pub fn grow(&mut self, new_capacity: usize) { infallible(self.try_grow(new_capacity)); } #[cold] pub fn try_grow(&mut self, new_capacity: usize) -> Result<(), CollectionAllocErr> { @@ -1357,7 +1345,7 @@ impl SmallVec { drop(DropDealloc { ptr: ptr.cast(), size_bytes: old_cap * size_of::(), - align: align_of::(), + align: align_of::() }); self.set_inline(); } @@ -1374,7 +1362,7 @@ impl SmallVec { self.len() .checked_add(additional) .and_then(usize::checked_next_power_of_two) - .ok_or(CollectionAllocErr::CapacityOverflow), + .ok_or(CollectionAllocErr::CapacityOverflow) ); self.grow(new_capacity); } @@ -1401,7 +1389,7 @@ impl SmallVec { let new_capacity = infallible( self.len() .checked_add(additional) - .ok_or(CollectionAllocErr::CapacityOverflow), + .ok_or(CollectionAllocErr::CapacityOverflow) ); self.grow(new_capacity); } @@ -1435,7 +1423,7 @@ impl SmallVec { self.set_inline(); alloc::alloc::dealloc( ptr.cast().as_ptr(), - Layout::from_size_align_unchecked(capacity * size_of::(), align_of::()), + Layout::from_size_align_unchecked(capacity * size_of::(), align_of::()) ); } } else if len < self.capacity() { @@ -1465,8 +1453,8 @@ impl SmallVec { ptr.cast().as_ptr(), Layout::from_size_align_unchecked( capacity * size_of::(), - align_of::(), - ), + align_of::() + ) ); } } else if target < self.capacity() { @@ -1488,7 +1476,7 @@ impl SmallVec { self.set_len(len); core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut( self.as_mut_ptr().add(len), - old_len - len, + old_len - len )) } } @@ -1524,7 +1512,7 @@ impl SmallVec { self.set_len(0); core::ptr::drop_in_place(core::ptr::slice_from_raw_parts_mut( self.as_mut_ptr(), - old_len, + old_len )); } } @@ -1550,9 +1538,7 @@ impl SmallVec { } #[inline] - pub fn insert(&mut self, index: usize, value: T) { - _ = self.insert_mut(index, value); - } + pub fn insert(&mut self, index: usize, value: T) { _ = self.insert_mut(index, value); } #[inline] #[must_use] @@ -1663,9 +1649,7 @@ impl SmallVec { } #[inline] - pub fn into_boxed_slice(self) -> Box<[T]> { - self.into_vec().into_boxed_slice() - } + pub fn into_boxed_slice(self) -> Box<[T]> { self.into_vec().into_boxed_slice() } #[inline] #[deprecated( @@ -1689,9 +1673,7 @@ impl SmallVec { } #[inline] - pub fn retain bool>(&mut self, mut f: F) { - self.retain_mut(|elem| f(elem)) - } + pub fn retain bool>(&mut self, mut f: F) { self.retain_mut(|elem| f(elem)) } #[inline] pub fn retain_mut bool>(&mut self, mut f: F) { @@ -1721,9 +1703,7 @@ impl SmallVec { #[inline] pub fn dedup(&mut self) - where - T: PartialEq, - { + where T: PartialEq { self.dedup_by(|a, b| a == b); } @@ -1731,16 +1711,14 @@ impl SmallVec { pub fn dedup_by_key(&mut self, mut key: F) where F: FnMut(&mut T) -> K, - K: PartialEq, + K: PartialEq { self.dedup_by(|a, b| key(a) == key(b)); } #[inline] pub fn dedup_by(&mut self, mut same_bucket: F) - where - F: FnMut(&mut T, &mut T) -> bool, - { + where F: FnMut(&mut T, &mut T) -> bool { // See the implementation of Vec::dedup_by in the // standard library for an explanation of this algorithm. let len = self.len(); @@ -1769,9 +1747,7 @@ impl SmallVec { } pub fn resize_with(&mut self, new_len: usize, f: F) - where - F: FnMut() -> T, - { + where F: FnMut() -> T { let old_len = self.len(); if old_len < new_len { let mut f = f; @@ -1806,7 +1782,7 @@ impl SmallVec { unsafe { core::slice::from_raw_parts_mut( self.as_mut_ptr().add(self.len()) as *mut MaybeUninit, - self.capacity() - self.len(), + self.capacity() - self.len() ) } } @@ -1843,7 +1819,10 @@ impl SmallVec { /// # Examples /// /// ``` - /// use smallvec::{smallvec, SmallVec}; + /// use smallvec::{ + /// SmallVec, + /// smallvec + /// }; /// /// let mut v: SmallVec<_, 1> = smallvec![1, 2, 3]; /// @@ -1888,7 +1867,7 @@ impl SmallVec { SmallVec { len: TaggedLen::new(length, true), raw: RawSmallVec::new_heap(ptr, capacity), - _marker: PhantomData, + _marker: PhantomData } } } @@ -1905,14 +1884,10 @@ impl SmallVec { } #[inline] - pub fn extend_from_slice(&mut self, other: &[T]) { - self.extend(other.iter()) - } + pub fn extend_from_slice(&mut self, other: &[T]) { self.extend(other.iter()) } pub fn extend_from_within(&mut self, src: R) - where - R: core::ops::RangeBounds, - { + where R: core::ops::RangeBounds { let src = slice_range(src, ..self.len()); self.reserve(src.len()); @@ -1933,9 +1908,7 @@ impl SmallVec { #[inline] pub fn extend_from_slice_copy(&mut self, other: &[T]) - where - T: Copy, - { + where T: Copy { let len = other.len(); let src = other.as_ptr(); @@ -1954,10 +1927,13 @@ impl SmallVec { pub fn extend_from_within_copy(&mut self, src: R) where R: core::ops::RangeBounds, - T: Copy, + T: Copy { let src = slice_range(src, ..self.len()); - let core::ops::Range { start, end } = src; + let core::ops::Range { + start, + end + } = src; let len = end - start; self.reserve(len); @@ -1972,9 +1948,7 @@ impl SmallVec { } pub fn insert_from_slice_copy(&mut self, index: usize, other: &[T]) - where - T: Copy, - { + where T: Copy { let l = self.len(); let len = other.len(); assert!(index <= l); @@ -1996,9 +1970,7 @@ impl SmallVec { /// A function for creating [`SmallVec`] values out of slices /// for types with the [`Copy`] trait. pub fn from_slice_copy(slice: &[T]) -> Self - where - T: Copy, - { + where T: Copy { let src = slice.as_ptr(); let len = slice.len(); let mut result = Self::with_capacity(len); @@ -2016,7 +1988,7 @@ impl SmallVec { struct DropGuard { ptr: *mut T, - len: usize, + len: usize } impl Drop for DropGuard { #[inline] @@ -2030,7 +2002,7 @@ impl Drop for DropGuard { struct DropDealloc { ptr: NonNull, size_bytes: usize, - align: usize, + align: usize } impl Drop for DropDealloc { @@ -2040,7 +2012,7 @@ impl Drop for DropDealloc { if self.size_bytes > 0 { alloc::alloc::dealloc( self.ptr.as_ptr(), - Layout::from_size_align_unchecked(self.size_bytes, self.align), + Layout::from_size_align_unchecked(self.size_bytes, self.align) ); } } @@ -2061,7 +2033,7 @@ unsafe impl<#[may_dangle] T, const N: usize> Drop for SmallVec { Some(DropDealloc { ptr: NonNull::new_unchecked(ptr as *mut u8), size_bytes: capacity * size_of::(), - align: align_of::(), + align: align_of::() }) } else { None @@ -2084,7 +2056,7 @@ impl Drop for SmallVec { Some(DropDealloc { ptr: NonNull::new_unchecked(ptr as *mut u8), size_bytes: capacity * size_of::(), - align: align_of::(), + align: align_of::() }) } else { None @@ -2107,7 +2079,7 @@ impl Drop for IntoIter { Some(DropDealloc { ptr: NonNull::new_unchecked(ptr as *mut u8), size_bytes: capacity * size_of::(), - align: align_of::(), + align: align_of::() }) } else { None @@ -2121,15 +2093,11 @@ impl core::ops::Deref for SmallVec { type Target = [T]; #[inline] - fn deref(&self) -> &Self::Target { - self.as_slice() - } + fn deref(&self) -> &Self::Target { self.as_slice() } } impl core::ops::DerefMut for SmallVec { #[inline] - fn deref_mut(&mut self) -> &mut Self::Target { - self.as_mut_slice() - } + fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut_slice() } } /// This function is used in the [`smallvec`] macro. @@ -2138,8 +2106,7 @@ impl core::ops::DerefMut for SmallVec { #[track_caller] pub fn from_elem(elem: T, n: usize) -> SmallVec { if n > SmallVec::::inline_size() { - // Standard Rust vectors are already specialized. - SmallVec::::from_vec(vec![elem; n]) + repeat_n(elem, n).collect() } else { #[cfg(feature = "specialization")] { @@ -2215,18 +2182,14 @@ mod spec_traits { } impl SpecExtend for SmallVec - where - I: Iterator, + where I: Iterator { #[inline] - default fn spec_extend(&mut self, iter: I) { - self.extend_fallback(iter); - } + default fn spec_extend(&mut self, iter: I) { self.extend_fallback(iter); } } impl SpecExtend for SmallVec - where - I: core::iter::TrustedLen, + where I: core::iter::TrustedLen { fn spec_extend(&mut self, iter: I) { let (_, Some(additional)) = iter.size_hint() else { @@ -2241,7 +2204,10 @@ mod spec_traits { unsafe { let len = self.len(); let ptr = self.as_mut_ptr().add(len); - let mut guard = DropGuard { ptr, len: 0 }; + let mut guard = DropGuard { + ptr, + len: 0 + }; for x in iter { ptr.add(guard.len).write(x); @@ -2284,17 +2250,14 @@ mod spec_traits { impl<'a, T: 'a, const N: usize, I> SpecExtend<&'a T, I> for SmallVec where I: Iterator, - T: Clone, + T: Clone { #[inline] - default fn spec_extend(&mut self, iterator: I) { - self.spec_extend(iterator.cloned()) - } + default fn spec_extend(&mut self, iterator: I) { self.spec_extend(iterator.cloned()) } } impl<'a, T: 'a, const N: usize> SpecExtend<&'a T, core::slice::Iter<'a, T>> for SmallVec - where - T: Copy, + where T: Copy { fn spec_extend(&mut self, iter: core::slice::Iter<'a, T>) { let slice = iter.as_slice(); @@ -2375,18 +2338,14 @@ mod spec_traits { } impl SpecFromIterator for SmallVec - where - I: Iterator, + where I: Iterator { #[inline] - default fn spec_from_iter(iter: I) -> Self { - Self::from_iter_fallback(iter) - } + default fn spec_from_iter(iter: I) -> Self { Self::from_iter_fallback(iter) } } impl SpecFromIterator for SmallVec - where - I: core::iter::TrustedLen, + where I: core::iter::TrustedLen { fn spec_from_iter(iter: I) -> Self { let mut v = match iter.size_hint() { @@ -2395,7 +2354,7 @@ mod spec_traits { // are more than `usize::MAX` elements. // Since the previous branch would eagerly panic if the capacity is too large // (via `with_capacity`) we do the same here. - _ => panic!("capacity overflow"), + _ => panic!("capacity overflow") }; // Reuse the extend specialization for TrustedLen. v.spec_extend(iter); @@ -2412,9 +2371,7 @@ mod spec_traits { impl SpecCloneFrom for SmallVec { #[inline] - default fn spec_clone_from(&mut self, source: &[T]) { - self.clone_from_fallback(source); - } + default fn spec_clone_from(&mut self, source: &[T]) { self.clone_from_fallback(source); } } impl SpecCloneFrom for SmallVec { @@ -2478,14 +2435,15 @@ impl SmallVec { /// /// The caller must ensure that `n <= Self::inline_size()`. unsafe fn from_elem_fallback(elem: T, n: usize) -> Self - where - T: Clone, - { + where T: Clone { let mut result = Self::new(); if n > 0 { let ptr = result.raw.as_mut_ptr_inline(); - let mut guard = DropGuard { ptr, len: 0 }; + let mut guard = DropGuard { + ptr, + len: 0 + }; // SAFETY: The caller ensures that the first `n` // is smaller than the inline size. @@ -2509,9 +2467,7 @@ impl SmallVec { } fn extend_fallback(&mut self, iter: I) - where - I: IntoIterator, - { + where I: IntoIterator { let iter = iter.into_iter(); let (size, _) = iter.size_hint(); self.reserve(size); @@ -2530,9 +2486,7 @@ impl SmallVec { /// /// [`extend_from_within`]: SmallVec::extend_from_within unsafe fn extend_from_within_fallback(&mut self, src: core::ops::Range) - where - T: Clone, - { + where T: Clone { let old_len = self.len(); let start = src.start; @@ -2546,7 +2500,10 @@ impl SmallVec { let dst = ptr.add(old_len); let src = ptr.add(start); - let mut guard = DropGuard { ptr: dst, len: 0 }; + let mut guard = DropGuard { + ptr: dst, + len: 0 + }; for i in 0..len { let val = (*src.add(i)).clone(); dst.add(i).write(val); @@ -2562,9 +2519,7 @@ impl SmallVec { } fn from_iter_fallback(iter: I) -> Self - where - I: Iterator, - { + where I: Iterator { let (size, _) = iter.size_hint(); let mut v = Self::with_capacity(size); for x in iter { @@ -2574,9 +2529,7 @@ impl SmallVec { } fn clone_from_fallback(&mut self, source: &[T]) - where - T: Clone, - { + where T: Clone { // Inspired from `impl Clone for Vec`. // Drop anything that will not be overwritten. @@ -2598,9 +2551,7 @@ impl SmallVec { /// /// The caller must ensure that `slice.len() <= Self::inline_size()`. unsafe fn from_slice_fallback(slice: &[T]) -> Self - where - T: Clone, - { + where T: Clone { let mut v = Self::new(); let src = slice.as_ptr(); @@ -2610,7 +2561,10 @@ impl SmallVec { // SAFETY: The caller ensures that the slice length is smaller // than or equal to the inline length. unsafe { - let mut guard = DropGuard { ptr: dst, len: 0 }; + let mut guard = DropGuard { + ptr: dst, + len: 0 + }; for i in 0..len { let val = (*src.add(i)).clone(); dst.add(i).write(val); @@ -2653,23 +2607,17 @@ impl From<&[T]> for SmallVec { impl From<&mut [T]> for SmallVec { #[inline] - fn from(slice: &mut [T]) -> Self { - Self::from(slice as &[T]) - } + fn from(slice: &mut [T]) -> Self { Self::from(slice as &[T]) } } impl From<&[T; M]> for SmallVec { #[inline] - fn from(slice: &[T; M]) -> Self { - Self::from(slice as &[T]) - } + fn from(slice: &[T; M]) -> Self { Self::from(slice as &[T]) } } impl From<&mut [T; M]> for SmallVec { #[inline] - fn from(slice: &mut [T; M]) -> Self { - Self::from(slice as &[T]) - } + fn from(slice: &mut [T; M]) -> Self { Self::from(slice as &[T]) } } impl From<[T; M]> for SmallVec { @@ -2713,16 +2661,12 @@ impl TryFrom> for [T; M] { } impl From> for SmallVec { - fn from(array: Vec) -> Self { - Self::from_vec(array) - } + fn from(array: Vec) -> Self { Self::from_vec(array) } } impl Clone for SmallVec { #[inline] - fn clone(&self) -> SmallVec { - SmallVec::from(self.as_slice()) - } + fn clone(&self) -> SmallVec { SmallVec::from(self.as_slice()) } #[inline] fn clone_from(&mut self, source: &Self) { @@ -2740,9 +2684,7 @@ impl Clone for SmallVec { impl Clone for IntoIter { #[inline] - fn clone(&self) -> IntoIter { - SmallVec::from(self.as_slice()).into_iter() - } + fn clone(&self) -> IntoIter { SmallVec::from(self.as_slice()).into_iter() } } impl Extend for SmallVec { @@ -2815,6 +2757,7 @@ macro_rules! smallvec_inline { impl IntoIterator for SmallVec { type IntoIter = IntoIter; type Item = T; + fn into_iter(self) -> Self::IntoIter { // SAFETY: we move out of this.raw by reading the value at its address, which is // fine since we don't drop it @@ -2825,7 +2768,7 @@ impl IntoIterator for SmallVec { raw: (&this.raw as *const RawSmallVec).read(), begin: 0, end: this.len, - _marker: PhantomData, + _marker: PhantomData } } } @@ -2834,83 +2777,62 @@ impl IntoIterator for SmallVec { impl<'a, T, const N: usize> IntoIterator for &'a SmallVec { type IntoIter = core::slice::Iter<'a, T>; type Item = &'a T; - fn into_iter(self) -> Self::IntoIter { - self.iter() - } + + fn into_iter(self) -> Self::IntoIter { self.iter() } } impl<'a, T, const N: usize> IntoIterator for &'a mut SmallVec { type IntoIter = core::slice::IterMut<'a, T>; type Item = &'a mut T; - fn into_iter(self) -> Self::IntoIter { - self.iter_mut() - } + + fn into_iter(self) -> Self::IntoIter { self.iter_mut() } } impl PartialEq> for SmallVec -where - T: PartialEq, +where T: PartialEq { #[inline] - fn eq(&self, other: &SmallVec) -> bool { - self.as_slice().eq(other.as_slice()) - } + fn eq(&self, other: &SmallVec) -> bool { self.as_slice().eq(other.as_slice()) } } impl Eq for SmallVec where T: Eq {} impl PartialEq<[U; M]> for SmallVec -where - T: PartialEq, +where T: PartialEq { #[inline] - fn eq(&self, other: &[U; M]) -> bool { - self[..] == other[..] - } + fn eq(&self, other: &[U; M]) -> bool { self[..] == other[..] } } impl PartialEq<&[U; M]> for SmallVec -where - T: PartialEq, +where T: PartialEq { #[inline] - fn eq(&self, other: &&[U; M]) -> bool { - self[..] == other[..] - } + fn eq(&self, other: &&[U; M]) -> bool { self[..] == other[..] } } impl PartialEq<[U]> for SmallVec -where - T: PartialEq, +where T: PartialEq { #[inline] - fn eq(&self, other: &[U]) -> bool { - self[..] == other[..] - } + fn eq(&self, other: &[U]) -> bool { self[..] == other[..] } } impl PartialEq<&[U]> for SmallVec -where - T: PartialEq, +where T: PartialEq { #[inline] - fn eq(&self, other: &&[U]) -> bool { - self[..] == other[..] - } + fn eq(&self, other: &&[U]) -> bool { self[..] == other[..] } } impl PartialEq<&mut [U]> for SmallVec -where - T: PartialEq, +where T: PartialEq { #[inline] - fn eq(&self, other: &&mut [U]) -> bool { - self[..] == other[..] - } + fn eq(&self, other: &&mut [U]) -> bool { self[..] == other[..] } } impl PartialOrd for SmallVec -where - T: PartialOrd, +where T: PartialOrd { #[inline] fn partial_cmp(&self, other: &SmallVec) -> Option { @@ -2919,8 +2841,7 @@ where } impl Ord for SmallVec -where - T: Ord, +where T: Ord { #[inline] fn cmp(&self, other: &SmallVec) -> core::cmp::Ordering { @@ -2929,37 +2850,27 @@ where } impl Hash for SmallVec { - fn hash(&self, state: &mut H) { - self.as_slice().hash(state) - } + fn hash(&self, state: &mut H) { self.as_slice().hash(state) } } impl Borrow<[T]> for SmallVec { #[inline] - fn borrow(&self) -> &[T] { - self.as_slice() - } + fn borrow(&self) -> &[T] { self.as_slice() } } impl BorrowMut<[T]> for SmallVec { #[inline] - fn borrow_mut(&mut self) -> &mut [T] { - self.as_mut_slice() - } + fn borrow_mut(&mut self) -> &mut [T] { self.as_mut_slice() } } impl AsRef<[T]> for SmallVec { #[inline] - fn as_ref(&self) -> &[T] { - self.as_slice() - } + fn as_ref(&self) -> &[T] { self.as_slice() } } impl AsMut<[T]> for SmallVec { #[inline] - fn as_mut(&mut self) -> &mut [T] { - self.as_mut_slice() - } + fn as_mut(&mut self) -> &mut [T] { self.as_mut_slice() } } impl Debug for SmallVec { @@ -2983,8 +2894,7 @@ impl Debug for Drain<'_, T, N> { #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl Serialize for SmallVec -where - T: Serialize, +where T: Serialize { fn serialize(&self, serializer: S) -> Result { let mut state = serializer.serialize_seq(Some(self.len()))?; @@ -2998,25 +2908,23 @@ where #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de, T, const N: usize> Deserialize<'de> for SmallVec -where - T: Deserialize<'de>, +where T: Deserialize<'de> { fn deserialize>(deserializer: D) -> Result { deserializer.deserialize_seq(SmallVecVisitor { - phantom: PhantomData, + phantom: PhantomData }) } } #[cfg(feature = "serde")] struct SmallVecVisitor { - phantom: PhantomData, + phantom: PhantomData } #[cfg(feature = "serde")] impl<'de, T, const N: usize> Visitor<'de> for SmallVecVisitor -where - T: Deserialize<'de>, +where T: Deserialize<'de> { type Value = SmallVec; @@ -3025,9 +2933,7 @@ where } fn visit_seq(self, mut seq: B) -> Result - where - B: SeqAccess<'de>, - { + where B: SeqAccess<'de> { use serde_core::de::Error; let len = seq.size_hint().unwrap_or(0); let mut values = SmallVec::new(); @@ -3079,9 +2985,7 @@ impl io::Write for SmallVec { } #[inline] - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } + fn flush(&mut self) -> io::Result<()> { Ok(()) } } #[cfg(feature = "bytes")] @@ -3125,9 +3029,7 @@ unsafe impl BufMut for SmallVec { // and `advance_mut`. #[inline] fn put(&mut self, mut src: T) - where - Self: Sized, - { + where Self: Sized { // In case the src isn't contiguous, reserve upfront. self.reserve(src.remaining()); @@ -3140,9 +3042,7 @@ unsafe impl BufMut for SmallVec { } #[inline] - fn put_slice(&mut self, src: &[u8]) { - self.extend_from_slice(src); - } + fn put_slice(&mut self, src: &[u8]) { self.extend_from_slice(src); } #[inline] fn put_bytes(&mut self, val: u8, cnt: usize) { diff --git a/src/rawsmallvec.rs b/src/rawsmallvec.rs index dadbf95..d0192ef 100644 --- a/src/rawsmallvec.rs +++ b/src/rawsmallvec.rs @@ -1,5 +1,10 @@ -use core::mem::{ManuallyDrop, MaybeUninit}; -use core::ptr::NonNull; +use core::{ + mem::{ + ManuallyDrop, + MaybeUninit + }, + ptr::NonNull +}; /// Either a stack array with `length <= N` or a heap array /// whose pointer and capacity are stored here. @@ -9,5 +14,5 @@ use core::ptr::NonNull; #[repr(C)] pub union RawSmallVec { pub inline: ManuallyDrop>, - pub heap: (NonNull, usize), + pub heap: (NonNull, usize) } diff --git a/src/tests.rs b/src/tests.rs index 803dc4f..41d47df 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,10 +1,19 @@ -use crate::{smallvec, SmallVec}; -use alloc::borrow::ToOwned; -use alloc::boxed::Box; -use alloc::rc::Rc; -use alloc::{vec, vec::Vec}; -use core::hash::Hasher; -use core::iter::FromIterator; +use { + crate::{ + SmallVec, + smallvec + }, + alloc::{ + borrow::ToOwned, + boxed::Box, + rc::Rc, + vec::Vec + }, + core::{ + hash::Hasher, + iter::FromIterator + } +}; #[test] pub fn test_zero() { @@ -106,9 +115,7 @@ pub fn test_double_spill() { // https://github.com/servo/rust-smallvec/issues/4 #[test] -fn issue_4() { - SmallVec::, 2>::new(); -} +fn issue_4() { SmallVec::, 2>::new(); } // https://github.com/servo/rust-smallvec/issues/5 #[test] @@ -231,9 +238,7 @@ fn into_iter_drop() { struct DropCounter<'a>(&'a Cell); impl<'a> Drop for DropCounter<'a> { - fn drop(&mut self) { - self.0.set(self.0.get() + 1); - } + fn drop(&mut self) { self.0.set(self.0.get() + 1); } } { @@ -317,7 +322,7 @@ fn test_truncate() { #[test] fn test_truncate_references() { - let mut v = vec![0, 1, 2, 3, 4, 5, 6, 7]; + let mut v = Vec::from([0, 1, 2, 3, 4, 5, 6, 7]); let mut i = 8; let mut v: SmallVec<&mut u8, 8> = v.iter_mut().collect(); @@ -486,8 +491,10 @@ fn test_ord() { #[test] fn test_hash() { - use std::collections::hash_map::DefaultHasher; - use std::hash::Hash; + use std::{ + collections::hash_map::DefaultHasher, + hash::Hash + }; fn hash(value: impl Hash) -> u64 { let mut hasher = DefaultHasher::new(); @@ -567,17 +574,17 @@ fn test_from() { assert_eq!(&SmallVec::::from(&[1][..])[..], [1]); assert_eq!(&SmallVec::::from(&[1, 2, 3][..])[..], [1, 2, 3]); - let vec = vec![]; + let vec = Vec::new(); let small_vec: SmallVec = SmallVec::from(vec); assert_eq!(&*small_vec, &[0u8; 0]); drop(small_vec); - let vec = vec![1, 2, 3, 4, 5]; + let vec = Vec::from([1, 2, 3, 4, 5]); let small_vec: SmallVec = SmallVec::from(vec); assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); drop(small_vec); - let vec = vec![1, 2, 3, 4, 5]; + let vec = Vec::from([1, 2, 3, 4, 5]); let small_vec: SmallVec = SmallVec::from(vec); assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); drop(small_vec); @@ -589,7 +596,7 @@ fn test_from() { let array = [99; 128]; let small_vec: SmallVec = SmallVec::from(array); - assert_eq!(&*small_vec, vec![99u8; 128].as_slice()); + assert_eq!(&*small_vec, Vec::from([99u8; 128]).as_slice()); drop(small_vec); #[derive(PartialEq, Eq, Debug)] @@ -599,14 +606,14 @@ fn test_from() { assert_eq!(&*small_vec, &[NoClone(42)]); drop(small_vec); - let vec = vec![NoClone(42)]; + let vec = Vec::from([NoClone(42)]); let small_vec: SmallVec = SmallVec::from(vec); assert_eq!(&*small_vec, &[NoClone(42)]); drop(small_vec); let array = [1; 128]; let small_vec: SmallVec = SmallVec::from(array); - assert_eq!(&*small_vec, vec![1; 128].as_slice()); + assert_eq!(&*small_vec, Vec::from([1; 128]).as_slice()); drop(small_vec); let array = [99]; @@ -686,7 +693,7 @@ fn shrink_to_fit_unspill() { #[test] fn shrink_after_from_empty_vec() { - let mut v = SmallVec::::from_vec(vec![]); + let mut v = SmallVec::::from_vec(Vec::new()); v.shrink_to_fit(); assert!(!v.spilled()) } @@ -694,10 +701,10 @@ fn shrink_after_from_empty_vec() { #[test] fn test_into_vec() { let vec = SmallVec::::from_iter(0..2); - assert_eq!(vec.into_vec(), vec![0, 1]); + assert_eq!(vec.into_vec(), Vec::from([0, 1])); let vec = SmallVec::::from_iter(0..3); - assert_eq!(vec.into_vec(), vec![0, 1, 2]); + assert_eq!(vec.into_vec(), Vec::from([0, 1, 2])); } #[test] @@ -734,32 +741,32 @@ fn test_try_into_array() { #[test] fn test_from_vec() { - let vec = vec![]; + let vec = Vec::new(); let small_vec: SmallVec = SmallVec::from_vec(vec); assert_eq!(&*small_vec, &[0u8; 0]); drop(small_vec); - let vec = vec![]; + let vec = Vec::new(); let small_vec: SmallVec = SmallVec::from_vec(vec); assert_eq!(&*small_vec, &[0u8; 0]); drop(small_vec); - let vec = vec![1]; + let vec = Vec::from([1]); let small_vec: SmallVec = SmallVec::from_vec(vec); assert_eq!(&*small_vec, &[1]); drop(small_vec); - let vec = vec![1, 2, 3]; + let vec = Vec::from([1, 2, 3]); let small_vec: SmallVec = SmallVec::from_vec(vec); assert_eq!(&*small_vec, &[1, 2, 3]); drop(small_vec); - let vec = vec![1, 2, 3, 4, 5]; + let vec = Vec::from([1, 2, 3, 4, 5]); let small_vec: SmallVec = SmallVec::from_vec(vec); assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); drop(small_vec); - let vec = vec![1, 2, 3, 4, 5]; + let vec = Vec::from([1, 2, 3, 4, 5]); let small_vec: SmallVec = SmallVec::from_vec(vec); assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]); drop(small_vec); @@ -850,25 +857,44 @@ fn test_write() { #[cfg(feature = "serde")] #[test] fn test_serde() { - use serde_test::{assert_tokens, Token}; + use serde_test::{ + Token, + assert_tokens + }; let mut small_vec: SmallVec = SmallVec::new(); - assert_tokens(&small_vec, &[Token::Seq { len: Some(0) }, Token::SeqEnd]); + assert_tokens( + &small_vec, + &[ + Token::Seq { + len: Some(0) + }, + Token::SeqEnd + ] + ); small_vec.push(1); assert_tokens( &small_vec, - &[Token::Seq { len: Some(1) }, Token::I32(1), Token::SeqEnd], + &[ + Token::Seq { + len: Some(1) + }, + Token::I32(1), + Token::SeqEnd + ] ); small_vec.extend([2, 3, 4]); assert_tokens( &small_vec, &[ - Token::Seq { len: Some(4) }, + Token::Seq { + len: Some(4) + }, Token::I32(1), Token::I32(2), Token::I32(3), Token::I32(4), - Token::SeqEnd, - ], + Token::SeqEnd + ] ); } @@ -923,9 +949,7 @@ fn grow_spilled_same_size() { } #[test] -fn const_generics() { - let _v = SmallVec::::default(); -} +fn const_generics() { let _v = SmallVec::::default(); } #[test] fn const_new() { @@ -942,25 +966,15 @@ fn const_new() { assert_eq!(v[0], 1); assert_eq!(v[1], 4); } -const fn const_new_inner() -> SmallVec { - SmallVec::::new() -} -const fn const_new_inline_sized() -> SmallVec { - crate::smallvec_inline![1; 4] -} -const fn const_new_inline_args() -> SmallVec { - crate::smallvec_inline![1, 4] -} +const fn const_new_inner() -> SmallVec { SmallVec::::new() } +const fn const_new_inline_sized() -> SmallVec { crate::smallvec_inline![1; 4] } +const fn const_new_inline_args() -> SmallVec { crate::smallvec_inline![1, 4] } #[test] -fn empty_macro() { - let _v: SmallVec = smallvec![]; -} +fn empty_macro() { let _v: SmallVec = smallvec![]; } #[test] -fn zero_size_items() { - SmallVec::<(), 0>::new().push(()); -} +fn zero_size_items() { SmallVec::<(), 0>::new().push(()); } #[test] fn test_clone_from() { @@ -1036,9 +1050,8 @@ fn collect_from_iter() { impl Iterator for IterNoHint { type Item = I::Item; - fn next(&mut self) -> Option { - self.0.next() - } + + fn next(&mut self) -> Option { self.0.next() } // no implementation of size_hint means it returns (0, None) - which forces // from_iter to grow the allocated space iteratively. From 00772a6b88d1b40df107e707bdaaf3bacd0611ec Mon Sep 17 00:00:00 2001 From: Alejandro Vaz Date: Sat, 29 Aug 2026 16:10:00 +0200 Subject: [PATCH 3/3] fix: style --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0726579..e962da3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,7 @@ use { Hash, Hasher }, + iter::repeat_n, marker::PhantomData, mem::{ ManuallyDrop, @@ -127,8 +128,7 @@ use { NonNull, copy, copy_nonoverlapping - }, - iter::repeat_n + } } }; @@ -1108,7 +1108,6 @@ impl SmallVec { // 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: core::ptr::NonNull::from(self), } } }