stash: rewrite wip
Added some scaffolding.
This commit is contained in:
parent
08b241481f
commit
831b208506
4 changed files with 64 additions and 76 deletions
56
Cargo.lock
generated
56
Cargo.lock
generated
|
|
@ -2,26 +2,6 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
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]]
|
[[package]]
|
||||||
name = "crossbeam-queue"
|
name = "crossbeam-queue"
|
||||||
version = "0.3.12"
|
version = "0.3.12"
|
||||||
|
|
@ -37,45 +17,9 @@ version = "0.8.21"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
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]]
|
[[package]]
|
||||||
name = "swactor"
|
name = "swactor"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytemuck",
|
|
||||||
"crossbeam-queue",
|
"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"
|
|
||||||
|
|
|
||||||
|
|
@ -7,5 +7,4 @@ edition = "2024"
|
||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytemuck = { version = "1.24.0", features = ["derive"] }
|
|
||||||
crossbeam-queue = "0.3.12"
|
crossbeam-queue = "0.3.12"
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,30 @@
|
||||||
use bytemuck::{Pod, Zeroable};
|
|
||||||
use swactor::{ActorAddress, ActorInterface, Message, Runtime};
|
use swactor::{ActorAddress, ActorInterface, Message, Runtime};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Pod, Zeroable)]
|
|
||||||
struct Greeter {
|
struct Greeter {
|
||||||
pub num_greeted: usize,
|
pub num_greeted: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
struct GreetMessage {
|
struct GreetMessage {
|
||||||
name: String,
|
/// who do we greet?
|
||||||
addr: ActorAddress,
|
who: String,
|
||||||
|
|
||||||
|
/// who do we send out greeting back to?
|
||||||
|
return_addr: ActorAddress,
|
||||||
}
|
}
|
||||||
impl Message for GreetMessage {}
|
impl Message for GreetMessage {}
|
||||||
|
|
||||||
|
impl ActorInterface for Greeter {
|
||||||
|
type Message = GreetMessage;
|
||||||
|
type Response = GreetResponse;
|
||||||
|
|
||||||
impl ActorInterface<GreetMessage> for Greeter {
|
|
||||||
fn handle(&mut self, ctx: &Runtime, msg: GreetMessage) {
|
fn handle(&mut self, ctx: &Runtime, msg: GreetMessage) {
|
||||||
let res = GreetResponse(format!("Hello, {}!", msg.name));
|
let res = GreetResponse(format!("Hello, {}!", msg.who));
|
||||||
if let Err(_) = ctx.send(res, msg.addr) {
|
self.num_greeted += 1;
|
||||||
|
if let Err(_) = ctx.send(msg.return_addr, res) {
|
||||||
// no error handling
|
// no error handling
|
||||||
|
self.num_greeted -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -30,5 +35,21 @@ impl Message for GreetResponse {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let rt = Runtime::new();
|
let rt = Runtime::new();
|
||||||
|
let addr = rt.spawn::<Greeter>();
|
||||||
|
let inbox = rt.new_inbox::<GreetResponse>();
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
46
src/lib.rs
46
src/lib.rs
|
|
@ -1,26 +1,40 @@
|
||||||
mod ring_buffer;
|
mod ring_buffer;
|
||||||
use bytemuck::{Pod, Zeroable};
|
|
||||||
use ring_buffer::{Receiver, Sender};
|
use ring_buffer::{Receiver, Sender};
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
|
||||||
pub trait Message: 'static + Sized + Clone {}
|
pub trait Message: 'static + Sized + Clone {}
|
||||||
|
|
||||||
pub trait ActorInterface<M: Message>: Pod + Zeroable {
|
pub trait ActorInterface {
|
||||||
fn handle(&mut self, ctx: &Runtime, msg: M);
|
type Message: Message;
|
||||||
|
type Response: Message;
|
||||||
|
fn handle(&mut self, ctx: &Runtime, msg: Self::Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ActorAddress = u64;
|
pub type ActorAddress = u64;
|
||||||
|
|
||||||
pub struct Actor<S, M, N>
|
pub struct Actor<A>
|
||||||
where
|
where
|
||||||
M: Message,
|
A: ActorInterface,
|
||||||
N: Message,
|
|
||||||
S: ActorInterface<M>,
|
|
||||||
{
|
{
|
||||||
inbox: Receiver<M>,
|
inbox: Receiver<A::Message>,
|
||||||
outbox: Sender<N>,
|
outbox: Sender<A::Response>,
|
||||||
state: S,
|
inner: A,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Inbox<M: Message> {
|
||||||
|
addr: ActorAddress,
|
||||||
|
inner: Receiver<M>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M: Message> Inbox<M> {
|
||||||
|
pub fn addr(&self) -> &ActorAddress {
|
||||||
|
&self.addr
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_recv(&self) -> Option<M> {
|
||||||
|
self.inner.try_recv()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Runtime {
|
pub struct Runtime {
|
||||||
|
|
@ -36,9 +50,19 @@ impl Runtime {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send<M>(&self, msg: M, addr: ActorAddress) -> Result<(), M> {
|
pub fn spawn<A: ActorInterface>(&self) -> ActorAddress {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tick(&self) {}
|
||||||
|
|
||||||
|
pub fn send<M>(&self, addr: ActorAddress, msg: M) -> Result<(), M> {
|
||||||
Err(msg)
|
Err(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_inbox<M: Message>(&self) -> Inbox<M> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MessageRouter<M> {
|
pub struct MessageRouter<M> {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue