Struct Callbacks

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

A set of methods that will get called at different states of a Supervisor or Children life.

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

Implementations§

Source§

impl Callbacks

Source

pub fn new() -> Self

Creates a new instance of Callbacks for Supervisor::with_callbacks or Children::with_callbacks.

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

pub fn with_before_start<C>(self, before_start: C) -> Self
where C: Fn() + Send + Sync + 'static,

Sets the method that will get called before the Supervisor or Children is launched if:

  • it was never called before
  • or the supervisor of the supervised element using this callback (or the system) decided to restart it and it was already stopped or killed
  • or the supervisor of the supervised element using this callback (or the system) decided to restart it and it wasn’t already stopped or killed but did not have a callback defined using with_after_restart
§Example
supervisor.children(|children| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Children group started."))
        .with_before_restart(|| println!("Children group restarting."))
        .with_after_restart(|| println!("Children group restarted."))
        .with_after_stop(|| println!("Children group stopped."));

    children
        .with_exec(|ctx| {
            // -- Children group started.
            async move {
                // ...

                // This will stop the children group...
                Ok(())
                // Note that because the children group stopped by itself,
                // if its supervisor restarts it, its `before_start` callback
                // will get called and not `after_restart`.
            }
            // -- Children group stopped.
        })
        .with_callbacks(callbacks)
})
Source

pub fn with_after_start<C>(self, after_start: C) -> Self
where C: Fn() + Send + Sync + 'static,

Sets the method that will get called right after the Supervisor or Children is launched. This method will be called after the child has subscribed to its distributors and dispatchers.

Once the callback has run, the child has caught up it’s message backlog, and is waiting for new messages to process.

§Example
supervisor.children(|children| {
    let callbacks = Callbacks::new()
        .with_after_start(|| println!("Children group ready to process messages."));

    children
        .with_exec(|ctx| {
            // -- Children group started.
            // with_after_start called
            async move {
                // ...

                // This will stop the children group...
                Ok(())
                // Note that because the children group stopped by itself,
                // if its supervisor restarts it, its `before_start` callback
                // will get called and not `after_restart`.
            }
            // -- Children group stopped.
        })
        .with_callbacks(callbacks)
})
Source

pub fn with_before_restart<C>(self, before_restart: C) -> Self
where C: Fn() + Send + Sync + 'static,

Sets the method that will get called before the Supervisor or Children is reset if:

  • the supervisor of the supervised element using this callback (or the system) decided to restart it and it wasn’t already stopped or killed

Note that if this callback isn’t defined but one was defined using with_after_stop, it will get called instead.

§Example
supervisor.children(|children| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Children group started."))
        .with_before_restart(|| println!("Children group restarting."))
        .with_after_restart(|| println!("Children group restarted."))
        .with_after_stop(|| println!("Children group stopped."));

    children
        .with_exec(|ctx| {
            // Once -- Children group started.
            // and then -- Children group restarted.
            async move {
                // ...

                // This will make the children group fault and get
                // restarted by its supervisor...
                Err(())
            }
            // -- Children group restarting.
            // Note that if a `before_restart` wasn't specified for
            // this children group, `after_stop` would get called
            // instead.
        })
        .with_callbacks(callbacks)
})
Source

pub fn with_after_restart<C>(self, after_restart: C) -> Self
where C: Fn() + Send + Sync + 'static,

Sets the method that will get called before the Supervisor or Children is launched if:

  • the supervisor of the supervised element using this callback (or the system) decided to restart it and it wasn’t already stopped or killed

Note that if this callback isn’t defined but one was defined using with_before_start, it will get called instead.

§Example
supervisor.children(|children| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Children group started."))
        .with_before_restart(|| println!("Children group restarting."))
        .with_after_restart(|| println!("Children group restarted."))
        .with_after_stop(|| println!("Children group stopped."));

    children
        .with_exec(|ctx| {
            // Once -- Children group started.
            // and then -- Children group restarted.
            // Note that if a `after_restart` callback wasn't specified
            // for this children group, `before_restart` would get called
            // instead.
            async move {
                // ...

                // This will make the children group fault and get
                // restarted by its supervisor...
                Err(())
            }
            // -- Children group restarting.
        })
        .with_callbacks(callbacks)
})
Source

pub fn with_after_stop<C>(self, after_stop: C) -> Self
where C: Fn() + Send + Sync + 'static,

Sets the method that will get called after the Supervisor or Children is stopped or killed if:

  • the supervisor of the supervised element using this callback (or the system) decided to stop (not restart nor kill) it and it wasn’t already stopped or killed
  • or the supervisor or children group using this callback stopped or killed itself or was stopped or killed by a reference to it
  • or the supervisor of the supervised element using this callback (or the system) decided to restart it and it wasn’t already stopped or killed but did not have a callback defined using with_before_restart
§Example
supervisor.children(|children| {
    let callbacks = Callbacks::new()
        .with_before_start(|| println!("Children group started."))
        .with_before_restart(|| println!("Children group restarting."))
        .with_after_restart(|| println!("Children group restarted."))
        .with_after_stop(|| println!("Children group stopped."));

    children
        .with_exec(|ctx| {
            // -- Children group started.
            async move {
                // ...

                // This will stop the children group...
                Ok(())
            }
            // -- Children group stopped.
            // Note that because the children group stopped by itself,
            // it its supervisor restarts it, its `before_restart` callback
            // will not get called.
        })
        .with_callbacks(callbacks)
})
Source

pub fn has_before_start(&self) -> bool

Returns whether a callback was defined using with_before_start.

§Example
let callbacks = Callbacks::new()
    .with_before_start(|| println!("Children group started."));

assert!(callbacks.has_before_start());
Source

pub fn has_before_restart(&self) -> bool

Returns whether a callback was defined using with_before_restart.

§Example
let callbacks = Callbacks::new()
    .with_before_restart(|| println!("Children group restarting."));

assert!(callbacks.has_before_restart());
Source

pub fn has_after_restart(&self) -> bool

Returns whether a callback was defined using with_after_restart.

§Example
let callbacks = Callbacks::new()
    .with_after_restart(|| println!("Children group restarted."));

assert!(callbacks.has_after_restart());
Source

pub fn has_after_stop(&self) -> bool

Returns whether a callback was defined using with_after_stop.

§Example
let callbacks = Callbacks::new()
    .with_after_stop(|| println!("Children group stopped."));

assert!(callbacks.has_after_stop());

Trait Implementations§

Source§

impl Clone for Callbacks

Source§

fn clone(&self) -> Callbacks

Returns a copy 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 Callbacks

Source§

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

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

impl Default for Callbacks

Source§

fn default() -> Callbacks

Returns the “default value” for a type. Read more

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> 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> MaybeSendSync for T

Source§

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

Source§

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