From: MegaBrutal Date: Sun, 17 Jul 2022 23:15:00 +0000 (+0200) Subject: Force actix-web to differentiate between canvases X-Git-Url: http://git.megabrutal.com/?p=litoprism.git;a=commitdiff_plain;h=c2e3debc19aa75958fb6c260c9ab2636b659cc98 Force actix-web to differentiate between canvases --- diff --git a/src/main.rs b/src/main.rs index 4618550..f502a9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,6 +146,9 @@ impl Canvas { } } +struct Canvas0(Option); +struct Canvas1(Option); + #[get("/hello/{name}")] async fn greet(name: web::Path, req: HttpRequest) -> impl Responder { println!("{:?}", req); @@ -240,13 +243,13 @@ async fn img_gen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result< } #[get("/pgen/0/{data}")] -async fn img_pgen0(req: HttpRequest, canvas0: web::Data>>>) -> Result { +async fn img_pgen0(req: HttpRequest, canvas0: web::Data>>) -> Result { let data = req.uri().path().split("/").skip(3).next().unwrap(); let mut cursor = Cursor::new(Vec::new()); let scale = 16; let img: RgbImage = ({ let canvas_option = &mut *canvas0.lock().unwrap(); - Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), canvas_option, None, data)?.img.clone()) + Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, None, data)?.img.clone()) } as Result)?; let (tdim_x, tdim_y) = img.dimensions(); let (tdim_x, tdim_y) = (tdim_x * scale, tdim_y * scale); @@ -258,12 +261,12 @@ async fn img_pgen0(req: HttpRequest, canvas0: web::Data } #[get("/pgen/1/{dim_x}/{dim_y}/{scale}/{data}")] -async fn img_pgen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1: web::Data>>>) -> Result { +async fn img_pgen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1: web::Data>>) -> Result { let (dim_x, dim_y, scale) = path.into_inner(); let data = req.uri().path().split("/").skip(6).next().unwrap(); let img: RgbImage = ({ let canvas_option = &mut *canvas1.lock().unwrap(); - Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), canvas_option, Some((dim_x, dim_y)), data)?.img.clone()) + Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone()) } as Result)?; let (tdim_x, tdim_y) = img.dimensions(); let (tdim_x, tdim_y) = (tdim_x * scale, tdim_y * scale); @@ -279,8 +282,8 @@ async fn img_pgen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1: #[actix_web::main] // or #[tokio::main] async fn main() -> std::io::Result<()> { env_logger::init(); - let canvas0 = Arc::new(Mutex::new(Some(Canvas::new(32, 32)))); - let canvas1 = Arc::new(Mutex::new(None as Option)); + let canvas0 = Arc::new(Mutex::new(Canvas0(Some(Canvas::new(32, 32))))); + let canvas1 = Arc::new(Mutex::new(Canvas1(None))); HttpServer::new(move || { App::new() .service(greet)