Skip to content

Implement Hash to be clippy compliant #654

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

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion src/array_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<S, Di> ArrayVisitor<S, Di> {
}
}

static ARRAY_FIELDS: &'static [&'static str] = &["v", "dim", "data"];
static ARRAY_FIELDS: &[&str] = &["v", "dim", "data"];

/// **Requires crate feature `"serde-1"`**
impl<'de, A, Di, S> Deserialize<'de> for ArrayBase<S, Di>
Expand Down
9 changes: 3 additions & 6 deletions src/arrayformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ enum PrintableCell {
// where indexes are being omitted.
fn to_be_printed(length: usize, limit: usize) -> Vec<PrintableCell> {
if length <= 2 * limit {
(0..length)
.map(|x| PrintableCell::ElementIndex(x))
.collect()
(0..length).map(PrintableCell::ElementIndex).collect()
} else {
let mut v: Vec<PrintableCell> =
(0..limit).map(|x| PrintableCell::ElementIndex(x)).collect();
let mut v: Vec<PrintableCell> = (0..limit).map(PrintableCell::ElementIndex).collect();
v.push(PrintableCell::Ellipses);
v.extend((length - limit..length).map(|x| PrintableCell::ElementIndex(x)));
v.extend((length - limit..length).map(PrintableCell::ElementIndex));
v
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub unsafe trait DataMut: Data + RawDataMut {
/// accessed with safe code.
///
/// ***Internal trait, see `Data`.***
#[deprecated(note = "use `Data + RawDataClone` instead", since = "0.13")]
#[deprecated(note = "use `Data + RawDataClone` instead", since = "0.13.0")]
pub trait DataClone: Data + RawDataClone {}

#[allow(deprecated)]
Expand Down
11 changes: 10 additions & 1 deletion src/dimension/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// except according to those terms.

use std::cmp::Ordering;
use std::hash::{Hash, Hasher};

/// An axis index.
///
Expand All @@ -15,7 +16,7 @@ use std::cmp::Ordering;
///
/// All array axis arguments use this type to make the code easier to write
/// correctly and easier to understand.
#[derive(Eq, Ord, Hash, Debug)]
#[derive(Eq, Ord, Debug)]
pub struct Axis(pub usize);

impl Axis {
Expand All @@ -28,6 +29,14 @@ impl Axis {

copy_and_clone! {Axis}

// Hash and PartialEq must be explicitly implemented or both default-generated.
// ref: https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
impl Hash for Axis {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}

macro_rules! derive_cmp {
($traitname:ident for $typename:ident, $method:ident -> $ret:ty) => {
impl $traitname for $typename {
Expand Down
13 changes: 11 additions & 2 deletions src/dimension/dim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ use crate::Ix;
/// array[[0, 0]] = 1.;
/// assert_eq!(array.raw_dim(), Dim([3, 2]));
/// ```
#[derive(Copy, Clone, PartialEq, Eq, Default)]
#[derive(Copy, Clone, Eq, Default)]
pub struct Dim<I: ?Sized> {
index: I,
}

impl<I> Dim<I> {
/// Private constructor and accessors for Dim
pub(crate) fn new(index: I) -> Dim<I> {
Dim { index: index }
Dim { index }
}
#[inline(always)]
pub(crate) fn ix(&self) -> &I {
Expand All @@ -65,6 +65,15 @@ where
index.into_dimension()
}

impl<I: ?Sized> PartialEq for Dim<I>
where
I: PartialEq,
{
fn eq(&self, rhs: &Dim<I>) -> bool {
self.index == rhs.index
}
}

impl<I: ?Sized> PartialEq<I> for Dim<I>
where
I: PartialEq,
Expand Down
13 changes: 7 additions & 6 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ pub trait Dimension:
if self.slice().iter().all(|&d| d != 0) {
let mut it = strides.slice_mut().iter_mut().rev();
// Set first element to 1
while let Some(rs) = it.next() {
if let Some(rs) = it.next() {
*rs = 1;
break;
}
let mut cum_prod = 1;
for (rs, dim) in it.zip(self.slice().iter().rev()) {
Expand All @@ -156,9 +155,8 @@ pub trait Dimension:
if self.slice().iter().all(|&d| d != 0) {
let mut it = strides.slice_mut().iter_mut();
// Set first element to 1
while let Some(rs) = it.next() {
if let Some(rs) = it.next() {
*rs = 1;
break;
}
let mut cum_prod = 1;
for (rs, dim) in it.zip(self.slice()) {
Expand Down Expand Up @@ -362,14 +360,14 @@ pub trait Dimension:
}

// Dimension impls

#[allow(clippy::range_plus_one)]
macro_rules! impl_insert_axis_array(
($n:expr) => (
fn insert_axis(&self, axis: Axis) -> Self::Larger {
debug_assert!(axis.index() <= $n);
let mut out = [1; $n + 1];
out[0..axis.index()].copy_from_slice(&self.slice()[0..axis.index()]);
out[axis.index()+1..$n+1].copy_from_slice(&self.slice()[axis.index()..$n]);
out[axis.index()+1..=$n].copy_from_slice(&self.slice()[axis.index()..$n]);
Dim(out)
}
);
Expand Down Expand Up @@ -661,6 +659,7 @@ impl Dimension for Dim<[Ix; 2]> {
}

/// Return stride offset for this dimension and index.
#[allow(clippy::many_single_char_names)]
#[inline]
fn stride_offset_checked(&self, strides: &Self, index: &Self) -> Option<isize> {
let m = get!(self, 0);
Expand Down Expand Up @@ -745,6 +744,7 @@ impl Dimension for Dim<[Ix; 3]> {
}

/// Self is an index, return the stride offset
#[allow(clippy::many_single_char_names)]
#[inline]
fn stride_offset(index: &Self, strides: &Self) -> isize {
let i = get!(index, 0);
Expand All @@ -757,6 +757,7 @@ impl Dimension for Dim<[Ix; 3]> {
}

/// Return stride offset for this dimension and index.
#[allow(clippy::many_single_char_names)]
#[inline]
fn stride_offset_checked(&self, strides: &Self, index: &Self) -> Option<isize> {
let m = get!(self, 0);
Expand Down
14 changes: 8 additions & 6 deletions src/dimension/dynindeximpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ impl<T: Copy + Zero> IxDynRepr<T> {
pub fn copy_from(x: &[T]) -> Self {
if x.len() <= CAP {
let mut arr = [T::zero(); CAP];
for i in 0..x.len() {
arr[i] = x[i];
}
arr[..x.len()].clone_from_slice(&x[..]);
//for i in 0..x.len() {
// arr[i] = x[i];
//}
IxDynRepr::Inline(x.len() as _, arr)
} else {
Self::from(x)
Expand Down Expand Up @@ -121,7 +122,7 @@ impl IxDynImpl {
IxDynImpl(if len < CAP {
let mut out = [1; CAP];
out[0..i].copy_from_slice(&self[0..i]);
out[i + 1..len + 1].copy_from_slice(&self[i..len]);
out[i + 1..=len].copy_from_slice(&self[i..len]);
IxDynRepr::Inline((len + 1) as u32, out)
} else {
let mut out = Vec::with_capacity(len + 1);
Expand Down Expand Up @@ -206,7 +207,8 @@ impl<'a> IntoIterator for &'a IxDynImpl {
type IntoIter = <&'a [Ix] as IntoIterator>::IntoIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self[..].into_iter()
//self[..].into_iter()
self[..].iter()
}
}

Expand All @@ -221,7 +223,7 @@ impl IxDyn {
/// Create a new dimension value with `n` axes, all zeros
#[inline]
pub fn zeros(n: usize) -> IxDyn {
const ZEROS: &'static [usize] = &[0; 4];
const ZEROS: &[usize] = &[0; 4];
if n <= ZEROS.len() {
Dim(&ZEROS[..n])
} else {
Expand Down
21 changes: 14 additions & 7 deletions src/dimension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,11 @@ pub trait DimensionExt {
/// Get the dimension at `axis`.
///
/// *Panics* if `axis` is out of bounds.
#[inline]
fn axis(&self, axis: Axis) -> Ix;

/// Set the dimension at `axis`.
///
/// *Panics* if `axis` is out of bounds.
#[inline]
fn set_axis(&mut self, axis: Axis, value: Ix);
}

Expand Down Expand Up @@ -422,6 +420,7 @@ pub fn do_slice(dim: &mut usize, stride: &mut usize, slice: Slice) -> isize {
/// nonnegative.
///
/// See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
#[allow(clippy::many_single_char_names)]
fn extended_gcd(a: isize, b: isize) -> (isize, (isize, isize)) {
if a == 0 {
(b.abs(), (0, b.signum()))
Expand Down Expand Up @@ -458,6 +457,7 @@ fn extended_gcd(a: isize, b: isize) -> (isize, (isize, isize)) {
///
/// See https://en.wikipedia.org/wiki/Diophantine_equation#One_equation
/// and https://math.stackexchange.com/questions/1656120#1656138
#[allow(clippy::many_single_char_names)]
fn solve_linear_diophantine_eq(a: isize, b: isize, c: isize) -> Option<(isize, isize)> {
debug_assert_ne!(a, 0);
debug_assert_ne!(b, 0);
Expand Down Expand Up @@ -537,14 +537,21 @@ fn arith_seq_intersect(
/// If the slice is empty, then returns `None`, otherwise returns `Some((min, max))`.
fn slice_min_max(axis_len: usize, slice: Slice) -> Option<(usize, usize)> {
let (start, end, step) = to_abs_slice(axis_len, slice);
//if start == end {
// None
//} else {
// if step > 0 {
// Some((start, end - 1 - (end - start - 1) % (step as usize)))
// } else {
// Some((start + (end - start - 1) % (-step as usize), end - 1))
// }
//}
if start == end {
None
} else if step > 0 {
Some((start, end - 1 - (end - start - 1) % (step as usize)))
} else {
if step > 0 {
Some((start, end - 1 - (end - start - 1) % (step as usize)))
} else {
Some((start + (end - start - 1) % (-step as usize), end - 1))
}
Some((start + (end - start - 1) % (-step as usize), end - 1))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/geomspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
Some(Geomspace {
sign: a.signum(),
start: log_a,
step: step,
step,
index: 0,
len: n,
})
Expand Down
1 change: 1 addition & 0 deletions src/impl_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ where
S: RawData<Elem = A>,
{
/// Return an vector with the elements of the one-dimensional array.
#[allow(clippy::map_clone)]
pub fn to_vec(&self) -> Vec<A>
where
A: Clone,
Expand Down
4 changes: 2 additions & 2 deletions src/impl_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl<S: RawDataClone, D: Clone> Clone for ArrayBase<S, D> {
unsafe {
let (data, ptr) = self.data.clone_with_ptr(self.ptr);
ArrayBase {
data: data,
ptr: ptr,
data,
ptr,
dim: self.dim.clone(),
strides: self.strides.clone(),
}
Expand Down
8 changes: 5 additions & 3 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ where
/// let array = Array::from_iter((0..5).map(|x| x * x));
/// assert!(array == arr1(&[0, 1, 4, 9, 16]))
/// ```
#[allow(clippy::should_implement_trait)]
pub fn from_iter<I>(iterable: I) -> Self
where
I: IntoIterator<Item = A>,
Expand Down Expand Up @@ -212,7 +213,7 @@ macro_rules! size_of_shape_checked_unwrap {
($dim:expr) => {
match dimension::size_of_shape_checked($dim) {
Ok(sz) => sz,
Err(_) => panic!(
Err(_e) => panic!(
"ndarray: Shape too large, product of non-zero axis lengths \
overflows isize in shape {:?}",
$dim
Expand Down Expand Up @@ -315,6 +316,7 @@ where
/// visited in arbitrary order.
///
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
#[allow(clippy::identity_conversion)]
pub fn from_shape_fn<Sh, F>(shape: Sh, f: F) -> Self
where
Sh: ShapeBuilder<Dim = D>,
Expand Down Expand Up @@ -422,8 +424,8 @@ where
ArrayBase {
ptr: v.as_mut_ptr(),
data: DataOwned::new(v),
strides: strides,
dim: dim,
strides,
dim,
}
}

Expand Down
Loading