[go: up one dir, main page]

ChildRef

Struct ChildRef 

Source
pub struct ChildRef { /* private fields */ }
Expand description

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

Implementations§

Source§

impl ChildRef

Source

pub fn id(&self) -> &BastionId

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.");
Source

pub fn is_public(&self) -> bool

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.");
Source

pub fn tell_anonymously<M: Message>(&self, msg: M) -> Result<(), M>

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.");
Source

pub fn try_tell_anonymously<M: Message>(&self, msg: M) -> Result<(), SendError>

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.");
Source

pub fn ask_anonymously<M: Message>(&self, msg: M) -> Result<Answer, M>

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`...
    _: _ => ();
}
Source

pub fn try_ask_anonymously<M: Message>( &self, msg: M, ) -> Result<Answer, SendError>

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`...
    _: _ => ();
}
Source

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

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.");
Source

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

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.");
Source

pub fn addr(&self) -> RefAddr

Returns RefAddr for the child

Source

pub fn path(&self) -> &Arc<BastionPath>

Returns the BastionPath of the child

Source

pub fn name(&self) -> &str

Return the name of the child

Trait Implementations§

Source§

impl Clone for ChildRef

Source§

fn clone(&self) -> ChildRef

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChildRef

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for ChildRef

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl PartialEq for ChildRef

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ChildRef

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&mut self) -> &mut (dyn Any + 'static)

Downcast implemented type to Any.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CallHasher for T
where T: Hash + ?Sized,

Source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Message for T
where T: Any + Send + Sync + Debug,

Source§

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