fix: make pedantic clippy happy

This commit is contained in:
Noa Aarts 2024-10-05 16:27:55 +02:00
parent f6f7623458
commit be383ec768
Signed by: noa
GPG key ID: 1850932741EFF672
4 changed files with 100 additions and 100 deletions

View file

@ -9,19 +9,19 @@ pub trait Grid<I, V> {
fn set(&self, x: I, y: I, value: V);
}
pub struct FlutGrid<T> {
pub struct Flut<T> {
size_x: usize,
size_y: usize,
cells: SyncUnsafeCell<Vec<T>>,
}
impl<T: Clone> FlutGrid<T> {
pub fn init(size_x: usize, size_y: usize, value: T) -> FlutGrid<T> {
impl<T: Clone> Flut<T> {
pub fn init(size_x: usize, size_y: usize, value: T) -> Flut<T> {
let mut vec = Vec::with_capacity(size_x * size_y);
for _ in 0..(size_x * size_y) {
vec.push(value.clone());
}
FlutGrid {
Flut {
size_x,
size_y,
cells: vec.into(),
@ -33,7 +33,7 @@ impl<T: Clone> FlutGrid<T> {
}
}
impl<T> FlutGrid<T> {
impl<T> Flut<T> {
fn index(&self, x: Coordinate, y: Coordinate) -> Option<usize> {
let x = x as usize;
let y = y as usize;
@ -44,7 +44,7 @@ impl<T> FlutGrid<T> {
}
}
impl<T> Grid<Coordinate, T> for FlutGrid<T> {
impl<T> Grid<Coordinate, T> for Flut<T> {
fn get(&self, x: Coordinate, y: Coordinate) -> Option<&T> {
self.index(x, y)
.map(|idx| unsafe { &(*self.cells.get())[idx] })
@ -64,21 +64,22 @@ impl<T> Grid<Coordinate, T> for FlutGrid<T> {
}
#[cfg(test)]
#[allow(clippy::needless_return)]
mod tests {
use super::*;
use crate::grid::FlutGrid;
use crate::grid::Flut;
use test::Bencher;
#[tokio::test]
async fn test_grid_init_values() {
let grid = FlutGrid::init(3, 3, 0);
let grid = Flut::init(3, 3, 0);
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 0, 0, 0, 0, 0])
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 0, 0, 0, 0, 0]);
}
#[tokio::test]
async fn test_grid_init_size() {
let grid = FlutGrid::init(800, 600, 0);
let grid = Flut::init(800, 600, 0);
assert_eq!(grid.size_x, 800);
assert_eq!(grid.size_y, 600);
@ -86,30 +87,30 @@ mod tests {
#[tokio::test]
async fn test_grid_set() {
let grid = FlutGrid::init(3, 3, 0);
let grid = Flut::init(3, 3, 0);
grid.set(1, 1, 255);
grid.set(2, 1, 256);
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 256, 0, 0, 0])
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 256, 0, 0, 0]);
}
#[tokio::test]
async fn test_grid_set_out_of_range() {
let grid = FlutGrid::init(3, 3, 0);
let grid = Flut::init(3, 3, 0);
grid.set(1, 1, 255);
grid.set(3, 1, 256);
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 0, 0, 0, 0])
assert_eq!(grid.cells.into_inner(), vec![0, 0, 0, 0, 255, 0, 0, 0, 0]);
}
#[tokio::test]
async fn test_grid_get() {
let grid = FlutGrid::init(3, 3, 0);
let grid = Flut::init(3, 3, 0);
grid.set(1, 2, 222);
assert_eq!(grid.get(1, 2), Some(&222));
}
#[tokio::test]
async fn test_grid_get_out_of_range() {
let grid = FlutGrid::init(3, 3, 0);
let grid = Flut::init(3, 3, 0);
grid.set(3, 1, 256);
assert_eq!(grid.get(3, 1), None);
assert_eq!(grid.get(1, 2), Some(&0));
@ -117,27 +118,27 @@ mod tests {
#[bench]
fn bench_init(b: &mut Bencher) {
b.iter(|| FlutGrid::init(800, 600, 0 as u32))
b.iter(|| Flut::init(800, 600, 0));
}
#[bench]
fn bench_set(b: &mut Bencher) {
let grid = FlutGrid::init(800, 600, 0 as u32);
let grid = Flut::init(800, 600, 0);
b.iter(|| {
let x = test::black_box(293);
let y = test::black_box(222);
let color = test::black_box(293_923);
let color = test::black_box(0x29_39_23);
grid.set(x, y, color);
})
});
}
#[bench]
fn bench_get(b: &mut Bencher) {
let grid = FlutGrid::init(800, 600, 0 as u32);
let grid = Flut::init(800, 600, 0);
b.iter(|| {
let x = test::black_box(293);
let y = test::black_box(222);
grid.get(x, y)
})
});
}
}