Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
frabul committed Oct 22, 2024
1 parent 165e228 commit 914c5a3
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "abcgen"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
authors = ["Francesco Buldo <frbuldo@gmail.com>"]
categories = ["rust-patterns"]
Expand Down Expand Up @@ -33,4 +33,10 @@ log = "0.4.22"
env_logger = "0.11.5"
ab-code-gen = { path = "code_gen" }

[profile.release]
opt-level = 3
strip = true
debug = false
lto = true

[workspace]
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod actor {
}

impl MyActor {
pub async fn start(&mut self, task_sender: tokio::sync::mpsc::Sender<Task<MyActor>>) {
pub async fn start(&mut self, task_sender: TaskSender) {
println!("Starting");
// here you can spawn some tasks using tokio::spawn
// or enqueue some tasks into the actor's main loop by sending them to task_sender
Expand Down Expand Up @@ -104,7 +104,7 @@ mod hello_wordl_actor {

#[actor] // this attribute is used to mark the struct defining the actor
pub struct HelloWorldActor {
pub event_sender: Option<tokio::sync::broadcast::Sender<HelloWorldActorEvent>>,
pub event_sender: Option<EventSender>,
}

impl HelloWorldActor {
Expand All @@ -114,9 +114,9 @@ mod hello_wordl_actor {
&mut self,
// this object can be used to send a function to be invoked in the actor's loop
// see Self::invoke
task_sender: tokio::sync::mpsc::Sender<Task<HelloWorldActor>>,
task_sender: TaskSender,
// this argument should be removed if there are no events
event_sender: tokio::sync::broadcast::Sender<HelloWorldActorEvent>,
event_sender: EventSender,
) {
self.event_sender = Some(event_sender);
println!("Hello, World!");
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
- Helper method that allows functions to subscribe to events
- Helper macros to easily call and define tasks
- implement the possibility to specify channels size
- support generics on message signature
- support generics on the actor
20 changes: 9 additions & 11 deletions examples/actor_prototyping.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! A sandbox example that i used to prototype what the code generator should finally produce.
//! A sandbox example that i used to prototype what the code generator should finally produce.
//! This file is not maintained and could eventually differ from the final implementation.
#![allow(unused)]
Expand Down Expand Up @@ -70,17 +70,17 @@ mod my_actor_module {
});
self.internal_task = Some(internal_task);
}

pub async fn shutdown(&mut self) {
log::info!("Shutting down");
self.termination_requested
.store(true, std::sync::atomic::Ordering::Relaxed);
}

pub async fn dummy_task<'b>(&'b mut self) {
log::info!("Dummy task executed");
}

pub fn dummy_task_2(&mut self) -> PinnedFuture<'_, ()> {
Box::pin(async {
log::info!("Dummy dummy task 2 executed");
Expand All @@ -101,7 +101,7 @@ mod my_actor_module {
42
}
}

#[derive(Debug)]
pub enum MyActorMessage {
DoThis {
Expand All @@ -114,7 +114,7 @@ mod my_actor_module {
respond_to: tokio::sync::oneshot::Sender<i32>,
},
}

pub struct MyActorProxy {
message_sender: tokio::sync::mpsc::Sender<MyActorMessage>,
events: tokio::sync::broadcast::Receiver<MyActorEvent>,
Expand All @@ -127,22 +127,22 @@ mod my_actor_module {
None => false,
}
}

pub fn stop(&mut self) -> Result<(), AbcgenError> {
match self.stop_signal.take() {
Some(tx) => tx.send(()).map_err(|_e: ()| AbcgenError::AlreadyStopped),
None => Err(AbcgenError::AlreadyStopped),
}
}

pub async fn stop_and_wait(&mut self) -> Result<(), AbcgenError> {
self.stop()?;
while self.is_running() {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
Ok(())
}

pub fn get_events(&self) -> tokio::sync::broadcast::Receiver<MyActorEvent> {
self.events.resubscribe()
}
Expand Down Expand Up @@ -245,11 +245,9 @@ mod my_actor_module {
.try_send(task)
.map_err(|e| AbcgenError::ChannelError(Box::new(e)))
}

}
}


// ----------------- main -----------------
#[tokio::main]
async fn main() {
Expand Down
1 change: 0 additions & 1 deletion examples/expand_file.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Example on how to use the ab-code-gen library to generate the code by parsing and modifying a file.
use std::{
io::{self, Write},
thread::spawn,
Expand Down
12 changes: 6 additions & 6 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod hello_wordl_actor {

// the follow attribute is used to mark the enum defining the events that can be signaled by the actor
// events are optional, they can be omitted
#[events]
#[events]
#[derive(Debug, Clone)]
pub enum HelloWorldActorEvent {
SomeoneAskedMyName(String),
Expand All @@ -14,15 +14,15 @@ mod hello_wordl_actor {

#[actor] // this attribute is used to mark the struct defining the actor
pub struct HelloWorldActor {
pub event_sender: Option<tokio::sync::broadcast::Sender<HelloWorldActorEvent>>,
pub event_sender: Option<EventSender>,
}

impl HelloWorldActor {
// following function *must* be implemented by the user and is called by the run function
async fn start(
&mut self,
task_sender: tokio::sync::mpsc::Sender<Task<HelloWorldActor>>, // this can be used to send a function to be invoked in the actor's loop
event_sender: tokio::sync::broadcast::Sender<HelloWorldActorEvent>, // this argument should be removed if there are no events
task_sender: TaskSender, // this can be used to send a function to be invoked in the actor's loop
event_sender: EventSender, // this argument should be removed if there are no events
) {
self.event_sender = Some(event_sender);
println!("Hello, World!");
Expand All @@ -31,7 +31,7 @@ mod hello_wordl_actor {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
// the following call will send the function Self::still_here to be invoked by the actor's loop
// ⚠️ Do not put any sleep in Self::still_here, it will block the actor's loop
Self::invoke(&task_sender, Box::new(Self::still_here)).unwrap();
Self::invoke(&task_sender, Box::new(Self::still_here)).unwrap();
});
}

Expand Down Expand Up @@ -80,7 +80,7 @@ async fn main() {
let actor = HelloWorldActor { event_sender: None };
// the following call will spawn a tokio task that will handle the messages received by the actor
// it consumes the actor and returns a proxy that can be used to send and receive messages
let proxy = actor.run();
let proxy = actor.run();

// handle events sent by the actor
let mut events_rx = proxy.get_events().resubscribe();
Expand Down
8 changes: 2 additions & 6 deletions examples/hello_world_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ mod hello_wordl_actor {
}
#[actor]
pub struct HelloWorldActor {
pub event_sender: Option<tokio::sync::broadcast::Sender<HelloWorldActorEvent>>,
pub event_sender: Option<EventSender>,
}
impl HelloWorldActor {
async fn start(
&mut self,
task_sender: tokio::sync::mpsc::Sender<Task<HelloWorldActor>>,
event_sender: tokio::sync::broadcast::Sender<HelloWorldActorEvent>,
) {
async fn start(&mut self, task_sender: TaskSender, event_sender: EventSender) {
self.event_sender = Some(event_sender);
println!("Hello, World!");
tokio::spawn(async move {
Expand Down
8 changes: 2 additions & 6 deletions examples/print_stuff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use abcgen::actor_module;
#[actor_module]
mod my_actor_module {
use std::sync::{atomic::AtomicBool, Arc};

use abcgen::*;
#[events]
#[derive(Debug, Clone)]
Expand All @@ -25,11 +25,7 @@ mod my_actor_module {

impl MyActor {
// following functions must be implemented by the user
pub async fn start(
&mut self,
task_sender: tokio::sync::mpsc::Sender<Task<MyActor>>,
event_sender: tokio::sync::broadcast::Sender<MyActorEvent>,
) {
pub async fn start(&mut self, task_sender: TaskSender, event_sender: EventSender) {
log::info!("Starting");
let term_req = self.termination_requested.clone();
let internal_task = tokio::spawn(async move {
Expand Down
6 changes: 1 addition & 5 deletions examples/print_stuff_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ mod my_actor_module {
pub(crate) internal_task: Option<tokio::task::JoinHandle<()>>,
}
impl MyActor {
pub async fn start(
&mut self,
task_sender: tokio::sync::mpsc::Sender<Task<MyActor>>,
event_sender: tokio::sync::broadcast::Sender<MyActorEvent>,
) {
pub async fn start(&mut self, task_sender: TaskSender, event_sender: EventSender) {
log::info!("Starting");
let term_req = self.termination_requested.clone();
let internal_task = tokio::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion examples/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod actor {
}

impl MyActor {
pub async fn start(&mut self, task_sender: tokio::sync::mpsc::Sender<Task<MyActor>>) {
pub async fn start(&mut self, task_sender: TaskSender) {
println!("Starting");
// here you can spawn some tasks using tokio::spawn
// or enqueue some tasks into the actor's main loop by sending them to task_sender
Expand Down
2 changes: 1 addition & 1 deletion examples/quick_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod actor {
pub some_value: i32,
}
impl MyActor {
pub async fn start(&mut self, task_sender: tokio::sync::mpsc::Sender<Task<MyActor>>) {
pub async fn start(&mut self, task_sender: TaskSender) {
println!("Starting");
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! }
//!
//! impl MyActor {
//! pub async fn start(&mut self, task_sender: tokio::sync::mpsc::Sender<Task<MyActor>>) {
//! pub async fn start(&mut self, task_sender: TaskSender) {
//! println!("Starting");
//! // here you can spawn some tasks using tokio::spawn
//! // or enqueue some tasks into the actor's main loop by sending them to task_sender
Expand Down

0 comments on commit 914c5a3

Please sign in to comment.