Trait apocalypse::Demon

source ·
pub trait Demon: Sized + Send + 'static {
    type Input;
    type Output;

    // Required method
    fn handle(
        &mut self,
        message: Self::Input
    ) -> impl Future<Output = Self::Output> + Send;

    // Provided methods
    fn spawned(
        &mut self,
        _location: Location<Self>
    ) -> impl Future<Output = ()> + Send { ... }
    fn vanquished(self) -> impl Future<Output = ()> + Send { ... }
    fn id(&self) -> String { ... }
    fn multiple_id() -> &'static str { ... }
}
Expand description

Demon trait

Demons are actors in the apocalypse framework. Implement this trait in your actors to allow them to reply to messages.

Required Associated Types§

Required Methods§

source

fn handle( &mut self, message: Self::Input ) -> impl Future<Output = Self::Output> + Send

Handler function for messages

This is the main function, called for every message that the broker receives.

use apocalypse::Demon;

struct EchoBot;

impl Demon for EchoBot {
    type Input = String;
    type Output = String;
 
    // Basic implementation of an echo handle function
    async fn handle(&mut self, message: Self::Input) -> Self::Output {
        message
    }
}

Provided Methods§

source

fn spawned( &mut self, _location: Location<Self> ) -> impl Future<Output = ()> + Send

Function that is called when a demon is spawned

By default, the function does nothing.

use apocalypse::{Demon, Location};

struct EchoBot;

impl Demon for EchoBot {
    type Input = String;
    type Output = String;
     
    // Callback for demon spawning
    async fn spawned(&mut self, location: Location<Self>) {
        log::debug!("Spawned echo bot with location {}", location);
    }
     
    // Basic implementation of an echo handle function
    async fn handle(&mut self, message: Self::Input) -> Self::Output {
        message
    }
}
source

fn vanquished(self) -> impl Future<Output = ()> + Send

Function that is called when a demon is removed

By default, the function does nothing.

use apocalypse::{Demon, Location};

struct EchoBot;

impl Demon for EchoBot {
    type Input = String;
    type Output = String;
     
    // Basic implementation of an echo handle function
    async fn handle(&mut self, message: Self::Input) -> Self::Output {
        message
    }

    // Callback function 
    async fn vanquished(self) {
        log::debug!("Killed echo bot");
    }
}
source

fn id(&self) -> String

This id will be printed in the debug logs of the demon’s thread.

It is useful when some lockup is happening and you have trouble to find it.

source

fn multiple_id() -> &'static str

This id will be printed in the debug logs of the demon’s thread, in case the spawn_multiple function is used.

It is useful when some lockup is happening and you have trouble to find it.

Object Safety§

This trait is not object safe.

Implementors§