logo

Struct bastion::child_ref::ChildRef[][src]

pub struct ChildRef { /* fields omitted */ }
Expand description

A “reference” to an element of a children group, allowing to communicate with it.

Implementations

Returns the identifier of the children group element this ChildRef is referencing.

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

Example
Bastion::children(|children| {
    children.with_exec(|ctx| {
        async move {
            let child_id: &BastionId = ctx.current().id();
            // ...
        }
    })
}).expect("Couldn't create the children group.");

Returns true if the child this ChildRef is referencing is public, Which means it can receive messages. private ChildRefs reference bastion internal children, such as the heartbeat child for example. This function comes in handy when implementing your own dispatchers.

Example
Bastion::children(|children| {
    children.with_exec(|ctx| {
        async move {
            if ctx.current().is_public() {
                // ...
            }
        }
    })
}).expect("Couldn't create the children group.");

Sends a message to the child this ChildRef is referencing. This message is intended to be used outside of Bastion context when there is no way for receiver to identify message sender

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

Argument
  • msg - The message to send.
Example
// The message that will be "told"...
const TELL_MSG: &'static str = "A message containing data (tell).";

// Create a new child...
Bastion::children(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // ...which will receive the message "told"...
            msg! { ctx.recv().await?,
                msg: &'static str => {
                    assert_eq!(msg, TELL_MSG);
                    // Handle the message...
                };
                // This won't happen because this example
                // only "tells" a `&'static str`...
                _: _ => ();
            }

            Ok(())
        }
    })
}).expect("Couldn't create the children group.");

// Later, the message is "told" to the child...
child_ref.tell_anonymously(TELL_MSG).expect("Couldn't send the message.");

Try to send a message to the child this ChildRef is referencing. This message is intended to be used outside of Bastion context when there is no way for receiver to identify message sender

This method returns () if it succeeded, or a SendError(../child_ref/enum.SendError.html) otherwise.

Argument
  • msg - The message to send.
Example
// The message that will be "told"...
const TELL_MSG: &'static str = "A message containing data (tell).";

// Create a new child...
Bastion::children(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // ...which will receive the message "told"...
            msg! { ctx.recv().await?,
                msg: &'static str => {
                    assert_eq!(msg, TELL_MSG);
                    // Handle the message...
                };
                // This won't happen because this example
                // only "tells" a `&'static str`...
                _: _ => ();
            }

            Ok(())
        }
    })
}).expect("Couldn't create the children group.");

// Later, the message is "told" to the child...
child_ref.try_tell_anonymously(TELL_MSG).expect("Couldn't send the message.");

Sends a message to the child this ChildRef is referencing, allowing it to answer. This message is intended to be used outside of Bastion context when there is no way for receiver to identify message sender

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

Argument
  • msg - The message to send.
Example
// The message that will be "asked"...
const ASK_MSG: &'static str = "A message containing data (ask).";
// The message the will be "answered"...
const ANSWER_MSG: &'static str = "A message containing data (answer).";

// Create a new child...
Bastion::children(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // ...which will receive the message asked...
            msg! { ctx.recv().await?,
                msg: &'static str =!> {
                    assert_eq!(msg, ASK_MSG);
                    // Handle the message...

                    // ...and eventually answer to it...
                    answer!(ctx, ANSWER_MSG);
                };
                // This won't happen because this example
                // only "asks" a `&'static str`...
                _: _ => ();
            }

            Ok(())
        }
    })
}).expect("Couldn't create the children group.");

// Later, the message is "asked" to the child...
let answer: Answer = child_ref.ask_anonymously(ASK_MSG).expect("Couldn't send the message.");

// ...and the child's answer is received...
msg! { answer.await.expect("Couldn't receive the answer."),
    msg: &'static str => {
        assert_eq!(msg, ANSWER_MSG);
        // Handle the answer...
    };
    // This won't happen because this example
    // only answers a `&'static str`...
    _: _ => ();
}

Try to send a message to the child this ChildRef is referencing, allowing it to answer. This message is intended to be used outside of Bastion context when there is no way for receiver to identify message sender

This method returns Answer if it succeeded, or a SendError(../child_ref/enum.SendError.html) otherwise.

Argument
  • msg - The message to send.
Example
// The message that will be "asked"...
const ASK_MSG: &'static str = "A message containing data (ask).";
// The message the will be "answered"...
const ANSWER_MSG: &'static str = "A message containing data (answer).";

// Create a new child...
Bastion::children(|children| {
    children.with_exec(|ctx: BastionContext| {
        async move {
            // ...which will receive the message asked...
            msg! { ctx.recv().await?,
                msg: &'static str =!> {
                    assert_eq!(msg, ASK_MSG);
                    // Handle the message...

                    // ...and eventually answer to it...
                    answer!(ctx, ANSWER_MSG);
                };
                // This won't happen because this example
                // only "asks" a `&'static str`...
                _: _ => ();
            }

            Ok(())
        }
    })
}).expect("Couldn't create the children group.");

// Later, the message is "asked" to the child...
let answer: Answer = child_ref.try_ask_anonymously(ASK_MSG).expect("Couldn't send the message.");

// ...and the child's answer is received...
msg! { answer.await.expect("Couldn't receive the answer."),
    msg: &'static str => {
        assert_eq!(msg, ANSWER_MSG);
        // Handle the answer...
    };
    // This won't happen because this example
    // only answers a `&'static str`...
    _: _ => ();
}

Sends a message to the child this ChildRef is referencing to tell it to stop its execution.

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

Example
    children.with_exec(|ctx: BastionContext| {
        async move {
            // ...which will receive the message asked...
            msg! { ctx.recv().await?,
                msg: &'static str =!> {
                    // Handle the message...

                    // ...and eventually answer to it...
                };
                // This won't happen because this example
                // only "asks" a `&'static str`...
                _: _ => ();
            }

            Ok(())
        }
    })
}).expect("Couldn't create the children group.");
    child_ref.stop().expect("Couldn't send the message.");

Sends a message to the child this ChildRef is referencing to tell it to suicide.

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

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

Returns RefAddr for the child

Returns the BastionPath of the child

Return the name of the child

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Downcast implemented type to Any. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more