applied clippy

This commit is contained in:
Ruediger Ludwig 2023-01-29 19:12:22 +01:00
parent 33eb92e9d1
commit 4e08117ed0
16 changed files with 205 additions and 504 deletions

View file

@ -1,12 +1,14 @@
#![allow(dead_code)]
use std::fmt::Display;
use super::{number::Number, pos::Pos};
use num_traits::{Num, NumAssignOps};
use super::pos::Pos;
#[derive(Debug, Clone, Copy, Default)]
pub struct Area<T>
where
T: Number,
T: Num,
{
lower_left: Pos<T>,
upper_right: Pos<T>,
@ -14,7 +16,7 @@ where
impl<T> Area<T>
where
T: Number + Ord,
T: Num + Ord + Copy,
{
pub fn new(p1: Pos<T>, p2: Pos<T>) -> Area<T> {
Area {
@ -22,7 +24,12 @@ where
upper_right: p1.max_components(&p2),
}
}
}
impl<T> Area<T>
where
T: Num + Ord + Copy,
{
pub fn extend(&self, pos: Pos<T>) -> Area<T> {
if self.contains(pos) {
return *self;
@ -33,7 +40,6 @@ where
upper_right: self.upper_right.max_components(&pos),
}
}
pub fn get_lower_left(&self) -> Pos<T> {
self.lower_left
}
@ -52,7 +58,7 @@ where
impl<'a, T> Area<T>
where
T: Number + Ord + 'a,
T: Num + Ord + 'a + Copy,
{
pub fn from_iterator<I>(mut iter: I) -> Option<Self>
where
@ -60,7 +66,7 @@ where
{
let first = *iter.next()?;
let (upper, lower) = iter.fold((first, first), |(mx, mn), p| {
(mx.max_components(&p), mn.min_components(&p))
(mx.max_components(p), mn.min_components(p))
});
Some(Area::new(lower, upper))
@ -69,21 +75,21 @@ where
impl<T> Area<T>
where
T: Number,
T: Num + Copy,
{
pub fn width(&self) -> T {
self.upper_right.x() - self.lower_left.x() + T::ONE
self.upper_right.x() - self.lower_left.x() + T::one()
}
#[allow(dead_code)]
pub fn height(&self) -> T {
self.upper_right.y() - self.lower_left.y() + T::ONE
self.upper_right.y() - self.lower_left.y() + T::one()
}
}
impl<T> Area<T>
where
T: Number,
T: Num + Copy,
{
#[allow(dead_code)]
pub fn area(&self) -> T {
@ -93,7 +99,7 @@ where
impl<T> Display for Area<T>
where
T: Number + Display,
T: Num + Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}-{}]", self.lower_left, self.upper_right)
@ -102,7 +108,7 @@ where
impl<T> Area<T>
where
T: Number,
T: Num + Copy,
{
pub fn cells(&self, ascending: bool) -> CellIterator<'_, T> {
CellIterator::new(self, ascending)
@ -116,7 +122,7 @@ where
#[derive(Debug)]
pub struct RowIterator<'a, T>
where
T: Number,
T: Num + Copy,
{
area: &'a Area<T>,
row: T,
@ -125,7 +131,7 @@ where
impl<'a, T> RowIterator<'a, T>
where
T: Number,
T: Num + Copy,
{
fn new(area: &'a Area<T>, ascending: bool) -> RowIterator<'a, T> {
RowIterator {
@ -142,7 +148,7 @@ where
impl<'a, T> Iterator for RowIterator<'a, T>
where
T: Number,
T: Num + Ord + NumAssignOps + Copy,
{
type Item = Row<'a, T>;
@ -155,9 +161,9 @@ where
row: self.row,
};
if self.ascending {
self.row += T::ONE;
self.row += T::one();
} else {
self.row -= T::ONE;
self.row -= T::one();
}
Some(row)
} else {
@ -169,7 +175,7 @@ where
#[derive(Debug)]
pub struct Row<'a, T>
where
T: Number,
T: Num,
{
area: &'a Area<T>,
row: T,
@ -177,7 +183,7 @@ where
impl<'a, T> Row<'a, T>
where
T: Number,
T: Num + Copy,
{
pub fn cols(&self, ascending: bool) -> ColIterator<'_, T> {
ColIterator {
@ -196,7 +202,7 @@ where
#[derive(Debug)]
pub struct ColIterator<'a, T>
where
T: Number,
T: Num,
{
area: &'a Area<T>,
row: T,
@ -206,7 +212,7 @@ where
impl<'a, T> Iterator for ColIterator<'a, T>
where
T: Number,
T: Num + Ord + NumAssignOps + Copy,
{
type Item = Pos<T>;
fn next(&mut self) -> Option<Self::Item> {
@ -215,10 +221,10 @@ where
{
let pos = Pos::new(self.col, self.row);
if self.ascending {
self.col += T::ONE
self.col += T::one();
} else {
self.col -= T::ONE
};
self.col -= T::one();
}
Some(pos)
} else {
None
@ -229,7 +235,7 @@ where
#[derive(Debug)]
pub struct CellIterator<'a, T>
where
T: Number,
T: Num,
{
area: &'a Area<T>,
row: T,
@ -239,7 +245,7 @@ where
impl<'a, T> CellIterator<'a, T>
where
T: Number,
T: Num + Copy,
{
pub fn new(area: &'a Area<T>, ascending: bool) -> CellIterator<'a, T> {
let (col, row) = if ascending {
@ -258,7 +264,7 @@ where
impl<'a, T> Iterator for CellIterator<'a, T>
where
T: Number,
T: Num + Ord + NumAssignOps + Copy,
{
type Item = Pos<T>;
@ -268,15 +274,15 @@ where
{
let pos = Pos::new(self.col, self.row);
if self.ascending {
self.col += T::ONE;
self.col += T::one();
if self.col > self.area.upper_right.x() {
self.row += T::ONE;
self.row += T::one();
self.col = self.area.lower_left.x();
}
} else {
self.col -= T::ONE;
self.col -= T::one();
if self.col < self.area.lower_left.x() {
self.row -= T::ONE;
self.row -= T::one();
self.col = self.area.upper_right.x();
}
}