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