[][src]Struct bastion::children::Children

pub struct Children { /* fields omitted */ }

A children group that will contain a defined number of elements (set with with_redundancy or 1 by default) all running a future (returned by the closure that is set with with_exec).

When an element of the group stops or panics, all the elements will be stopped as well and the group's supervisor will receive a notice that a stop or panic occurred (note that if a panic occurred, the supervisor will restart the children group and eventually some of its other children, depending on its SupervisionStrategy).

Example

let children_ref: ChildrenRef = Bastion::children(|children| {
    // Configure the children group...
    children.with_exec(|ctx: BastionContext| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<Msg> = 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.
        }
    })
    // ...and return it.
}).expect("Couldn't create the children group.");

Methods

impl Children[src]

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

Returns this children group's identifier.

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

Example

Bastion::children(|children| {
    let children_id: &BastionId = children.id();
    // ...
}).expect("Couldn't create the children group.");

pub fn with_exec<I, F>(self, init: I) -> Self where
    I: Fn(BastionContext) -> F + Send + Sync + 'static,
    F: Future<Output = Result<(), ()>> + Send + 'static, 
[src]

Sets the closure taking a BastionContext and returning a Future that will be used by every element of this children group.

When a new element is started, it will be assigned a new context, pass it to the init closure and poll the returned future until it stops, panics or another element of the group stops or panics.

The returned future's output should be Result<(), ()>.

Arguments

  • init - The closure taking a BastionContext and returning a Future that will be used by every element of this children group.

Example

Bastion::children(|children| {
    children.with_exec(|ctx| {
        async move {
            // Send and receive messages...
            let opt_msg: Option<Msg> = 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 with_redundancy(self, redundancy: usize) -> Self[src]

Sets the number of number of elements this children group will contain. Each element will call the closure passed in with_exec and run the returned future until it stops, panics or another element in the group stops or panics.

The default number of elements a children group contains is 1.

Arguments

  • redundancy - The number of elements this group will contain.

Example

Bastion::children(|children| {
    // Note that "1" is the default number of elements.
    children.with_redundancy(1)
}).expect("Couldn't create the children group.");

pub fn with_callbacks(self, callbacks: Callbacks) -> Self[src]

Sets the callbacks that will get called at this children group's different lifecycle events.

See Callbacks's documentation for more information about the different callbacks available.

Arguments

  • callbacks - The callbacks that will get called for this children group.

Example

Bastion::children(|children| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Children group started."))
        .with_after_stop(|| println!("Children group stopped."));

    children
        .with_callbacks(callbacks)
        .with_exec(|ctx| {
            // -- Children group started.
            async move {
                // ...
            }
            // -- Children group stopped.
        })
}).expect("Couldn't create the children group.");

Trait Implementations

impl Debug for Children[src]

Auto Trait Implementations

impl Send for Children

impl Sync for Children

impl Unpin for Children

impl !UnwindSafe for Children

impl !RefUnwindSafe for Children

Blanket Implementations

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

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

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

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

type Error = !

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> Borrow<T> for T where
    T: ?Sized
[src]

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

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

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,