From ccf226f3503590be92809ee9aa4330442504201c Mon Sep 17 00:00:00 2001 From: Noa Aarts Date: Thu, 3 Oct 2024 22:47:20 +0200 Subject: [PATCH] refactor: the syncUnsafeCell is now in the grid --- Cargo.lock | 2 +- src/grid.rs | 22 ++++++++++++---------- src/main.rs | 32 ++++++++++++++------------------ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fcccc56..8150480 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" diff --git a/src/grid.rs b/src/grid.rs index b3d5f04..93f8c29 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -1,13 +1,15 @@ +use std::cell::SyncUnsafeCell; + pub trait Grid { fn get(&self, x: I, y: I) -> Option<&V>; fn get_unchecked(&self, x: I, y: I) -> &V; - fn set(&mut self, x: I, y: I, value: V); + fn set(&self, x: I, y: I, value: V); } pub struct FlutGrid { size_x: usize, size_y: usize, - cells: Vec, + cells: SyncUnsafeCell>, } impl FlutGrid { @@ -19,7 +21,7 @@ impl FlutGrid { return FlutGrid { size_x, size_y, - cells: vec, + cells: vec.into(), }; } } @@ -39,20 +41,20 @@ impl Grid for FlutGrid { fn get(&self, x: u16, y: u16) -> Option<&T> { match self.index(x, y) { None => None, - Some(idx) => Some(&self.cells[idx]), + Some(idx) => Some(unsafe { &(*self.cells.get())[idx] }), } } - fn set(&mut self, x: u16, y: u16, value: T) { + fn set(&self, x: u16, y: u16, value: T) { match self.index(x, y) { None => (), - Some(idx) => self.cells[idx] = value, + Some(idx) => unsafe { (*self.cells.get())[idx] = value }, } } fn get_unchecked(&self, x: u16, y: u16) -> &T { let idx = y as usize * self.size_x + x as usize; - return &self.cells[idx]; + return unsafe { &(*self.cells.get())[idx] }; } } @@ -66,7 +68,7 @@ mod tests { async fn test_grid_init_values() { let grid = FlutGrid::init(3, 3, 0); - assert_eq!(grid.cells, 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] @@ -82,7 +84,7 @@ mod tests { let mut grid = FlutGrid::init(3, 3, 0); grid.set(1, 1, 255); grid.set(2, 1, 256); - assert_eq!(grid.cells, 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] @@ -90,7 +92,7 @@ mod tests { let mut grid = FlutGrid::init(3, 3, 0); grid.set(1, 1, 255); grid.set(3, 1, 256); - assert_eq!(grid.cells, 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] diff --git a/src/main.rs b/src/main.rs index c46b5ed..8622342 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use std::{ time::Duration, }; -use grid::Grid; +use grid::{FlutGrid, Grid}; use tokio::{ io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, net::TcpListener, @@ -35,15 +35,15 @@ const GRID_LENGTH: usize = 1; static COUNTER: AtomicU64 = AtomicU64::new(0); -fn set_pixel_rgba(grids: &mut [grid::FlutGrid], canvas: u8, x: u16, y: u16, rgb: u32) { - match grids.get_mut(canvas as usize) { +fn set_pixel_rgba(grids: &[grid::FlutGrid], canvas: u8, x: u16, y: u16, rgb: u32) { + match grids.get(canvas as usize) { Some(grid) => grid.set(x, y, rgb), None => (), } } -fn get_pixel(grids: &mut [grid::FlutGrid], canvas: u8, x: u16, y: u16) -> Option<&u32> { - match grids.get_mut(canvas as usize) { +fn get_pixel(grids: &[grid::FlutGrid], canvas: u8, x: u16, y: u16) -> Option<&u32> { + match grids.get(canvas as usize) { Some(grid) => return grid.get(x, y), None => return None, } @@ -59,7 +59,7 @@ async fn process_lock< >( reader: &mut R, _writer: &mut W, - grids: &mut [grid::FlutGrid; GRID_LENGTH], + grids: &[grid::FlutGrid; GRID_LENGTH], ) -> io::Result<()> { let amount = reader.read_u16_le().await?; let command = reader.read_u8().await?; @@ -101,7 +101,7 @@ async fn process_lock< }) .map(|z| z) .collect::>(); - match grids.get_mut(aa[0] as usize) { + match grids.get(aa[0] as usize) { Some(grid) => { grid.set( u16::from_le_bytes([aa[1], aa[2]]), @@ -131,7 +131,7 @@ async fn process_msg< >( reader: &mut R, writer: &mut W, - grids: &mut [grid::FlutGrid; GRID_LENGTH], + grids: &[grid::FlutGrid; GRID_LENGTH], ) -> io::Result<()> { let fst = reader.read_u8().await; match fst { @@ -189,7 +189,7 @@ async fn rgb_bin_read( async fn set_px_rgb_bin( reader: &mut R, - grids: &mut [grid::FlutGrid; GRID_LENGTH], + grids: &[grid::FlutGrid; GRID_LENGTH], ) -> io::Result<()> { let (canvas, x, y, r, g, b) = rgb_bin_read(reader).await?; let rgb = u32::from_be_bytes([r, g, b, 0xff]); @@ -201,7 +201,7 @@ async fn set_px_rgb_bin( async fn process_socket( reader: R, writer: W, - grids: &mut [grid::FlutGrid; GRID_LENGTH], + grids: &[grid::FlutGrid; GRID_LENGTH], ) -> io::Result<()> where W: AsyncWriteExt + std::marker::Unpin, @@ -235,16 +235,13 @@ async fn listen_handle() { #[tokio::main] async fn main() -> io::Result<()> { - println!("Start initialisation"); - let grids = [grid::FlutGrid::init(800, 600, 0xff00ffff)]; - assert_eq!(grids.len(), GRID_LENGTH); - let asuc = Arc::new(SyncUnsafeCell::new(grids)); println!("created grids"); + let grids: Arc<[FlutGrid; GRID_LENGTH]> = + [grid::FlutGrid::init(800, 600, 0xff00ffff)].into(); let flut_listener = TcpListener::bind("0.0.0.0:7791").await?; println!("bound flut listener"); - let img_grids = unsafe { asuc.get().as_ref().unwrap() }; let web_listener = TcpListener::bind("0.0.0.0:7792").await?; println!("bound web listener"); @@ -252,11 +249,10 @@ async fn main() -> io::Result<()> { loop { let (mut socket, _) = flut_listener.accept().await?; - let asuc = asuc.clone(); + let grids = grids.clone(); let _ = tokio::spawn(async move { - let grids = unsafe { asuc.get().as_mut().unwrap() }; let (reader, writer) = socket.split(); - match process_socket(reader, writer, grids).await { + match process_socket(reader, writer, &grids).await { Ok(()) => return Ok(()), Err(err) => return Err(err), }