diff --git a/src/lib.rs b/src/lib.rs index 9085752..f5a9480 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,7 @@ impl VecCell { Self { mut_borrow: Cell::new(None), borrows: Cell::new(0), - inner: Vec::with_capacity(capacity) + inner: Vec::with_capacity(capacity), } } @@ -356,7 +356,10 @@ impl VecCell { /// assert_eq!(s[1], 2); /// assert!(s.get(2).is_none()); /// ``` - pub fn borrow_range<'b, R: std::ops::RangeBounds>(&'b self, range: R) -> Option> { + pub fn borrow_range<'b, R: std::ops::RangeBounds>( + &'b self, + range: R, + ) -> Option> { VecRef::from_range(self, range) } @@ -491,9 +494,7 @@ impl VecCell { /// std::mem::drop(x); /// ``` pub fn try_iter<'b>(&'b self) -> impl Iterator>> { - (0..self.len()).map(|index| { - self.borrow(index) - }) + (0..self.len()).map(|index| self.borrow(index)) } /// Resets the [`borrows`](VecCell::borrows) and [`mut_borrow`](VecCell::mut_borrow) counters. @@ -547,7 +548,11 @@ impl VecCell { /// If no reference was [forgotten](std::mem::forget), then `mut_borrow == None` and `borrows == 0`. #[inline] pub fn into_raw_parts(self) -> (Vec>, Option, usize) { - (self.inner, self.mut_borrow.into_inner(), self.borrows.into_inner()) + ( + self.inner, + self.mut_borrow.into_inner(), + self.borrows.into_inner(), + ) } // == Unsafe functions section == @@ -571,7 +576,11 @@ impl VecCell { } /// Constructs a `VecCell` from its raw parts. - pub unsafe fn from_raw_parts(inner: Vec>, mut_borrow: Option, borrows: usize) -> Self { + pub unsafe fn from_raw_parts( + inner: Vec>, + mut_borrow: Option, + borrows: usize, + ) -> Self { Self { inner, borrows: Cell::new(borrows), @@ -599,12 +608,16 @@ impl fmt::Debug for VecCell { f.debug_struct("VecCell") .field("borrows", &self.borrows.get()) .field("mut_borrow", &self.mut_borrow.get()) - .field("inner", &self.try_iter().map(|x| { - match x { - Some(y) => BorrowStatus::Ok(y), - None => BorrowStatus::Borrowed, - } - }).collect::>()) + .field( + "inner", + &self + .try_iter() + .map(|x| match x { + Some(y) => BorrowStatus::Ok(y), + None => BorrowStatus::Borrowed, + }) + .collect::>(), + ) .finish() } } @@ -619,7 +632,7 @@ impl<'a, T: 'a> IntoIterator for &'a VecCell { fn into_iter(self) -> Self::IntoIter { VecCellRefIter { vec: self, - index: 0 + index: 0, } } } @@ -629,7 +642,7 @@ impl<'a, T: 'a> IntoIterator for &'a VecCell { #[derive(Clone)] pub struct VecCellRefIter<'a, T> { vec: &'a VecCell, - index: usize + index: usize, } impl<'a, T> Iterator for VecCellRefIter<'a, T> { @@ -637,12 +650,15 @@ impl<'a, T> Iterator for VecCellRefIter<'a, T> { fn next(&mut self) -> Option { if self.index >= self.vec.len() { - return None + return None; } let res = match self.vec.borrow(self.index) { Some(x) => x, - None => panic!("Error while borrowing immutably element {} of VecCell: already mutably borrowed", self.index), + None => panic!( + "Error while borrowing immutably element {} of VecCell: already mutably borrowed", + self.index + ), }; self.index += 1; @@ -681,7 +697,7 @@ impl IntoIterator for VecCell { /// Panics if a value is currently mutably borrowed fn into_iter(self) -> Self::IntoIter { VecCellIntoIter { - iter: self.inner.into_iter() + iter: self.inner.into_iter(), } } } @@ -718,7 +734,10 @@ impl Clone for VecCell { /// Panics if a value is currently mutably borrowed fn clone(&self) -> Self { VecCell { - inner: self.into_iter().map(|x| UnsafeCell::new((*x).clone())).collect::>(), + inner: self + .into_iter() + .map(|x| UnsafeCell::new((*x).clone())) + .collect::>(), mut_borrow: Cell::new(None), borrows: Cell::new(0), } @@ -731,12 +750,12 @@ impl PartialEq for VecCell { /// Panics if a value in `self` or `other` is currently mutably borrowed when it is encountered in the comparison. fn eq(&self, other: &Self) -> bool { if self.len() != other.len() { - return false + return false; } for (s, o) in self.iter().zip(other.iter()) { if *s != *o { - return false + return false; } } @@ -750,12 +769,12 @@ impl PartialEq> for VecCell { /// Panics if a value in `self` is currently mutably borrowed when it is encountered in the comparison. fn eq(&self, other: &Vec) -> bool { if self.len() != other.len() { - return false + return false; } for (s, o) in self.iter().zip(other.iter()) { if *s != *o { - return false + return false; } } @@ -766,7 +785,10 @@ impl PartialEq> for VecCell { impl From> for VecCell { fn from(vec: Vec) -> Self { VecCell { - inner: vec.into_iter().map(|x| UnsafeCell::new(x)).collect::>(), + inner: vec + .into_iter() + .map(|x| UnsafeCell::new(x)) + .collect::>(), mut_borrow: Cell::new(None), borrows: Cell::new(0), } @@ -783,9 +805,11 @@ impl From> for Vec { impl serde::Serialize for VecCell { fn serialize(&self, serializer: S) -> Result where - S: serde::Serializer + S: serde::Serializer, { - let range = self.borrow_range(..).expect("Cannot borrow immutably VecCell: already borrowed mutably."); + let range = self + .borrow_range(..) + .expect("Cannot borrow immutably VecCell: already borrowed mutably."); (*range).serialize(serializer) } } @@ -794,13 +818,20 @@ impl serde::Serialize for VecCell { impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for VecCell { fn deserialize(deserializer: D) -> Result where - D: serde::Deserializer<'de> + D: serde::Deserializer<'de>, { let vec: Vec = Vec::deserialize(deserializer)?; Ok(Self::from(vec)) } } +impl Default for VecCell { + #[inline(always)] + fn default() -> Self { + VecCell::new() + } +} + #[cfg(test)] mod test; diff --git a/src/test.rs b/src/test.rs index 9daa35d..529b371 100644 --- a/src/test.rs +++ b/src/test.rs @@ -19,7 +19,6 @@ fn test_create_vec() { assert_eq!(vec.len(), 0); assert_eq!(vec.capacity(), usize::MAX); - let vec: VecCell = VecCell::with_capacity(0); assert_eq!(vec.len(), 0); assert_eq!(vec.capacity(), 0); @@ -31,6 +30,21 @@ fn test_create_vec() { assert_eq!(vec.capacity(), usize::MAX); } +#[test] +fn test_default_vec() { + let container: Container = Container::default(); + assert_eq!(container.vec.len(), 0); + let container: Container = Container::default(); + assert_eq!(container.vec.len(), 0); + let container: Container<()> = Container::default(); + assert_eq!(container.vec.len(), 0); + + #[derive(Default)] + struct Container { + vec: VecCell, + } +} + #[test] fn test_push() { let mut vec: VecCell = VecCell::with_capacity(1); @@ -284,7 +298,8 @@ fn test_try_map() { assert!(VecRefMut::try_map(ref_mut, |_value| Err::<&mut (), ()>(())).is_err()); let ref_mut = vec.borrow_mut(5).unwrap(); - let ref_mut: Result, ()> = VecRefMut::try_map(ref_mut, |value| Ok(value.as_mut())); + let ref_mut: Result, ()> = + VecRefMut::try_map(ref_mut, |value| Ok(value.as_mut())); assert!(ref_mut.is_ok()); assert_eq!(ref_mut.unwrap(), -5);