Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }


7 changes: 5 additions & 2 deletions concurrency/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[package]
name = "spawned-concurrency"
version = "0.1.0"
edition = "2024"
edition = "2021"

[dependencies]
spawned-rt = { workspace = true }
tracing = { workspace = true }
tracing = { workspace = true }

[lib]
path = "./src/lib.rs"
17 changes: 8 additions & 9 deletions concurrency/src/gen_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,14 +19,13 @@ pub struct GenServerHandle<G: GenServer + 'static> {
}

impl<G: GenServer> GenServerHandle<G> {
pub(crate) fn new() -> Self {
pub(crate) fn new(mut initial_state: G::State) -> Self {
let (tx, mut rx) = mpsc::channel::<GenServerInMsg<G>>();
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()
{
Expand Down Expand Up @@ -88,8 +89,8 @@ where

fn new() -> Self;

fn start() -> GenServerHandle<Self> {
GenServerHandle::new()
fn start(initial_state: Self::State) -> GenServerHandle<Self> {
GenServerHandle::new(initial_state)
}

fn run(
Expand Down Expand Up @@ -176,8 +177,6 @@ where
}
}

fn initial_state(&self) -> Self::State;

fn handle_call(
&mut self,
message: Self::InMsg,
Expand Down
2 changes: 1 addition & 1 deletion concurrency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
4 changes: 3 additions & 1 deletion concurrency/src/process.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
Expand Down
4 changes: 2 additions & 2 deletions examples/bank/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bank"
version = "0.1.0"
edition = "2024"
edition = "2021"

[dependencies]
spawned-rt = { workspace = true }
Expand All @@ -10,4 +10,4 @@ tracing = { workspace = true }

[[bin]]
name = "bank"
path = "src/main.rs"
path = "src/main.rs"
4 changes: 3 additions & 1 deletion examples/bank/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
mod messages;
mod server;

use std::collections::HashMap;

use messages::{BankError, BankOutMessage};
use server::Bank;
use spawned_concurrency::GenServer as _;
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();

Expand Down
4 changes: 0 additions & 4 deletions examples/bank/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/name_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "name_server"
version = "0.1.0"
edition = "2024"
edition = "2021"

[dependencies]
spawned-rt = { workspace = true }
Expand All @@ -10,4 +10,4 @@ tracing = { workspace = true }

[[bin]]
name = "name_server"
path = "src/main.rs"
path = "src/main.rs"
4 changes: 3 additions & 1 deletion examples/name_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
mod messages;
mod server;

use std::collections::HashMap;

use messages::NameServerOutMessage;
use server::NameServer;
use spawned_concurrency::GenServer as _;
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;
Expand Down
4 changes: 0 additions & 4 deletions examples/name_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ impl GenServer for NameServer {
Self {}
}

fn initial_state(&self) -> Self::State {
HashMap::new()
}

fn handle_call(
&mut self,
message: InMessage,
Expand Down
4 changes: 2 additions & 2 deletions examples/name_server_with_error/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "name_server_with_error"
version = "0.1.0"
edition = "2024"
edition = "2021"

[dependencies]
spawned-rt = { workspace = true }
Expand All @@ -10,4 +10,4 @@ tracing = { workspace = true }

[[bin]]
name = "name_server_with_error"
path = "src/main.rs"
path = "src/main.rs"
4 changes: 3 additions & 1 deletion examples/name_server_with_error/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
mod messages;
mod server;

use std::collections::HashMap;

use messages::NameServerOutMessage;
use server::NameServer;
use spawned_concurrency::GenServer as _;
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;
Expand Down
4 changes: 0 additions & 4 deletions examples/name_server_with_error/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/ping_pong/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ping_pong"
version = "0.1.0"
edition = "2024"
edition = "2021"

[dependencies]
spawned-rt = { workspace = true }
Expand All @@ -10,4 +10,4 @@ tracing = { workspace = true }

[[bin]]
name = "ping_pong"
path = "src/main.rs"
path = "src/main.rs"
7 changes: 5 additions & 2 deletions rt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 }
tracing-subscriber = { workspace = true }

[lib]
path = "./src/lib.rs"
5 changes: 3 additions & 2 deletions rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: Future>(future: F) -> F::Output {
init_tracing();
Expand Down
2 changes: 1 addition & 1 deletion rt/src/tokio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pub mod oneshot;

pub use tokio::{
runtime::Runtime,
task::{JoinHandle, spawn},
task::{spawn, JoinHandle},
};
4 changes: 2 additions & 2 deletions rt/src/tokio/mpsc.rs
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 1 addition & 1 deletion rt/src/tokio/oneshot.rs
Original file line number Diff line number Diff line change
@@ -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};