diff --git a/benches/arraystring.rs b/benches/arraystring.rs index 5b986fa..467c4e1 100644 --- a/benches/arraystring.rs +++ b/benches/arraystring.rs @@ -35,7 +35,7 @@ fn try_push_string(b: &mut Bencher) { b.iter(|| { v.clear(); for ch in input.chars().cycle() { - if !v.try_push(ch).is_ok() { + if v.try_push(ch).is_err() { break; } } diff --git a/benches/extend.rs b/benches/extend.rs index ba33a93..11afa3e 100644 --- a/benches/extend.rs +++ b/benches/extend.rs @@ -38,7 +38,7 @@ fn extend_with_slice(b: &mut Bencher) { let data = [1; 512]; b.iter(|| { v.clear(); - let iter = data.iter().map(|&x| x); + let iter = data.iter().copied(); v.extend(iter); v[511] }); @@ -50,7 +50,7 @@ fn extend_with_write(b: &mut Bencher) { let data = [1; 512]; b.iter(|| { v.clear(); - v.write(&data[..]).ok(); + v.write_all(&data[..]).ok(); v[511] }); b.bytes = v.capacity() as u64; diff --git a/src/array_string.rs b/src/array_string.rs index 1144360..240072e 100644 --- a/src/array_string.rs +++ b/src/array_string.rs @@ -309,7 +309,7 @@ impl ArrayString /// assert_eq!(s.pop(), None); /// ``` pub fn pop(&mut self) -> Option { - let ch = match self.chars().rev().next() { + let ch = match self.chars().next_back() { Some(ch) => ch, None => return None, }; @@ -394,8 +394,8 @@ impl ArrayString /// Set the strings’s length. /// - /// This function is `unsafe` because it changes the notion of the - /// number of “valid” bytes in the string. Use with care. + /// # Safety + /// The data must be initialised up to the length provided. /// /// This method uses *debug assertions* to check the validity of `length` /// and may use other debug assertions. @@ -523,17 +523,12 @@ impl Clone for ArrayString fn clone(&self) -> ArrayString { *self } - fn clone_from(&mut self, rhs: &Self) { - // guaranteed to fit due to types matching. - self.clear(); - self.try_push_str(rhs).ok(); - } } impl PartialOrd for ArrayString { fn partial_cmp(&self, rhs: &Self) -> Option { - (**self).partial_cmp(&**rhs) + Some(self.cmp(rhs)) } fn lt(&self, rhs: &Self) -> bool { **self < **rhs } fn le(&self, rhs: &Self) -> bool { **self <= **rhs } @@ -677,7 +672,7 @@ impl<'a, const CAP: usize> TryFrom> for ArrayString fn try_from(f: fmt::Arguments<'a>) -> Result { use fmt::Write; let mut v = Self::new(); - v.write_fmt(f).map_err(|e| CapacityError::new(e))?; + v.write_fmt(f).map_err(CapacityError::new)?; Ok(v) } } diff --git a/src/arrayvec.rs b/src/arrayvec.rs index e87b3ef..e1be112 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -208,11 +208,15 @@ impl ArrayVec { /// Push `element` to the end of the vector without checking the capacity. /// + /// # Safety + /// /// It is up to the caller to ensure the capacity of the vector is /// sufficiently large. /// /// This method uses *debug assertions* to check that the arrayvec is not full. /// + /// # Example + /// /// ``` /// use arrayvec::ArrayVec; /// @@ -537,8 +541,8 @@ impl ArrayVec { /// Set the vector’s length without dropping or moving out elements /// - /// This method is `unsafe` because it changes the notion of the - /// number of “valid” elements in the vector. Use with care. + /// # Safety + /// The data must be initialised up to the length provided. /// /// This method uses *debug assertions* to check that `length` is /// not greater than the capacity. @@ -664,13 +668,14 @@ impl ArrayVec { /// Return the inner fixed size array. /// - /// Safety: - /// This operation is safe if and only if length equals capacity. + /// # Safety + /// + /// [`Self::len`] must equal [`Self::capacity`]. pub unsafe fn into_inner_unchecked(self) -> [T; CAP] { debug_assert_eq!(self.len(), self.capacity()); + let self_ = ManuallyDrop::new(self); - let array = ptr::read(self_.as_ptr() as *const [T; CAP]); - array + ptr::read(self_.as_ptr() as *const [T; CAP]) } /// Returns the ArrayVec, replacing the original with a new empty ArrayVec. @@ -1013,7 +1018,7 @@ impl<'a, T: 'a, const CAP: usize> Drop for Drain<'a, T, CAP> { // len is currently 0 so panicking while dropping will not cause a double drop. // exhaust self first - while let Some(_) = self.next() { } + for _ in self.by_ref() { } if self.tail_len > 0 { unsafe { diff --git a/src/errors.rs b/src/errors.rs index 7ca3ebc..1f00db2 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -13,9 +13,7 @@ pub struct CapacityError { impl CapacityError { /// Create a new `CapacityError` from `element`. pub const fn new(element: T) -> CapacityError { - CapacityError { - element: element, - } + CapacityError { element } } /// Extract the overflowing element @@ -29,21 +27,19 @@ impl CapacityError { } } -const CAPERROR: &'static str = "insufficient capacity"; - #[cfg(feature="std")] /// Requires `features="std"`. impl Error for CapacityError {} impl fmt::Display for CapacityError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", CAPERROR) + f.write_str("insufficient capacity") } } impl fmt::Debug for CapacityError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}: {}", "CapacityError", CAPERROR) + write!(f, "CapacityError: {}", self) } } diff --git a/tests/tests.rs b/tests/tests.rs index 2f8a5ef..be584f8 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -300,9 +300,10 @@ fn test_extend_capacity_panic_2() { #[test] fn test_is_send_sync() { - let data = ArrayVec::, 5>::new(); - &data as &dyn Send; - &data as &dyn Sync; + fn assert_send_and_sync(_: T) {} + + let data = ArrayVec::, 5>::new(); + assert_send_and_sync(data) } #[test] @@ -594,7 +595,7 @@ fn test_string_push() { let text = "abcαβγ"; let mut s = ArrayString::<8>::new(); for c in text.chars() { - if let Err(_) = s.try_push(c) { + if s.try_push(c).is_err() { break; } }