diff --git a/lib.rs b/lib.rs index cedf2bd..843eef2 100644 --- a/lib.rs +++ b/lib.rs @@ -34,7 +34,6 @@ #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))] #![deny(missing_docs)] - #[cfg(not(feature = "std"))] #[macro_use] extern crate alloc; @@ -50,24 +49,24 @@ mod std { pub use core::*; } +#[cfg(feature = "serde")] +use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor}; +#[cfg(feature = "serde")] +use serde::ser::{Serialize, SerializeSeq, Serializer}; use std::borrow::{Borrow, BorrowMut}; use std::cmp; use std::fmt; use std::hash::{Hash, Hasher}; -use std::iter::{IntoIterator, FromIterator, repeat}; +#[cfg(feature = "std")] +use std::io; +use std::iter::{repeat, FromIterator, IntoIterator}; +#[cfg(feature = "serde")] +use std::marker::PhantomData; use std::mem; use std::mem::ManuallyDrop; use std::ops; use std::ptr; use std::slice; -#[cfg(feature = "std")] -use std::io; -#[cfg(feature = "serde")] -use serde::ser::{Serialize, Serializer, SerializeSeq}; -#[cfg(feature = "serde")] -use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor}; -#[cfg(feature = "serde")] -use std::marker::PhantomData; /// Creates a [`SmallVec`] containing the arguments. /// @@ -141,14 +140,16 @@ pub unsafe fn unreachable() -> ! { /// `panic!()` in debug builds, optimization hint in release. #[cfg(not(feature = "union"))] macro_rules! debug_unreachable { - () => { debug_unreachable!("entered unreachable code") }; + () => { + debug_unreachable!("entered unreachable code") + }; ($e:expr) => { if cfg!(not(debug_assertions)) { unreachable(); } else { panic!($e); } - } + }; } /// Common operations implemented by both `Vec` and `SmallVec`. @@ -174,19 +175,19 @@ macro_rules! debug_unreachable { /// ``` #[deprecated(note = "Use `Extend` and `Deref<[T]>` instead")] pub trait VecLike: - ops::Index + - ops::IndexMut + - ops::Index, Output=[T]> + - ops::IndexMut> + - ops::Index, Output=[T]> + - ops::IndexMut> + - ops::Index, Output=[T]> + - ops::IndexMut> + - ops::Index + - ops::IndexMut + - ops::DerefMut + - Extend { - + ops::Index + + ops::IndexMut + + ops::Index, Output = [T]> + + ops::IndexMut> + + ops::Index, Output = [T]> + + ops::IndexMut> + + ops::Index, Output = [T]> + + ops::IndexMut> + + ops::Index + + ops::IndexMut + + ops::DerefMut + + Extend +{ /// Append an element to the vector. fn push(&mut self, value: T); } @@ -240,15 +241,17 @@ unsafe fn deallocate(ptr: *mut T, capacity: usize) { /// /// [1]: struct.SmallVec.html#method.drain pub struct Drain<'a, T: 'a> { - iter: slice::IterMut<'a,T>, + iter: slice::IterMut<'a, T>, } -impl<'a, T: 'a> Iterator for Drain<'a,T> { +impl<'a, T: 'a> Iterator for Drain<'a, T> { type Item = T; #[inline] fn next(&mut self) -> Option { - self.iter.next().map(|reference| unsafe { ptr::read(reference) }) + self.iter + .next() + .map(|reference| unsafe { ptr::read(reference) }) } #[inline] @@ -260,13 +263,15 @@ impl<'a, T: 'a> Iterator for Drain<'a,T> { impl<'a, T: 'a> DoubleEndedIterator for Drain<'a, T> { #[inline] fn next_back(&mut self) -> Option { - self.iter.next_back().map(|reference| unsafe { ptr::read(reference) }) + self.iter + .next_back() + .map(|reference| unsafe { ptr::read(reference) }) } } -impl<'a, T> ExactSizeIterator for Drain<'a, T> { } +impl<'a, T> ExactSizeIterator for Drain<'a, T> {} -impl<'a, T: 'a> Drop for Drain<'a,T> { +impl<'a, T: 'a> Drop for Drain<'a, T> { fn drop(&mut self) { // Destroy the remaining elements. for _ in self.by_ref() {} @@ -291,10 +296,14 @@ impl SmallVecData { } #[inline] fn from_inline(inline: A) -> SmallVecData { - SmallVecData { inline: ManuallyDrop::new(inline) } + SmallVecData { + inline: ManuallyDrop::new(inline), + } } #[inline] - unsafe fn into_inline(self) -> A { ManuallyDrop::into_inner(self.inline) } + unsafe fn into_inline(self) -> A { + ManuallyDrop::into_inner(self.inline) + } #[inline] unsafe fn heap(&self) -> (*mut A::Item, usize) { self.heap @@ -527,7 +536,6 @@ impl SmallVec { } } - /// Sets the length of a vector. /// /// This will explicitly set the size of the vector, without actually @@ -584,7 +592,11 @@ impl SmallVec { let &mut (ptr, ref mut len_ptr) = self.data.heap_mut(); (ptr, len_ptr, self.capacity) } else { - (self.data.inline_mut().ptr_mut(), &mut self.capacity, A::size()) + ( + self.data.inline_mut().ptr_mut(), + &mut self.capacity, + A::size(), + ) } } } @@ -685,9 +697,10 @@ impl SmallVec { // from callers like insert() let (_, &mut len, cap) = self.triple_mut(); if cap - len < additional { - let new_cap = len.checked_add(additional). - and_then(usize::checked_next_power_of_two). - unwrap_or(usize::max_value()); + let new_cap = len + .checked_add(additional) + .and_then(usize::checked_next_power_of_two) + .unwrap_or(usize::max_value()); self.grow(new_cap); } } @@ -813,15 +826,15 @@ impl SmallVec { /// Insert multiple elements at position `index`, shifting all following elements toward the /// back. - pub fn insert_many>(&mut self, index: usize, iterable: I) { + pub fn insert_many>(&mut self, index: usize, iterable: I) { let iter = iterable.into_iter(); if index == self.len() { return self.extend(iter); } let (lower_size_bound, _) = iter.size_hint(); - assert!(lower_size_bound <= std::isize::MAX as usize); // Ensure offset is indexable - assert!(index + lower_size_bound >= index); // Protect against overflow + assert!(lower_size_bound <= std::isize::MAX as usize); // Ensure offset is indexable + assert!(index + lower_size_bound >= index); // Protect against overflow self.reserve(lower_size_bound); unsafe { @@ -850,7 +863,11 @@ impl SmallVec { } if num_added < lower_size_bound { // Iterator provided fewer elements than the hint - ptr::copy(ptr.offset(lower_size_bound as isize), ptr.offset(num_added as isize), old_len - index); + ptr::copy( + ptr.offset(lower_size_bound as isize), + ptr.offset(num_added as isize), + old_len - index, + ); } self.set_len(old_len + num_added); @@ -907,13 +924,17 @@ impl SmallVec { } /// Removes consecutive duplicate elements. - pub fn dedup(&mut self) where A::Item: PartialEq { + pub fn dedup(&mut self) + where + A::Item: PartialEq, + { self.dedup_by(|a, b| a == b); } /// Removes consecutive duplicate elements using the given equality relation. pub fn dedup_by(&mut self, mut same_bucket: F) - where F: FnMut(&mut A::Item, &mut A::Item) -> bool + where + F: FnMut(&mut A::Item, &mut A::Item) -> bool, { // See the implementation of Vec::dedup_by in the // standard library for an explanation of this algorithm. @@ -944,8 +965,9 @@ impl SmallVec { /// Removes consecutive elements that map to the same key. pub fn dedup_by_key(&mut self, mut key: F) - where F: FnMut(&mut A::Item) -> K, - K: PartialEq + where + F: FnMut(&mut A::Item) -> K, + K: PartialEq, { self.dedup_by(|a, b| key(a) == key(b)); } @@ -1018,11 +1040,7 @@ impl SmallVec { /// assert_eq!(&*rebuilt, &[4, 5, 6]); /// } /// } - pub unsafe fn from_raw_parts( - ptr: *mut A::Item, - length: usize, - capacity: usize, - ) -> SmallVec { + pub unsafe fn from_raw_parts(ptr: *mut A::Item, length: usize, capacity: usize) -> SmallVec { assert!(capacity > A::size()); SmallVec { capacity, @@ -1031,7 +1049,10 @@ impl SmallVec { } } -impl SmallVec where A::Item: Copy { +impl SmallVec +where + A::Item: Copy, +{ /// Copy the elements from a slice into a new `SmallVec`. /// /// For slices of `Copy` types, this is more efficient than `SmallVec::from(slice)`. @@ -1044,7 +1065,7 @@ impl SmallVec where A::Item: Copy { let mut data: A = mem::uninitialized(); ptr::copy_nonoverlapping(slice.as_ptr(), data.ptr_mut(), len); data - }) + }), } } else { let mut b = slice.to_vec(); @@ -1086,7 +1107,10 @@ impl SmallVec where A::Item: Copy { } } -impl SmallVec where A::Item: Clone { +impl SmallVec +where + A::Item: Clone, +{ /// Resizes the vector so that its length is equal to `len`. /// /// If `len` is less than the current length, the vector simply truncated. @@ -1199,7 +1223,10 @@ impl> io::Write for SmallVec { } #[cfg(feature = "serde")] -impl Serialize for SmallVec where A::Item: Serialize { +impl Serialize for SmallVec +where + A::Item: Serialize, +{ fn serialize(&self, serializer: S) -> Result { let mut state = serializer.serialize_seq(Some(self.len()))?; for item in self { @@ -1210,20 +1237,26 @@ impl Serialize for SmallVec where A::Item: Serialize { } #[cfg(feature = "serde")] -impl<'de, A: Array> Deserialize<'de> for SmallVec where A::Item: Deserialize<'de> { +impl<'de, A: Array> Deserialize<'de> for SmallVec +where + A::Item: Deserialize<'de>, +{ fn deserialize>(deserializer: D) -> Result { - deserializer.deserialize_seq(SmallVecVisitor{phantom: PhantomData}) + deserializer.deserialize_seq(SmallVecVisitor { + phantom: PhantomData, + }) } } #[cfg(feature = "serde")] struct SmallVecVisitor { - phantom: PhantomData + phantom: PhantomData, } #[cfg(feature = "serde")] impl<'de, A: Array> Visitor<'de> for SmallVecVisitor -where A::Item: Deserialize<'de>, +where + A::Item: Deserialize<'de>, { type Value = SmallVec; @@ -1232,8 +1265,8 @@ where A::Item: Deserialize<'de>, } fn visit_seq(self, mut seq: B) -> Result - where - B: SeqAccess<'de>, + where + B: SeqAccess<'de>, { let len = seq.size_hint().unwrap_or(0); let mut values = SmallVec::with_capacity(len); @@ -1246,7 +1279,6 @@ where A::Item: Deserialize<'de>, } } - #[cfg(feature = "specialization")] trait SpecFrom { fn spec_from(slice: S) -> SmallVec; @@ -1256,14 +1288,20 @@ trait SpecFrom { mod specialization; #[cfg(feature = "specialization")] -impl<'a, A: Array> SpecFrom for SmallVec where A::Item: Copy { +impl<'a, A: Array> SpecFrom for SmallVec +where + A::Item: Copy, +{ #[inline] fn spec_from(slice: &'a [A::Item]) -> SmallVec { SmallVec::from_slice(slice) } } -impl<'a, A: Array> From<&'a [A::Item]> for SmallVec where A::Item: Clone { +impl<'a, A: Array> From<&'a [A::Item]> for SmallVec +where + A::Item: Clone, +{ #[cfg(not(feature = "specialization"))] #[inline] fn from(slice: &'a [A::Item]) -> SmallVec { @@ -1307,7 +1345,7 @@ macro_rules! impl_index { &mut (&mut **self)[index] } } - } + }; } impl_index!(usize, A::Item); @@ -1316,7 +1354,10 @@ impl_index!(ops::RangeFrom, [A::Item]); impl_index!(ops::RangeTo, [A::Item]); impl_index!(ops::RangeFull, [A::Item]); -impl ExtendFromSlice for SmallVec where A::Item: Copy { +impl ExtendFromSlice for SmallVec +where + A::Item: Copy, +{ fn extend_from_slice(&mut self, other: &[A::Item]) { SmallVec::extend_from_slice(self, other) } @@ -1331,7 +1372,7 @@ impl VecLike for SmallVec { } impl FromIterator for SmallVec { - fn from_iter>(iterable: I) -> SmallVec { + fn from_iter>(iterable: I) -> SmallVec { let mut v = SmallVec::new(); v.extend(iterable); v @@ -1339,7 +1380,7 @@ impl FromIterator for SmallVec { } impl Extend for SmallVec { - fn extend>(&mut self, iterable: I) { + fn extend>(&mut self, iterable: I) { let mut iter = iterable.into_iter(); let (lower_size_bound, _) = iter.size_hint(); self.reserve(lower_size_bound); @@ -1363,7 +1404,10 @@ impl Extend for SmallVec { } } -impl fmt::Debug for SmallVec where A::Item: fmt::Debug { +impl fmt::Debug for SmallVec +where + A::Item: fmt::Debug, +{ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_list().entries(self.iter()).finish() } @@ -1404,7 +1448,10 @@ impl Drop for SmallVec { } } -impl Clone for SmallVec where A::Item: Clone { +impl Clone for SmallVec +where + A::Item: Clone, +{ fn clone(&self) -> SmallVec { let mut new_vector = SmallVec::with_capacity(self.len()); for element in self.iter() { @@ -1415,30 +1462,45 @@ impl Clone for SmallVec where A::Item: Clone { } impl PartialEq> for SmallVec - where A::Item: PartialEq { +where + A::Item: PartialEq, +{ #[inline] - fn eq(&self, other: &SmallVec) -> bool { self[..] == other[..] } + fn eq(&self, other: &SmallVec) -> bool { + self[..] == other[..] + } #[inline] - fn ne(&self, other: &SmallVec) -> bool { self[..] != other[..] } + fn ne(&self, other: &SmallVec) -> bool { + self[..] != other[..] + } } impl Eq for SmallVec where A::Item: Eq {} -impl PartialOrd for SmallVec where A::Item: PartialOrd { +impl PartialOrd for SmallVec +where + A::Item: PartialOrd, +{ #[inline] fn partial_cmp(&self, other: &SmallVec) -> Option { PartialOrd::partial_cmp(&**self, &**other) } } -impl Ord for SmallVec where A::Item: Ord { +impl Ord for SmallVec +where + A::Item: Ord, +{ #[inline] fn cmp(&self, other: &SmallVec) -> cmp::Ordering { Ord::cmp(&**self, &**other) } } -impl Hash for SmallVec where A::Item: Hash { +impl Hash for SmallVec +where + A::Item: Hash, +{ fn hash(&self, state: &mut H) { (**self).hash(state) } @@ -1459,7 +1521,7 @@ pub struct IntoIter { impl Drop for IntoIter { fn drop(&mut self) { - for _ in self { } + for _ in self {} } } @@ -1470,8 +1532,7 @@ impl Iterator for IntoIter { fn next(&mut self) -> Option { if self.current == self.end { None - } - else { + } else { unsafe { let current = self.current as isize; self.current += 1; @@ -1492,8 +1553,7 @@ impl DoubleEndedIterator for IntoIter { fn next_back(&mut self) -> Option { if self.current == self.end { None - } - else { + } else { unsafe { self.end -= 1; Some(ptr::read(self.data.as_ptr().offset(self.end as isize))) @@ -1502,7 +1562,7 @@ impl DoubleEndedIterator for IntoIter { } } -impl ExactSizeIterator for IntoIter { } +impl ExactSizeIterator for IntoIter {} impl IntoIterator for SmallVec { type IntoIter = IntoIter; @@ -1560,7 +1620,10 @@ struct SetLenOnDrop<'a> { impl<'a> SetLenOnDrop<'a> { #[inline] fn new(len: &'a mut usize) -> Self { - SetLenOnDrop { local_len: *len, len: len } + SetLenOnDrop { + local_len: *len, + len: len, + } } #[inline] @@ -1594,9 +1657,11 @@ macro_rules! impl_array( } ); -impl_array!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, - 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, - 0x10000, 0x20000, 0x40000, 0x80000, 0x100000); +impl_array!( + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, 36, 0x40, 0x80, 0x100, + 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000, 0x10000, 0x20000, 0x40000, 0x80000, + 0x100000 +); #[cfg(test)] mod tests { @@ -1604,18 +1669,18 @@ mod tests { use std::iter::FromIterator; - #[cfg(feature = "std")] - use std::borrow::ToOwned; #[cfg(not(feature = "std"))] use alloc::borrow::ToOwned; - #[cfg(feature = "std")] - use std::rc::Rc; - #[cfg(not(feature = "std"))] - use alloc::rc::Rc; #[cfg(not(feature = "std"))] use alloc::boxed::Box; #[cfg(not(feature = "std"))] + use alloc::rc::Rc; + #[cfg(not(feature = "std"))] use alloc::vec::Vec; + #[cfg(feature = "std")] + use std::borrow::ToOwned; + #[cfg(feature = "std")] + use std::rc::Rc; #[test] pub fn test_zero() { @@ -1633,10 +1698,7 @@ mod tests { let mut v = SmallVec::<[_; 16]>::new(); v.push("hello".to_owned()); v.push("there".to_owned()); - assert_eq!(&*v, &[ - "hello".to_owned(), - "there".to_owned(), - ][..]); + assert_eq!(&*v, &["hello".to_owned(), "there".to_owned(),][..]); } #[test] @@ -1648,12 +1710,15 @@ mod tests { v.push("burma".to_owned()); assert_eq!(v[0], "hello"); v.push("shave".to_owned()); - assert_eq!(&*v, &[ - "hello".to_owned(), - "there".to_owned(), - "burma".to_owned(), - "shave".to_owned(), - ][..]); + assert_eq!( + &*v, + &[ + "hello".to_owned(), + "there".to_owned(), + "burma".to_owned(), + "shave".to_owned(), + ][..] + ); } #[test] @@ -1667,16 +1732,19 @@ mod tests { v.push("there".to_owned()); v.push("burma".to_owned()); v.push("shave".to_owned()); - assert_eq!(&*v, &[ - "hello".to_owned(), - "there".to_owned(), - "burma".to_owned(), - "shave".to_owned(), - "hello".to_owned(), - "there".to_owned(), - "burma".to_owned(), - "shave".to_owned(), - ][..]); + assert_eq!( + &*v, + &[ + "hello".to_owned(), + "there".to_owned(), + "burma".to_owned(), + "shave".to_owned(), + "hello".to_owned(), + "there".to_owned(), + "burma".to_owned(), + "shave".to_owned(), + ][..] + ); } /// https://github.com/servo/rust-smallvec/issues/4 @@ -1857,14 +1925,24 @@ mod tests { } assert_eq!(v.len(), 4); v.insert_many(1, [5, 6].iter().cloned()); - assert_eq!(&v.iter().map(|v| *v).collect::>(), &[0, 5, 6, 1, 2, 3]); + assert_eq!( + &v.iter().map(|v| *v).collect::>(), + &[0, 5, 6, 1, 2, 3] + ); } - struct MockHintIter{x: T, hint: usize} + struct MockHintIter { + x: T, + hint: usize, + } impl Iterator for MockHintIter { type Item = T::Item; - fn next(&mut self) -> Option {self.x.next()} - fn size_hint(&self) -> (usize, Option) {(self.hint, None)} + fn next(&mut self) -> Option { + self.x.next() + } + fn size_hint(&self) -> (usize, Option) { + (self.hint, None) + } } #[test] @@ -1874,8 +1952,17 @@ mod tests { v.push(x); } assert_eq!(v.len(), 4); - v.insert_many(1, MockHintIter{x: [5, 6].iter().cloned(), hint: 5}); - assert_eq!(&v.iter().map(|v| *v).collect::>(), &[0, 5, 6, 1, 2, 3]); + v.insert_many( + 1, + MockHintIter { + x: [5, 6].iter().cloned(), + hint: 5, + }, + ); + assert_eq!( + &v.iter().map(|v| *v).collect::>(), + &[0, 5, 6, 1, 2, 3] + ); } #[test] @@ -1885,8 +1972,17 @@ mod tests { v.push(x); } assert_eq!(v.len(), 4); - v.insert_many(1, MockHintIter{x: [5, 6].iter().cloned(), hint: 1}); - assert_eq!(&v.iter().map(|v| *v).collect::>(), &[0, 5, 6, 1, 2, 3]); + v.insert_many( + 1, + MockHintIter { + x: [5, 6].iter().cloned(), + hint: 1, + }, + ); + assert_eq!( + &v.iter().map(|v| *v).collect::>(), + &[0, 5, 6, 1, 2, 3] + ); } #[cfg(feature = "std")] @@ -1894,7 +1990,7 @@ mod tests { // https://github.com/servo/rust-smallvec/issues/96 fn test_insert_many_panic() { struct PanicOnDoubleDrop { - dropped: Box + dropped: Box, } impl Drop for PanicOnDoubleDrop { @@ -1907,14 +2003,23 @@ mod tests { struct BadIter; impl Iterator for BadIter { type Item = PanicOnDoubleDrop; - fn size_hint(&self) -> (usize, Option) { (1, None) } - fn next(&mut self) -> Option { panic!() } + fn size_hint(&self) -> (usize, Option) { + (1, None) + } + fn next(&mut self) -> Option { + panic!() + } } let mut vec: SmallVec<[PanicOnDoubleDrop; 0]> = vec![ - PanicOnDoubleDrop { dropped: Box::new(false) }, - PanicOnDoubleDrop { dropped: Box::new(false) }, - ].into(); + PanicOnDoubleDrop { + dropped: Box::new(false), + }, + PanicOnDoubleDrop { + dropped: Box::new(false), + }, + ] + .into(); let result = ::std::panic::catch_unwind(move || { vec.insert_many(0, BadIter); }); @@ -1937,7 +2042,10 @@ mod tests { } assert_eq!(v.len(), 4); v.insert_from_slice(1, &[5, 6]); - assert_eq!(&v.iter().map(|v| *v).collect::>(), &[0, 5, 6, 1, 2, 3]); + assert_eq!( + &v.iter().map(|v| *v).collect::>(), + &[0, 5, 6, 1, 2, 3] + ); } #[test] @@ -1948,7 +2056,10 @@ mod tests { } assert_eq!(v.len(), 4); v.extend_from_slice(&[5, 6]); - assert_eq!(&v.iter().map(|v| *v).collect::>(), &[0, 1, 2, 3, 5, 6]); + assert_eq!( + &v.iter().map(|v| *v).collect::>(), + &[0, 1, 2, 3, 5, 6] + ); } #[test] @@ -2010,8 +2121,8 @@ mod tests { #[cfg(feature = "std")] #[test] fn test_hash() { - use std::hash::Hash; use std::collections::hash_map::DefaultHasher; + use std::hash::Hash; { let mut a: SmallVec<[u32; 2]> = SmallVec::new(); @@ -2115,7 +2226,10 @@ mod tests { #[test] fn test_from_slice() { assert_eq!(&SmallVec::<[u32; 2]>::from_slice(&[1][..])[..], [1]); - assert_eq!(&SmallVec::<[u32; 2]>::from_slice(&[1, 2, 3][..])[..], [1, 2, 3]); + assert_eq!( + &SmallVec::<[u32; 2]>::from_slice(&[1, 2, 3][..])[..], + [1, 2, 3] + ); } #[test] diff --git a/specialization.rs b/specialization.rs index 2e7bb1e..658fa77 100644 --- a/specialization.rs +++ b/specialization.rs @@ -6,9 +6,12 @@ //! Implementations that require `default fn`. -use super::{SpecFrom, SmallVec, Array}; +use super::{Array, SmallVec, SpecFrom}; -impl<'a, A: Array> SpecFrom for SmallVec where A::Item: Clone { +impl<'a, A: Array> SpecFrom for SmallVec +where + A::Item: Clone, +{ #[inline] default fn spec_from(slice: &'a [A::Item]) -> SmallVec { slice.into_iter().cloned().collect()