[][src]Struct bastion::context::BastionContext

pub struct BastionContext {
    pub parent: Option<Box<Supervisor>>,
    pub descendants: Vec<BastionChildren>,
    pub bcast_tx: Option<Sender<Box<dyn Message>>>,
    pub bcast_rx: Option<Receiver<Box<dyn Message>>>,
    // some fields omitted
}

Context definition for any lightweight process.

You can use context to:

  • spawn children
  • communicate with other spawned processes
  • communicate with parent

It is used internally for:

  • communication with the system
  • tracking the supervision

Fields

parent: Option<Box<Supervisor>>

Reference to the parent, it is None for root supervisor.

descendants: Vec<BastionChildren>

Container holding children.

bcast_tx: Option<Sender<Box<dyn Message>>>

Send endpoint for system broadcast.

bcast_rx: Option<Receiver<Box<dyn Message>>>

Receive endpoint for system broadcast.

Methods

impl BastionContext[src]

pub fn hook(self)[src]

One-time use hook for spawned processes. You need to have this function for processes which you want to end gracefully by the system after successful completion.

Examples

use bastion::prelude::*;

fn main() {
    Bastion::platform();
    Bastion::spawn(|context: BastionContext, _msg: Box<dyn Message>| {
        println!("root supervisor - spawn_at_root - 1");

        // Rebind to the system
        context.hook();
    }, String::from("A Message"));

    // Comment out to start the system, so runtime can initialize.
    // Bastion::start()
}

pub fn blocking_hook(self)[src]

Forever running hook for spawned processes. You need to have this function for processes which you want to reutilize the process later and handover the control back again to the system after successful completion.

This function is a must to use for receiving signals from supervision and applying supervision strategies.

Examples

use bastion::prelude::*;

fn main() {
    Bastion::platform();
    Bastion::spawn(|context: BastionContext, _msg: Box<dyn Message>| {
        println!("root supervisor - spawn_at_root - 1");

        // Rebind to the system
        context.blocking_hook();
    }, String::from("A Message"));

    // Comment out to start the system, so runtime can initialize.
    // Bastion::start()
}

pub fn spawn<F, M>(self, thunk: F, msg: M, scale: i32) -> Self where
    F: BastionClosure,
    M: Message
[src]

Context level spawn function for child generation from the parent context. This context carries global broadcast for the system. Every context directly tied to the parent process. If you listen broadcast tx/rx pair in the parent process, you can communicate with the children with specific message type.

Bastion doesn't enforce you to use specific Message type or force you to implement traits. Dynamic dispatch is made over heap fat ptrs and that means all message objects can be passed around with heap constructs.

Arguments

  • thunk - User code which will be executed inside the process.
  • msg - Initial message which will be passed to the thunk.
  • scale - How many children will be spawn with given thunk and msg as process body.

Examples

use bastion::prelude::*;

fn main() {
    Bastion::platform();
    Bastion::spawn(|context: BastionContext, _msg: Box<dyn Message>| {
        println!("root supervisor - spawn_at_root - 1");

        // Let's spawn a child from here
        context.clone().spawn(
              |sub_p: BastionContext, sub_msg: Box<dyn Message>| {
                     receive! { sub_msg,
                        i32 => |msg| { println!("An integer message: {}", msg)},
                        _ => println!("Message not known")
                     }

                     /// Use blocking hook to commence restart of children
                     /// that has finished their jobs.
                     sub_p.blocking_hook();
              },
              9999_i32, // Initial message which is passed down to child.
              1, // How many children with this body will be spawned
        );

        // Rebind to the system
        context.blocking_hook();
    }, String::from("A Message"));

    // Comment out to start the system, so runtime can initialize.
    // Bastion::start()
}

Trait Implementations

impl Default for BastionContext[src]

impl Clone for BastionContext[src]

impl Debug for BastionContext[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Shell for T where
    T: Send + Sync + Clone + Any + 'static, 
[src]

impl<T> Message for T where
    T: Shell + Debug
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T

impl<T> Clone for T where
    T: Clone
[src]