From b506d479d393880498fa3107e897aa4d118fd4cb Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 30 Nov 2024 12:05:31 -0500 Subject: [PATCH] Switch all channels to async-std --- Cargo.lock | 2 ++ visions/server/Cargo.toml | 1 + visions/server/Taskfile.yml | 3 ++- visions/server/src/database.rs | 20 +++++++++++--------- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ee0e392..1bcb68c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4284,6 +4284,8 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" name = "visions" version = "0.1.0" dependencies = [ + "async-std", + "async-trait", "authdb", "cool_asserts", "futures", diff --git a/visions/server/Cargo.toml b/visions/server/Cargo.toml index 5bc25d1..8dc0ed8 100644 --- a/visions/server/Cargo.toml +++ b/visions/server/Cargo.toml @@ -25,6 +25,7 @@ lazy_static = "1.5.0" include_dir = "0.7.4" async-trait = "0.1.83" futures = "0.3.31" +async-std = "1.13.0" [dev-dependencies] cool_asserts = "2.0.3" diff --git a/visions/server/Taskfile.yml b/visions/server/Taskfile.yml index 82a446d..d54c919 100644 --- a/visions/server/Taskfile.yml +++ b/visions/server/Taskfile.yml @@ -7,7 +7,8 @@ tasks: test: cmds: - - cargo watch -x 'test -- --nocapture' + # - cargo watch -x 'test -- --nocapture' + - cargo watch -x 'nextest run' dev: cmds: diff --git a/visions/server/src/database.rs b/visions/server/src/database.rs index a960ae6..2f0e9d9 100644 --- a/visions/server/src/database.rs +++ b/visions/server/src/database.rs @@ -10,7 +10,7 @@ use rusqlite::Connection; use rusqlite_migration::Migrations; use serde::{Deserialize, Serialize}; use thiserror::Error; -use tokio::sync::{mpsc, oneshot}; +use async_std::channel::{Sender, Receiver, bounded}; use uuid::Uuid; static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations"); @@ -39,7 +39,7 @@ enum Request { #[derive(Debug)] struct DatabaseRequest { - tx: oneshot::Sender, + tx: Sender, req: Request, } @@ -161,16 +161,18 @@ impl DiskDb { } } -async fn db_handler(db: DiskDb, mut requestor: mpsc::Receiver) { +async fn db_handler(db: DiskDb, mut requestor: Receiver) { println!("Starting db_handler"); - while let Some(DatabaseRequest{ tx, req }) = requestor.recv().await { + while let Ok(DatabaseRequest{ tx, req }) = requestor.recv().await { println!("Request received: {:?}", req); match req { Request::Charsheet(id) => { let sheet = db.charsheet(id); println!("sheet retrieved: {:?}", sheet); match sheet { - Ok(sheet) => tx.send(DatabaseResponse::Charsheet(sheet)).unwrap(), + Ok(sheet) => { + tx.send(DatabaseResponse::Charsheet(sheet)).await.unwrap(); + }, _ => unimplemented!(), } } @@ -180,7 +182,7 @@ async fn db_handler(db: DiskDb, mut requestor: mpsc::Receiver) } pub struct DbConn { - conn: mpsc::Sender, + conn: Sender, handle: tokio::task::JoinHandle<()>, } @@ -189,7 +191,7 @@ impl DbConn { where P: AsRef, { - let (tx, rx) = mpsc::channel(5); + let (tx, rx) = bounded::(5); let db = DiskDb::new(path).unwrap(); let handle = tokio::spawn(async move { db_handler(db, rx).await; }); @@ -204,14 +206,14 @@ impl Database for DbConn { &mut self, id: CharacterId, ) -> Result, Error> { - let (tx, rx) = oneshot::channel::(); + let (tx, rx) = bounded::(1); let request = DatabaseRequest{ tx, req: Request::Charsheet(id), }; self.conn.send(request).await.unwrap(); - match rx.await { + match rx.recv().await { Ok(DatabaseResponse::Charsheet(row)) => Ok(row), Ok(_) => Err(Error::MessageMismatch), Err(err) => {