diff --git a/fuzz/src/arb_range.rs b/fuzz/src/arb_range.rs index f0db947..0a14f94 100644 --- a/fuzz/src/arb_range.rs +++ b/fuzz/src/arb_range.rs @@ -1,61 +1,64 @@ use arbitrary::{Arbitrary, Unstructured}; -use std::ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive, Bound}; +use std::ops::{ + Bound, Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, + RangeToInclusive, +}; #[derive(Debug, Clone, Eq, PartialEq)] pub enum ArbRange { - Range(Range), - RangeFrom(RangeFrom), - RangeInclusive(RangeInclusive), - RangeTo(RangeTo), - RangeToInclusive(RangeToInclusive), + Range(Range), + RangeFrom(RangeFrom), + RangeInclusive(RangeInclusive), + RangeTo(RangeTo), + RangeToInclusive(RangeToInclusive), } impl RangeBounds for ArbRange { - fn start_bound(&self) -> Bound<&T> { - match self { - ArbRange::Range(range) => range.start_bound(), - ArbRange::RangeFrom(range) => range.start_bound(), - ArbRange::RangeInclusive(range) => range.start_bound(), - ArbRange::RangeTo(range) => range.start_bound(), - ArbRange::RangeToInclusive(range) => range.start_bound(), - } + fn start_bound(&self) -> Bound<&T> { + match self { + ArbRange::Range(range) => range.start_bound(), + ArbRange::RangeFrom(range) => range.start_bound(), + ArbRange::RangeInclusive(range) => range.start_bound(), + ArbRange::RangeTo(range) => range.start_bound(), + ArbRange::RangeToInclusive(range) => range.start_bound(), } + } - fn end_bound(&self) -> Bound<&T> { - match self { - ArbRange::Range(range) => range.end_bound(), - ArbRange::RangeFrom(range) => range.end_bound(), - ArbRange::RangeInclusive(range) => range.end_bound(), - ArbRange::RangeTo(range) => range.end_bound(), - ArbRange::RangeToInclusive(range) => range.end_bound(), - } + fn end_bound(&self) -> Bound<&T> { + match self { + ArbRange::Range(range) => range.end_bound(), + ArbRange::RangeFrom(range) => range.end_bound(), + ArbRange::RangeInclusive(range) => range.end_bound(), + ArbRange::RangeTo(range) => range.end_bound(), + ArbRange::RangeToInclusive(range) => range.end_bound(), } + } - fn contains(&self, item: &U) -> bool - where - T: PartialOrd, - U: PartialOrd, - { - match self { - ArbRange::Range(range) => range.contains(item), - ArbRange::RangeFrom(range) => range.contains(item), - ArbRange::RangeInclusive(range) => range.contains(item), - ArbRange::RangeTo(range) => range.contains(item), - ArbRange::RangeToInclusive(range) => range.contains(item), - } + fn contains(&self, item: &U) -> bool + where + T: PartialOrd, + U: PartialOrd, + { + match self { + ArbRange::Range(range) => range.contains(item), + ArbRange::RangeFrom(range) => range.contains(item), + ArbRange::RangeInclusive(range) => range.contains(item), + ArbRange::RangeTo(range) => range.contains(item), + ArbRange::RangeToInclusive(range) => range.contains(item), } + } } impl Arbitrary for ArbRange { - fn arbitrary(u: &mut U) -> Result { - let variant = u8::arbitrary(u)? % 5; - Ok(match variant { - 0 => ArbRange::Range(T::arbitrary(u)?..T::arbitrary(u)?), - 1 => ArbRange::RangeFrom(T::arbitrary(u)?..), - 2 => ArbRange::RangeInclusive(T::arbitrary(u)?..=T::arbitrary(u)?), - 3 => ArbRange::RangeTo(..T::arbitrary(u)?), - 4 => ArbRange::RangeToInclusive(..=T::arbitrary(u)?), - _ => unreachable!(), - }) - } + fn arbitrary(u: &mut U) -> Result { + let variant = u8::arbitrary(u)? % 5; + Ok(match variant { + 0 => ArbRange::Range(T::arbitrary(u)?..T::arbitrary(u)?), + 1 => ArbRange::RangeFrom(T::arbitrary(u)?..), + 2 => ArbRange::RangeInclusive(T::arbitrary(u)?..=T::arbitrary(u)?), + 3 => ArbRange::RangeTo(..T::arbitrary(u)?), + 4 => ArbRange::RangeToInclusive(..=T::arbitrary(u)?), + _ => unreachable!(), + }) + } } diff --git a/fuzz/src/bin/arrayish.rs b/fuzz/src/bin/arrayish.rs index cb0586f..7146126 100644 --- a/fuzz/src/bin/arrayish.rs +++ b/fuzz/src/bin/arrayish.rs @@ -1,7 +1,11 @@ -use derive_arbitrary::Arbitrary; use arbitrary_model_tests::arbitrary_stateful_operations; +use derive_arbitrary::Arbitrary; use honggfuzz::fuzz; -use std::{fmt::Debug, iter::FromIterator, ops::{RangeBounds, Bound}}; +use std::{ + fmt::Debug, + iter::FromIterator, + ops::{Bound, RangeBounds}, +}; use tinyvec::ArrayVec; use tinyvec_fuzz::ArbRange; @@ -83,29 +87,31 @@ arbitrary_stateful_operations! { const MAX_RING_SIZE: usize = 16_384; fn fuzz_cycle(data: &[u8]) -> Result<(), ()> { - use arbitrary::{Arbitrary, FiniteBuffer}; + use arbitrary::{Arbitrary, FiniteBuffer}; - let mut ring = FiniteBuffer::new(&data, MAX_RING_SIZE).map_err(|_| ())?; + let mut ring = FiniteBuffer::new(&data, MAX_RING_SIZE).map_err(|_| ())?; - let mut model = Vec::::default(); - let mut tested: ArrayVec<[u16; 32]> = ArrayVec::new(); + let mut model = Vec::::default(); + let mut tested: ArrayVec<[u16; 32]> = ArrayVec::new(); - let mut _op_trace = String::new(); - while let Ok(op) = > as Arbitrary>::arbitrary(&mut ring) { - #[cfg(fuzzing_debug)] - _op_trace.push_str(&format!("{}\n", op.to_string())); - op.execute_and_compare(&mut model, &mut tested); - } + let mut _op_trace = String::new(); + while let Ok(op) = + > as Arbitrary>::arbitrary(&mut ring) + { + #[cfg(fuzzing_debug)] + _op_trace.push_str(&format!("{}\n", op.to_string())); + op.execute_and_compare(&mut model, &mut tested); + } - Ok(()) + Ok(()) } fn main() -> Result<(), ()> { - better_panic::install(); + better_panic::install(); - loop { - fuzz!(|data: &[u8]| { - let _ = fuzz_cycle(data); - }); - } + loop { + fuzz!(|data: &[u8]| { + let _ = fuzz_cycle(data); + }); + } } diff --git a/fuzz/src/bin/tinyvec.rs b/fuzz/src/bin/tinyvec.rs index 1a66b69..3bacaa8 100644 --- a/fuzz/src/bin/tinyvec.rs +++ b/fuzz/src/bin/tinyvec.rs @@ -1,7 +1,11 @@ -use derive_arbitrary::Arbitrary; use arbitrary_model_tests::arbitrary_stateful_operations; +use derive_arbitrary::Arbitrary; use honggfuzz::fuzz; -use std::{fmt::Debug, iter::FromIterator, ops::{RangeBounds, Bound}}; +use std::{ + fmt::Debug, + iter::FromIterator, + ops::{Bound, RangeBounds}, +}; use tinyvec::TinyVec; use tinyvec_fuzz::ArbRange; @@ -81,29 +85,31 @@ arbitrary_stateful_operations! { const MAX_RING_SIZE: usize = 16_384; fn fuzz_cycle(data: &[u8]) -> Result<(), ()> { - use arbitrary::{Arbitrary, FiniteBuffer}; + use arbitrary::{Arbitrary, FiniteBuffer}; - let mut ring = FiniteBuffer::new(&data, MAX_RING_SIZE).map_err(|_| ())?; + let mut ring = FiniteBuffer::new(&data, MAX_RING_SIZE).map_err(|_| ())?; - let mut model = Vec::::default(); - let mut tested: TinyVec<[u16; 32]> = TinyVec::new(); + let mut model = Vec::::default(); + let mut tested: TinyVec<[u16; 32]> = TinyVec::new(); - let mut _op_trace = String::new(); - while let Ok(op) = > as Arbitrary>::arbitrary(&mut ring) { - #[cfg(fuzzing_debug)] - _op_trace.push_str(&format!("{}\n", op.to_string())); - op.execute_and_compare(&mut model, &mut tested); - } + let mut _op_trace = String::new(); + while let Ok(op) = + > as Arbitrary>::arbitrary(&mut ring) + { + #[cfg(fuzzing_debug)] + _op_trace.push_str(&format!("{}\n", op.to_string())); + op.execute_and_compare(&mut model, &mut tested); + } - Ok(()) + Ok(()) } fn main() -> Result<(), ()> { - better_panic::install(); + better_panic::install(); - loop { - fuzz!(|data: &[u8]| { - let _ = fuzz_cycle(data); - }); - } + loop { + fuzz!(|data: &[u8]| { + let _ = fuzz_cycle(data); + }); + } } diff --git a/src/array.rs b/src/array.rs index 840e433..c152258 100644 --- a/src/array.rs +++ b/src/array.rs @@ -28,7 +28,7 @@ pub trait Array { fn as_slice(&self) -> &[Self::Item]; /// Gives a unique slice over the whole thing. - /// + /// /// A correct implementation will return a slice with a length equal to the /// `CAPACITY` value. fn as_slice_mut(&mut self) -> &mut [Self::Item]; diff --git a/src/lib.rs b/src/lib.rs index af7c239..87cd9d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,10 +4,7 @@ feature = "nightly_slice_partition_dedup", feature(slice_partition_dedup) )] -#![cfg_attr( - feature = "nightly_const_generics", - feature(const_generics) -)] +#![cfg_attr(feature = "nightly_const_generics", feature(const_generics))] #![warn(clippy::missing_inline_in_public_items)] #![warn(clippy::must_use_candidate)] #![warn(missing_docs)] @@ -91,5 +88,5 @@ pub use tinyvec::*; // Replace with mem::take as soon as MSRV allows it fn take(from: &mut T) -> T { - replace(from, T::default()) + replace(from, T::default()) } diff --git a/src/tinyvec.rs b/src/tinyvec.rs index 0bb84e9..2ff42c8 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -14,9 +14,9 @@ use alloc::vec::Vec; /// /// ```rust /// use tinyvec::*; -/// +/// /// let empty_av = tiny_vec!([u8; 16]); -/// +/// /// let some_ints = tiny_vec!([i32; 4], 1, 2, 3); /// ``` #[macro_export] @@ -51,7 +51,7 @@ pub enum TinyVec { #[allow(missing_docs)] Inline(ArrayVec), #[allow(missing_docs)] - Heap(Vec) + Heap(Vec), } impl Default for TinyVec { #[inline] @@ -127,9 +127,9 @@ impl TinyVec { } /// A mutable pointer to the backing array. - /// + /// /// ## Safety - /// + /// /// This pointer has provenance over the _entire_ backing array/buffer. #[inline(always)] #[must_use] @@ -148,9 +148,9 @@ impl TinyVec { } /// A const pointer to the backing array. - /// + /// /// ## Safety - /// + /// /// This pointer has provenance over the _entire_ backing array/buffer. #[inline(always)] #[must_use] @@ -169,7 +169,7 @@ impl TinyVec { } /// The capacity of the `TinyVec`. - /// + /// /// When not heap allocated this is fixed based on the array type. /// Otherwise its the result of the underlying Vec::capacity. #[inline(always)] @@ -186,7 +186,7 @@ impl TinyVec { pub fn clear(&mut self) { self.truncate(0) } - + /// De-duplicates the vec. #[cfg(feature = "nightly_slice_partition_dedup")] #[inline(always)] @@ -325,12 +325,14 @@ impl TinyVec { #[inline] pub fn insert(&mut self, index: usize, item: A::Item) { match self { - TinyVec::Inline(a) => if a.len() == A::CAPACITY { - self.move_to_the_heap(); - self.insert(index, item) - } else { - a.insert(index, item); - }, + TinyVec::Inline(a) => { + if a.len() == A::CAPACITY { + self.move_to_the_heap(); + self.insert(index, item) + } else { + a.insert(index, item); + } + } TinyVec::Heap(v) => v.insert(index, item), } } @@ -363,7 +365,7 @@ impl TinyVec { } /// Remove and return the last element of the vec, if there is one. - /// + /// /// ## Failure /// * If the vec is empty you get `None`. #[inline] @@ -380,12 +382,14 @@ impl TinyVec { #[inline(always)] pub fn push(&mut self, val: A::Item) { match self { - TinyVec::Inline(a) => if a.len() == A::CAPACITY { - self.move_to_the_heap(); - self.push(val) - } else { - a.push(val); - }, + TinyVec::Inline(a) => { + if a.len() == A::CAPACITY { + self.move_to_the_heap(); + self.push(val) + } else { + a.push(val); + } + } TinyVec::Heap(v) => v.push(val), } } @@ -440,12 +444,14 @@ impl TinyVec { A::Item: Clone, { match self { - TinyVec::Inline(a) => if new_len > A::CAPACITY { - self.move_to_the_heap(); - self.resize(new_len, new_val); - } else { - a.resize(new_len, new_val); - }, + TinyVec::Inline(a) => { + if new_len > A::CAPACITY { + self.move_to_the_heap(); + self.resize(new_len, new_val); + } else { + a.resize(new_len, new_val); + } + } TinyVec::Heap(v) => v.resize(new_len, new_val), } } @@ -473,11 +479,7 @@ impl TinyVec { /// assert_eq!(tv.as_slice(), &[2, 4, 8, 16][..]); /// ``` #[inline] - pub fn resize_with A::Item>( - &mut self, - new_len: usize, - f: F, - ) { + pub fn resize_with A::Item>(&mut self, new_len: usize, f: F) { match self { TinyVec::Inline(a) => a.resize_with(new_len, f), TinyVec::Heap(v) => v.resize_with(new_len, f), @@ -558,7 +560,7 @@ impl TinyVec { } /// Reduces the vec's length to the given value. - /// + /// /// If the vec is already shorter than the input, nothing happens. #[inline] pub fn truncate(&mut self, new_len: usize) { @@ -585,7 +587,7 @@ impl TinyVec { } /// Draining iterator for `TinyVecDrain` -/// +/// /// See [`TinyVecDrain::drain`](TinyVecDrain::::drain) pub struct TinyVecDrain<'p, A: Array> { parent: &'p mut TinyVec, @@ -680,7 +682,7 @@ pub enum TinyVecIterator { #[allow(missing_docs)] Inline(ArrayVecIterator), #[allow(missing_docs)] - Heap(alloc::vec::IntoIter) + Heap(alloc::vec::IntoIter), } impl Iterator for TinyVecIterator { type Item = A::Item; diff --git a/tests/arrayvec.rs b/tests/arrayvec.rs index 2c8e5d8..a9a9381 100644 --- a/tests/arrayvec.rs +++ b/tests/arrayvec.rs @@ -1,7 +1,7 @@ #![allow(bad_style)] -use tinyvec::*; use std::iter::FromIterator; +use tinyvec::*; #[test] fn test_a_vec() { diff --git a/tests/tinyvec.rs b/tests/tinyvec.rs index 7a642ee..705ab59 100644 --- a/tests/tinyvec.rs +++ b/tests/tinyvec.rs @@ -1,8 +1,8 @@ #![allow(bad_style)] #![allow(clippy::redundant_clone)] -use tinyvec::*; use std::iter::FromIterator; +use tinyvec::*; #[test] fn TinyVec_swap_remove() {