Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy lints #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion benches/arraystring.rs
Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions benches/extend.rs
Expand Up @@ -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]
});
Expand All @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to change the benchmark, it now tests a different function.

v[511]
});
b.bytes = v.capacity() as u64;
Expand Down
15 changes: 5 additions & 10 deletions src/array_string.rs
Expand Up @@ -309,7 +309,7 @@ impl<const CAP: usize> ArrayString<CAP>
/// assert_eq!(s.pop(), None);
/// ```
pub fn pop(&mut self) -> Option<char> {
let ch = match self.chars().rev().next() {
let ch = match self.chars().next_back() {
Some(ch) => ch,
None => return None,
};
Expand Down Expand Up @@ -394,8 +394,8 @@ impl<const CAP: usize> ArrayString<CAP>

/// 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to remove the old comment. Was it incorrect?

///
/// This method uses *debug assertions* to check the validity of `length`
/// and may use other debug assertions.
Expand Down Expand Up @@ -523,17 +523,12 @@ impl<const CAP: usize> Clone for ArrayString<CAP>
fn clone(&self) -> ArrayString<CAP> {
*self
}
fn clone_from(&mut self, rhs: &Self) {
// guaranteed to fit due to types matching.
self.clear();
self.try_push_str(rhs).ok();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

}

impl<const CAP: usize> PartialOrd for ArrayString<CAP>
{
fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
(**self).partial_cmp(&**rhs)
Some(self.cmp(rhs))
}
fn lt(&self, rhs: &Self) -> bool { **self < **rhs }
fn le(&self, rhs: &Self) -> bool { **self <= **rhs }
Expand Down Expand Up @@ -677,7 +672,7 @@ impl<'a, const CAP: usize> TryFrom<fmt::Arguments<'a>> for ArrayString<CAP>
fn try_from(f: fmt::Arguments<'a>) -> Result<Self, Self::Error> {
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)
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/arrayvec.rs
Expand Up @@ -208,11 +208,15 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {

/// 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;
///
Expand Down Expand Up @@ -537,8 +541,8 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {

/// 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was wrong about the old comment?

///
/// This method uses *debug assertions* to check that `length` is
/// not greater than the capacity.
Expand Down Expand Up @@ -664,13 +668,14 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {

/// 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.
Expand Down Expand Up @@ -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() { }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is more readable.


if self.tail_len > 0 {
unsafe {
Expand Down
10 changes: 3 additions & 7 deletions src/errors.rs
Expand Up @@ -13,9 +13,7 @@ pub struct CapacityError<T = ()> {
impl<T> CapacityError<T> {
/// Create a new `CapacityError` from `element`.
pub const fn new(element: T) -> CapacityError<T> {
CapacityError {
element: element,
}
CapacityError { element }
}

/// Extract the overflowing element
Expand All @@ -29,21 +27,19 @@ impl<T> CapacityError<T> {
}
}

const CAPERROR: &'static str = "insufficient capacity";

#[cfg(feature="std")]
/// Requires `features="std"`.
impl<T: Any> Error for CapacityError<T> {}

impl<T> fmt::Display for CapacityError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", CAPERROR)
f.write_str("insufficient capacity")
}
}

impl<T> fmt::Debug for CapacityError<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", "CapacityError", CAPERROR)
write!(f, "CapacityError: {}", self)
}
}

9 changes: 5 additions & 4 deletions tests/tests.rs
Expand Up @@ -300,9 +300,10 @@ fn test_extend_capacity_panic_2() {

#[test]
fn test_is_send_sync() {
let data = ArrayVec::<Vec<i32>, 5>::new();
&data as &dyn Send;
&data as &dyn Sync;
fn assert_send_and_sync<T: Send + Sync>(_: T) {}

let data = ArrayVec::<Vec<i32>, 5>::new();
assert_send_and_sync(data)
}

#[test]
Expand Down Expand Up @@ -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;
}
}
Expand Down