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.
|
||||
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"
|
||||
|
|
|
|||
|
|
@ -7,5 +7,4 @@ edition = "2024"
|
|||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
bytemuck = { version = "1.24.0", features = ["derive"] }
|
||||
crossbeam-queue = "0.3.12"
|
||||
|
|
|
|||
|
|
@ -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<GreetMessage> 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::<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;
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use ring_buffer::{Receiver, Sender};
|
||||
|
||||
pub mod error;
|
||||
|
||||
pub trait Message: 'static + Sized + Clone {}
|
||||
|
||||
pub trait ActorInterface<M: Message>: 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<S, M, N>
|
||||
pub struct Actor<A>
|
||||
where
|
||||
M: Message,
|
||||
N: Message,
|
||||
S: ActorInterface<M>,
|
||||
A: ActorInterface,
|
||||
{
|
||||
inbox: Receiver<M>,
|
||||
outbox: Sender<N>,
|
||||
state: S,
|
||||
inbox: Receiver<A::Message>,
|
||||
outbox: Sender<A::Response>,
|
||||
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 {
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
pub fn new_inbox<M: Message>(&self) -> Inbox<M> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MessageRouter<M> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue