Skip to content

Commit

Permalink
Add child and children macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Stupremee committed Dec 31, 2019
1 parent c0f555c commit e9fc034
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bastion/src/lib.rs
Expand Up @@ -70,6 +70,7 @@ mod broadcast;
mod callbacks;
mod child;
mod config;
mod macros;
mod system;

pub mod child_ref;
Expand All @@ -96,4 +97,5 @@ pub mod prelude {
pub use crate::msg;
pub use crate::path::{BastionPath, BastionPathElement};
pub use crate::supervisor::{SupervisionStrategy, Supervisor, SupervisorRef};
pub use crate::{child, children};
}
91 changes: 91 additions & 0 deletions bastion/src/macros.rs
@@ -0,0 +1,91 @@
//! This module contains some useful macros to simply
//! create supervisors and children groups.
#![allow(unused)]

/// This macro creates a new children group with the given amount of workers
/// callbacks, and a closure which should be executed when a message is received.
///
/// # Example
///
/// ```
/// # use bastion::prelude::*;
/// # fn main() {
/// // This creates a new children group with 100 workers
/// // and will call the closure every time a message is received.
/// let children_without_callbacks = children! { 100,
/// ctx, msg => {
/// // use ctx, and msg here
/// }
/// };
///
/// let callbacks = Callbacks::new()
/// .with_before_start(|| println!("Children group started."))
/// .with_after_stop(|| println!("Children group stopped."));
/// // This creates the same children group as above, but with your own callbacks
/// let children_with_callbacks = children! { 100, callbacks,
/// ctx, msg => {
/// // use ctx, and msg here
/// }
/// };
/// # }
/// ```
#[macro_export]
macro_rules! children {
($count:expr, $ctx:ident, $msg:ident => $code:block) => {
children!($count, $crate::Callbacks::default(), $ctx, $msg => $code)
};

($count:expr, $callbacks:expr, $ctx:ident, $msg:ident => $code:block) => {
$crate::Bastion::children(|children: $crate::children::Children| {
children
.with_redundancy($count)
.with_callbacks($callbacks)
.with_exec(|ctx: $crate::context::BastionContext| {
async move {
let $ctx = ctx;
loop {
let $msg = $ctx.recv().await?;
$code;
}
}
})
});
};
}

/// This macro creates a new children group with the only one worker and the given
/// closure as action.
///
/// # Example
///
/// ```
/// # use bastion::prelude::*;
/// # fn main() {
/// // This creates a new children group with 1 worker
/// // and will call the closure every time a message is received.
/// let children_without_callbacks = child! {
/// ctx, msg => {
/// // use ctx, and msg here
/// }
/// };
///
/// let callbacks = Callbacks::new()
/// .with_before_start(|| println!("Children group started."))
/// .with_after_stop(|| println!("Children group stopped."));
/// // This creates the same children group as above, but with your own callbacks
/// let children_with_callbacks = child! { callbacks,
/// ctx, msg => {
/// // use ctx, and msg here
/// }
/// };
/// # }
/// ```
#[macro_export]
macro_rules! child {
($ctx:ident, $msg:ident => $code:block) => {
child!($crate::Callbacks::default(), $ctx, $msg => $code)
};
($callbacks:expr, $ctx:ident, $msg:ident => $code:block) => {
children!(1, $callbacks, $ctx, $msg => $code)
};
}

0 comments on commit e9fc034

Please sign in to comment.