[][src]Struct bastion::supervisor::SupervisorRef

pub struct SupervisorRef { /* fields omitted */ }

A "reference" to a Supervisor, allowing to communicate with it.

Implementations

impl SupervisorRef[src]

pub fn id(&self) -> &BastionId[src]

Returns the identifier of the supervisor this SupervisorRef is referencing.

Note that the supervisor's identifier is reset when it is restarted.

Example

let supervisor_ref = Bastion::supervisor(|sp| {
    // ...
}).expect("Couldn't create the supervisor.");

let supervisor_id: &BastionId = supervisor_ref.id();

pub fn supervisor<S>(&self, init: S) -> Result<Self, ()> where
    S: FnOnce(Supervisor) -> Supervisor
[src]

Creates a new Supervisor, passes it through the specified init closure and then sends it to the supervisor this SupervisorRef is referencing to supervise it.

This method returns a SupervisorRef referencing the newly created supervisor if it succeeded, or Err(()) otherwise.

Arguments

  • init - The closure taking the new Supervisor as an argument and returning it once configured.

Example

let sp_ref: SupervisorRef = parent_ref.supervisor(|sp| {
    // Configure the supervisor...
    sp.with_strategy(SupervisionStrategy::OneForOne)
    // ...and return it.
}).expect("Couldn't create the supervisor.");

pub fn children<C>(&self, init: C) -> Result<ChildrenRef, ()> where
    C: FnOnce(Children) -> Children
[src]

Creates a new Children, passes it through the specified init closure and then sends it to the supervisor this SupervisorRef is referencing to supervise it.

This methods returns a ChildrenRef referencing the newly created children group it it succeeded, or Err(()) otherwise.

Arguments

  • init - The closure taking the new Children as an argument and returning it once configured.

Example

let children_ref: ChildrenRef = sp_ref.children(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<SignedMessage> = ctx.try_recv().await;

            // ...and return `Ok(())` or `Err(())` when you are done...
            Ok(())
            // Note that if `Err(())` was returned, the supervisor would
            // restart the children group.
        }
    })
}).expect("Couldn't create the children group.");

pub fn strategy(&self, strategy: SupervisionStrategy) -> Result<(), ()>[src]

Sends to the supervisor this SupervisorRef is referencing the strategy that it should start using when one of its supervised children groups or supervisors dies (in the case of a children group, it could be because one of its elements panicked or returned an error).

The default strategy Supervisor is SupervisionStrategy::OneForOne.

This method returns () if it succeeded, or Err(()) otherwise.

Arguments

  • strategy - The strategy to use:
    • SupervisionStrategy::OneForOne would only restart the supervised children groups or supervisors that fault.
    • SupervisionStrategy::OneForAll would restart all the supervised children groups or supervisors (even those which were stopped) when one of them faults, respecting the order in which they were added.
    • SupervisionStrategy::RestForOne would restart the supervised children groups or supervisors that fault along with all the other supervised children groups or supervisors that were added after them (even the stopped ones), respecting the order in which they were added.

Example

// Note that "one-for-one" is the default strategy.
sp_ref.strategy(SupervisionStrategy::OneForOne);

pub fn broadcast<M: Message>(&self, msg: M) -> Result<(), M>[src]

Sends a message to the supervisor this SupervisorRef is referencing which will then send it to all of its supervised children groups and supervisors.

This method returns () if it succeeded, or Err(msg) otherwise.

Arguments

  • msg - The message to send.

Example

let msg = "A message containing data.";
sp_ref.broadcast(msg).expect("Couldn't send the message.");

// And then in every future of the elements of the children
// groups that are supervised by this supervisor or one of
// its supervised supervisors (etc.)...
msg! { ctx.recv().await?,
    ref msg: &'static str => {
        assert_eq!(msg, &"A message containing data.");
    };
    // We are only broadcasting a `&'static str` in this
    // example, so we know that this won't happen...
    _: _ => ();
}

pub fn stop(&self) -> Result<(), ()>[src]

Sends a message to the supervisor this SupervisorRef is referencing to tell it to stop every running children groups and supervisors that it is supervising.

This method returns () if it succeeded, or Err(()) otherwise.

Example

sp_ref.stop().expect("Couldn't send the message.");

pub fn kill(&self) -> Result<(), ()>[src]

Sends a message to the supervisor this SupervisorRef is referencing to tell it to kill every running children groups and supervisors that it is supervising.

This method returns () if it succeeded, or Err(()) otherwise.

Example

sp_ref.kill().expect("Couldn't send the message.");

Trait Implementations

impl Clone for SupervisorRef[src]

impl Debug for SupervisorRef[src]

impl Eq for SupervisorRef[src]

impl PartialEq<SupervisorRef> for SupervisorRef[src]

Auto Trait Implementations

Blanket Implementations

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

impl<T> AsAny for T where
    T: Any
[src]

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

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

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

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

impl<T> Message for T where
    T: Any + Send + Sync + Debug
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

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

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

type Owned = T

The resulting type after obtaining ownership.

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<V, T> VZip<V> for T where
    V: MultiLane<T>,