pub trait AbstractProcess {
    type Arg: Serialize + DeserializeOwned;
    type State;

    fn init(this: ProcessRef<Self>, arg: Self::Arg) -> Self::State;

    fn terminate(_state: Self::State) { ... }
    fn handle_link_trapped(_state: &mut Self::State, _tag: Tag) { ... }
}
Expand description

Types that implement the AbstractProcess trait can be started as processes.

Their state can be mutated through messages and requests. To define a handler for them, use MessageHandler or RequestHandler.

Message provides a send method to send messages to the process, without waiting on a response. Request provides a request method that will block until a response is received.

Example

use lunatic::process::{
    AbstractProcess, Message, MessageHandler, ProcessRef, RequestHandler,
    Request, StartProcess,
};

struct Counter(u32);

impl AbstractProcess for Counter {
    type Arg = u32;
    type State = Self;

    fn init(_: ProcessRef<Self>, start: u32) -> Self {
        Self(start)
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
struct Inc;
impl MessageHandler<Inc> for Counter {
    fn handle(state: &mut Self::State, _: Inc) {
        state.0 += 1;
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
struct Count;
impl RequestHandler<Count> for Counter {
    type Response = u32;

    fn handle(state: &mut Self::State, _: Count) -> u32 {
        state.0
    }
}


let counter = Counter::start(5, None);
counter.send(Inc);
assert_eq!(counter.request(Count), 6);

Required Associated Types

The argument received by the init function.

This argument is sent from the parent to the child and needs to be serializable.

The state of the process.

In most cases this value is set to Self.

Required Methods

Entry function of the new process.

This function is executed inside the new process. It will receive the arguments passed to the start or start_link function by the parent. And will return the starting state of the newly spawned process.

The parent will block on the call of start or start_link until this function finishes. This allows startups to be synchronized.

Provided Methods

Called when a shutdown command is received.

This function will be called if the process is set to catch link deaths with host::api::process::die_when_link_dies(1) and a linked process traps.

Implementors