diff --git a/Cargo.lock b/Cargo.lock index 04eda39..8639de5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,26 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "bytemuck" -version = "1.24.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fbdf580320f38b612e485521afda1ee26d10cc9884efaaa750d383e13e3c5f4" -dependencies = [ - "bytemuck_derive", -] - -[[package]] -name = "bytemuck_derive" -version = "1.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9abbd1bc6865053c427f7198e6af43bfdedc55ab791faed4fbd361d789575ff" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "crossbeam-queue" version = "0.3.12" @@ -37,45 +17,9 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" -[[package]] -name = "proc-macro2" -version = "1.0.103" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" -dependencies = [ - "proc-macro2", -] - [[package]] name = "swactor" version = "0.1.0" dependencies = [ - "bytemuck", "crossbeam-queue", ] - -[[package]] -name = "syn" -version = "2.0.111" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" diff --git a/Cargo.toml b/Cargo.toml index 95b6f23..b1d9fb1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,4 @@ edition = "2024" crate-type = ["cdylib", "rlib"] [dependencies] -bytemuck = { version = "1.24.0", features = ["derive"] } crossbeam-queue = "0.3.12" diff --git a/examples/hello.rs b/examples/hello.rs index 8ac6d71..d7d0b77 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,25 +1,30 @@ -use bytemuck::{Pod, Zeroable}; use swactor::{ActorAddress, ActorInterface, Message, Runtime}; #[repr(C)] -#[derive(Pod, Zeroable)] struct Greeter { pub num_greeted: usize, } -#[derive(Clone)] +#[derive(Debug, Default, Clone)] struct GreetMessage { - name: String, - addr: ActorAddress, + /// who do we greet? + who: String, + + /// who do we send out greeting back to? + return_addr: ActorAddress, } impl Message for GreetMessage {} +impl ActorInterface for Greeter { + type Message = GreetMessage; + type Response = GreetResponse; -impl ActorInterface for Greeter { fn handle(&mut self, ctx: &Runtime, msg: GreetMessage) { - let res = GreetResponse(format!("Hello, {}!", msg.name)); - if let Err(_) = ctx.send(res, msg.addr) { + let res = GreetResponse(format!("Hello, {}!", msg.who)); + self.num_greeted += 1; + if let Err(_) = ctx.send(msg.return_addr, res) { // no error handling + self.num_greeted -= 1; } } } @@ -30,5 +35,21 @@ impl Message for GreetResponse {} fn main() { let rt = Runtime::new(); + let addr = rt.spawn::(); + let inbox = rt.new_inbox::(); + rt.send( + addr, + GreetMessage { + who: "world".into(), + return_addr: *inbox.addr(), + }, + ).unwrap(); + for _ in 0..3 { + rt.tick(); + } + + let resp = inbox.try_recv().expect("greeter should have said hello"); + + println!("{}", resp.0); } diff --git a/src/lib.rs b/src/lib.rs index 83a600c..3a8cbe4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,26 +1,40 @@ mod ring_buffer; -use bytemuck::{Pod, Zeroable}; use ring_buffer::{Receiver, Sender}; pub mod error; pub trait Message: 'static + Sized + Clone {} -pub trait ActorInterface: Pod + Zeroable { - fn handle(&mut self, ctx: &Runtime, msg: M); +pub trait ActorInterface { + type Message: Message; + type Response: Message; + fn handle(&mut self, ctx: &Runtime, msg: Self::Message); } pub type ActorAddress = u64; -pub struct Actor +pub struct Actor where - M: Message, - N: Message, - S: ActorInterface, + A: ActorInterface, { - inbox: Receiver, - outbox: Sender, - state: S, + inbox: Receiver, + outbox: Sender, + inner: A, +} + +pub struct Inbox { + addr: ActorAddress, + inner: Receiver, +} + +impl Inbox { + pub fn addr(&self) -> &ActorAddress { + &self.addr + } + + pub fn try_recv(&self) -> Option { + self.inner.try_recv() + } } pub struct Runtime { @@ -36,9 +50,19 @@ impl Runtime { } } - pub fn send(&self, msg: M, addr: ActorAddress) -> Result<(), M> { + pub fn spawn(&self) -> ActorAddress { + todo!() + } + + pub fn tick(&self) {} + + pub fn send(&self, addr: ActorAddress, msg: M) -> Result<(), M> { Err(msg) } + + pub fn new_inbox(&self) -> Inbox { + todo!() + } } pub struct MessageRouter {