From bbc6a28f7da209913e13f6265c0799ff7805f90a Mon Sep 17 00:00:00 2001 From: Philippe Renon Date: Thu, 19 Nov 2020 12:24:26 +0100 Subject: [PATCH] clippy: fix len_without_is_empty warnings --- src/base/matrix.rs | 14 -------------- src/base/properties.rs | 28 +++++++++++++++++++++++++--- src/base/vec_storage.rs | 6 ++++++ src/geometry/point.rs | 13 +++++++++++++ src/linalg/permutation_sequence.rs | 5 +++++ 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index e945a594..8035d2f8 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -298,20 +298,6 @@ impl> Matrix { unsafe { Self::from_data_statically_unchecked(data) } } - /// The total number of elements of this matrix. - /// - /// # Examples: - /// - /// ``` - /// # use nalgebra::Matrix3x4; - /// let mat = Matrix3x4::::zeros(); - /// assert_eq!(mat.len(), 12); - #[inline] - pub fn len(&self) -> usize { - let (nrows, ncols) = self.shape(); - nrows * ncols - } - /// The shape of this matrix returned as the tuple (number of rows, number of columns). /// /// # Examples: diff --git a/src/base/properties.rs b/src/base/properties.rs index a3664d48..d1119afe 100644 --- a/src/base/properties.rs +++ b/src/base/properties.rs @@ -10,11 +10,33 @@ use crate::base::storage::Storage; use crate::base::{DefaultAllocator, Matrix, Scalar, SquareMatrix}; impl> Matrix { - /// Indicates if this is an empty matrix. + /// The total number of elements of this matrix. + /// + /// # Examples: + /// + /// ``` + /// # use nalgebra::Matrix3x4; + /// let mat = Matrix3x4::::zeros(); + /// assert_eq!(mat.len(), 12); + /// ``` + #[inline] + pub fn len(&self) -> usize { + let (nrows, ncols) = self.shape(); + nrows * ncols + } + + /// Returns true if the matrix contains no elements. + /// + /// # Examples: + /// + /// ``` + /// # use nalgebra::Matrix3x4; + /// let mat = Matrix3x4::::zeros(); + /// assert!(!mat.is_empty()); + /// ``` #[inline] pub fn is_empty(&self) -> bool { - let (nrows, ncols) = self.shape(); - nrows == 0 || ncols == 0 + self.len() == 0 } /// Indicates if this is a square matrix. diff --git a/src/base/vec_storage.rs b/src/base/vec_storage.rs index 37f31213..03f032e8 100644 --- a/src/base/vec_storage.rs +++ b/src/base/vec_storage.rs @@ -85,6 +85,12 @@ impl VecStorage { pub fn len(&self) -> usize { self.data.len() } + + /// Returns true if the underlying vector contains no elements. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } } impl Into> for VecStorage { diff --git a/src/geometry/point.rs b/src/geometry/point.rs index e0c26054..0684b3b0 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -194,6 +194,19 @@ where self.coords.len() } + /// Returns true if the point contains no elements. + /// + /// # Example + /// ``` + /// # use nalgebra::{Point2, Point3}; + /// let p = Point2::new(1.0, 2.0); + /// assert!(!p.is_empty()); + /// ``` + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// The stride of this point. This is the number of buffer element separating each component of /// this point. #[inline] diff --git a/src/linalg/permutation_sequence.rs b/src/linalg/permutation_sequence.rs index 0b45510c..47255832 100644 --- a/src/linalg/permutation_sequence.rs +++ b/src/linalg/permutation_sequence.rs @@ -144,6 +144,11 @@ where self.len } + /// Returns true if the permutation sequence contains no elements. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// The determinant of the matrix corresponding to this permutation. #[inline] pub fn determinant(&self) -> N {