/Cargo.toml Secret
Created
July 18, 2020 18:08
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "riker-ex" | |
version = "0.1.0" | |
authors = ["Mina Galić <me+git@igalic.co>"] | |
edition = "2018" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
riker = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use riker::actors::*; | |
use std::time::Duration; | |
// Define the messages we'll use | |
#[derive(Clone, Debug)] | |
pub struct Add(i32); | |
#[derive(Clone, Debug)] | |
pub struct Update(i32); | |
#[derive(Clone, Debug)] | |
pub struct Delete(i32); | |
#[derive(Clone, Debug)] | |
pub struct Print; | |
// Define the Actor and use the 'actor' attribute | |
// to specify which messages it will receive | |
#[actor(Add, Update, Delete, Print)] | |
#[derive(Default)] | |
struct Searcher { | |
count: i32, | |
} | |
impl Actor for Searcher { | |
// we used the #[actor] attribute so SearcherMsg is the Msg type | |
type Msg = SearcherMsg; | |
fn recv(&mut self, ctx: &Context<Self::Msg>, msg: Self::Msg, sender: Sender) { | |
// Use the respective Receive<T> implementation | |
self.receive(ctx, msg, sender); | |
} | |
} | |
impl Receive<Add> for Searcher { | |
type Msg = SearcherMsg; | |
fn receive(&mut self, _ctx: &Context<Self::Msg>, msg: Add, _sender: Sender) { | |
self.count += msg.0; | |
} | |
} | |
impl Receive<Update> for Searcher { | |
type Msg = SearcherMsg; | |
fn receive(&mut self, _ctx: &Context<Self::Msg>, _msg: Update, _sender: Sender) { | |
self.count += 0; // nothing to see here | |
} | |
} | |
impl Receive<Delete> for Searcher { | |
type Msg = SearcherMsg; | |
fn receive(&mut self, _ctx: &Context<Self::Msg>, msg: Delete, _sender: Sender) { | |
self.count -= msg.0; | |
} | |
} | |
impl Receive<Print> for Searcher { | |
type Msg = SearcherMsg; | |
fn receive(&mut self, _ctx: &Context<Self::Msg>, _msg: Print, _sender: Sender) { | |
println!("Total counter value: {}", self.count); | |
} | |
} | |
fn main() { | |
let sys = ActorSystem::new().unwrap(); | |
let props = Props::new::<Searcher>(); | |
let searcher = sys.actor_of_props("counteraahh", props).unwrap(); | |
searcher.tell(Add(7), None); | |
searcher.tell(Print, None); | |
searcher.tell(Add(8), None); | |
searcher.tell(Print, None); | |
searcher.tell(Add(2), None); | |
searcher.tell(Print, None); | |
searcher.tell(Delete(4), None); | |
searcher.tell(Print, None); | |
searcher.tell(Update(8), None); | |
searcher.tell(Print, None); | |
// force main to wait before exiting program | |
std::thread::sleep(Duration::from_millis(500)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment