diff --git a/Cargo.toml b/Cargo.toml index 02379aa..9df2774 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,5 +12,3 @@ spawned-rt = { version = "0.1.0", path = "rt" } spawned-concurrency = { version = "0.1.0", path = "concurrency" } tracing = { version = "0.1.41", features = ["log"] } tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } - - diff --git a/concurrency/Cargo.toml b/concurrency/Cargo.toml index 9739dbf..06cc95e 100644 --- a/concurrency/Cargo.toml +++ b/concurrency/Cargo.toml @@ -1,8 +1,11 @@ [package] name = "spawned-concurrency" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] spawned-rt = { workspace = true } -tracing = { workspace = true } \ No newline at end of file +tracing = { workspace = true } + +[lib] +path = "./src/lib.rs" diff --git a/concurrency/src/gen_server.rs b/concurrency/src/gen_server.rs index 11ec651..b1318b6 100644 --- a/concurrency/src/gen_server.rs +++ b/concurrency/src/gen_server.rs @@ -2,10 +2,12 @@ //! See examples/name_server for a usage example. use std::{ fmt::Debug, - panic::{AssertUnwindSafe, catch_unwind}, + panic::{catch_unwind, AssertUnwindSafe}, }; -use spawned_rt::{self as rt, JoinHandle, mpsc, oneshot}; +use std::future::Future; + +use spawned_rt::{self as rt, mpsc, oneshot, JoinHandle}; use crate::error::GenServerError; @@ -17,14 +19,13 @@ pub struct GenServerHandle { } impl GenServerHandle { - pub(crate) fn new() -> Self { + pub(crate) fn new(mut initial_state: G::State) -> Self { let (tx, mut rx) = mpsc::channel::>(); let tx_clone = tx.clone(); let mut gen_server: G = GenServer::new(); - let mut state = gen_server.initial_state(); let handle = rt::spawn(async move { if gen_server - .run(&tx_clone, &mut rx, &mut state) + .run(&tx_clone, &mut rx, &mut initial_state) .await .is_err() { @@ -88,8 +89,8 @@ where fn new() -> Self; - fn start() -> GenServerHandle { - GenServerHandle::new() + fn start(initial_state: Self::State) -> GenServerHandle { + GenServerHandle::new(initial_state) } fn run( @@ -176,8 +177,6 @@ where } } - fn initial_state(&self) -> Self::State; - fn handle_call( &mut self, message: Self::InMsg, diff --git a/concurrency/src/lib.rs b/concurrency/src/lib.rs index cae8218..a5cbe79 100644 --- a/concurrency/src/lib.rs +++ b/concurrency/src/lib.rs @@ -7,4 +7,4 @@ mod process; pub use error::GenServerError; pub use gen_server::{CallResponse, CastResponse, GenServer, GenServerHandle, GenServerInMsg}; -pub use process::{Process, ProcessInfo, send}; +pub use process::{send, Process, ProcessInfo}; diff --git a/concurrency/src/process.rs b/concurrency/src/process.rs index bbfc220..b474222 100644 --- a/concurrency/src/process.rs +++ b/concurrency/src/process.rs @@ -1,7 +1,9 @@ //! Process trait and struct to create a process abstraction similar to Erlang processes. //! See examples/ping_pong for a usage example. -use spawned_rt::{self as rt, JoinHandle, mpsc}; +use std::future::Future; + +use spawned_rt::{self as rt, mpsc, JoinHandle}; #[derive(Debug)] pub struct ProcessInfo { diff --git a/examples/bank/Cargo.toml b/examples/bank/Cargo.toml index ff1d05d..4f3144d 100644 --- a/examples/bank/Cargo.toml +++ b/examples/bank/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "bank" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] spawned-rt = { workspace = true } @@ -10,4 +10,4 @@ tracing = { workspace = true } [[bin]] name = "bank" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/examples/bank/src/main.rs b/examples/bank/src/main.rs index 54f7275..db6e7a0 100644 --- a/examples/bank/src/main.rs +++ b/examples/bank/src/main.rs @@ -22,6 +22,8 @@ mod messages; mod server; +use std::collections::HashMap; + use messages::{BankError, BankOutMessage}; use server::Bank; use spawned_concurrency::GenServer as _; @@ -29,7 +31,7 @@ use spawned_rt as rt; fn main() { rt::run(async { - let mut name_server = Bank::start(); + let mut name_server = Bank::start(HashMap::new()); let joe = "Joe".to_string(); diff --git a/examples/bank/src/server.rs b/examples/bank/src/server.rs index 44fe376..87477c0 100644 --- a/examples/bank/src/server.rs +++ b/examples/bank/src/server.rs @@ -48,10 +48,6 @@ impl GenServer for Bank { type Error = BankError; type State = BankState; - fn initial_state(&self) -> Self::State { - HashMap::new() - } - fn new() -> Self { Self {} } diff --git a/examples/name_server/Cargo.toml b/examples/name_server/Cargo.toml index eda8456..66e7c1d 100644 --- a/examples/name_server/Cargo.toml +++ b/examples/name_server/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "name_server" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] spawned-rt = { workspace = true } @@ -10,4 +10,4 @@ tracing = { workspace = true } [[bin]] name = "name_server" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/examples/name_server/src/main.rs b/examples/name_server/src/main.rs index 7acd3e0..8eb69d8 100644 --- a/examples/name_server/src/main.rs +++ b/examples/name_server/src/main.rs @@ -14,6 +14,8 @@ mod messages; mod server; +use std::collections::HashMap; + use messages::NameServerOutMessage; use server::NameServer; use spawned_concurrency::GenServer as _; @@ -21,7 +23,7 @@ use spawned_rt as rt; fn main() { rt::run(async { - let mut name_server = NameServer::start(); + let mut name_server = NameServer::start(HashMap::new()); let result = NameServer::add(&mut name_server, "Joe".to_string(), "At Home".to_string()).await; diff --git a/examples/name_server/src/server.rs b/examples/name_server/src/server.rs index 9520271..6a2d429 100644 --- a/examples/name_server/src/server.rs +++ b/examples/name_server/src/server.rs @@ -37,10 +37,6 @@ impl GenServer for NameServer { Self {} } - fn initial_state(&self) -> Self::State { - HashMap::new() - } - fn handle_call( &mut self, message: InMessage, diff --git a/examples/name_server_with_error/Cargo.toml b/examples/name_server_with_error/Cargo.toml index db0d743..f3caf13 100644 --- a/examples/name_server_with_error/Cargo.toml +++ b/examples/name_server_with_error/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "name_server_with_error" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] spawned-rt = { workspace = true } @@ -10,4 +10,4 @@ tracing = { workspace = true } [[bin]] name = "name_server_with_error" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/examples/name_server_with_error/src/main.rs b/examples/name_server_with_error/src/main.rs index f8329c0..ae4aae2 100644 --- a/examples/name_server_with_error/src/main.rs +++ b/examples/name_server_with_error/src/main.rs @@ -14,6 +14,8 @@ mod messages; mod server; +use std::collections::HashMap; + use messages::NameServerOutMessage; use server::NameServer; use spawned_concurrency::GenServer as _; @@ -21,7 +23,7 @@ use spawned_rt as rt; fn main() { rt::run(async { - let mut name_server = NameServer::start(); + let mut name_server = NameServer::start(HashMap::new()); let result = NameServer::add(&mut name_server, "Joe".to_string(), "At Home".to_string()).await; diff --git a/examples/name_server_with_error/src/server.rs b/examples/name_server_with_error/src/server.rs index 71839bb..1d8d87b 100644 --- a/examples/name_server_with_error/src/server.rs +++ b/examples/name_server_with_error/src/server.rs @@ -36,10 +36,6 @@ impl GenServer for NameServer { type Error = std::fmt::Error; type State = NameServerState; - fn initial_state(&self) -> NameServerState { - HashMap::new() - } - fn new() -> Self { NameServer {} } diff --git a/examples/ping_pong/Cargo.toml b/examples/ping_pong/Cargo.toml index 71105a3..ade63c3 100644 --- a/examples/ping_pong/Cargo.toml +++ b/examples/ping_pong/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ping_pong" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] spawned-rt = { workspace = true } @@ -10,4 +10,4 @@ tracing = { workspace = true } [[bin]] name = "ping_pong" -path = "src/main.rs" \ No newline at end of file +path = "src/main.rs" diff --git a/rt/Cargo.toml b/rt/Cargo.toml index 7c455d8..6b5de34 100644 --- a/rt/Cargo.toml +++ b/rt/Cargo.toml @@ -1,9 +1,12 @@ [package] name = "spawned-rt" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] tokio = { version = "1", features = ["full"] } tracing = { workspace = true } -tracing-subscriber = { workspace = true } \ No newline at end of file +tracing-subscriber = { workspace = true } + +[lib] +path = "./src/lib.rs" diff --git a/rt/src/lib.rs b/rt/src/lib.rs index 848ce5c..5eebdc3 100644 --- a/rt/src/lib.rs +++ b/rt/src/lib.rs @@ -9,15 +9,16 @@ mod tokio; +use std::future::Future; use std::str::FromStr; +use tracing_subscriber::filter::Directive; use tracing_subscriber::EnvFilter; use tracing_subscriber::FmtSubscriber; -use tracing_subscriber::filter::Directive; pub use crate::tokio::mpsc; pub use crate::tokio::oneshot; -pub use crate::tokio::{JoinHandle, Runtime, spawn}; +pub use crate::tokio::{spawn, JoinHandle, Runtime}; pub fn run(future: F) -> F::Output { init_tracing(); diff --git a/rt/src/tokio/mod.rs b/rt/src/tokio/mod.rs index c68d38f..995bfab 100644 --- a/rt/src/tokio/mod.rs +++ b/rt/src/tokio/mod.rs @@ -4,5 +4,5 @@ pub mod oneshot; pub use tokio::{ runtime::Runtime, - task::{JoinHandle, spawn}, + task::{spawn, JoinHandle}, }; diff --git a/rt/src/tokio/mpsc.rs b/rt/src/tokio/mpsc.rs index 62e4977..ec520a6 100644 --- a/rt/src/tokio/mpsc.rs +++ b/rt/src/tokio/mpsc.rs @@ -1,6 +1,6 @@ //! Tokio.rs reexports to prevent tokio dependencies within external code pub use tokio::sync::mpsc::{ - UnboundedReceiver as Receiver, UnboundedSender as Sender, error::SendError, - unbounded_channel as channel, + error::SendError, unbounded_channel as channel, UnboundedReceiver as Receiver, + UnboundedSender as Sender, }; diff --git a/rt/src/tokio/oneshot.rs b/rt/src/tokio/oneshot.rs index f147d7d..682aba2 100644 --- a/rt/src/tokio/oneshot.rs +++ b/rt/src/tokio/oneshot.rs @@ -1,3 +1,3 @@ //! Tokio.rs reexports to prevent tokio dependencies within external code -pub use tokio::sync::oneshot::{Receiver, Sender, channel}; +pub use tokio::sync::oneshot::{channel, Receiver, Sender};