Trait rotor::Machine [] [src]

pub trait Machine: Sized {
    type Context;
    type Seed: Sized;
    fn create(seed: Self::Seed, scope: &mut Scope<Self::Context>) -> Response<Self, Void>;
    fn ready(self, events: EventSet, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>;
    fn spawned(self, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>;
    fn timeout(self, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>;
    fn wakeup(self, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>;

    fn spawn_error(self, _scope: &mut Scope<Self::Context>, error: SpawnError<Self::Seed>) -> Response<Self, Self::Seed> { ... }
}

A trait that every state machine in the loop must implement

Associated Types

type Context

Context type for the state machine

This is a container of the global state for the application

type Seed: Sized

Seed is piece of data that is needed to initialize the machine

It needs Any because it's put into Box object when state machine is failed to create. Hopefully this is not huge limitation.

Note: this is only used to create machines returned by this machine. So unless this machine processses accepting socket this should probably be Void.

Required Methods

fn create(seed: Self::Seed, scope: &mut Scope<Self::Context>) -> Response<Self, Void>

Create a machine from some data

The error should be rare enough so that Box overhead is negligible. Most errors here should be resource exhaustion, like there are no slots in Slab or system limit on epoll watches exceeded.

Note: this method is used internally (by event loop) to create a socket from a Seed returned by this machine. This method should not be used to create machine by external code. Create a machine-specific Type::new method for the purpose.

Note: we don't support spawning more state machines in create handler

fn ready(self, events: EventSet, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>

Socket readiness notification

fn spawned(self, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>

Called after spawn event

This is mostly a continuation event. I.e. when you accept a socket and return a new state machine from ready(). You may wish to accept another socket right now. This is what spawned event is for.

fn timeout(self, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>

Timeout happened

fn wakeup(self, scope: &mut Scope<Self::Context>) -> Response<Self, Self::Seed>

Message received

Note the spurious wakeups are possible, because messages are asynchronous, and state machine is identified by token. Tokens are reused quickly.

So never make this unreachable!() or unimplemented!()

Provided Methods

fn spawn_error(self, _scope: &mut Scope<Self::Context>, error: SpawnError<Self::Seed>) -> Response<Self, Self::Seed>

Called instead of spawned, if there is no slab space

For example, in accept handler you might want to put the thing into temporary storage, stop accepting and wait until slot is empty again.

Note: it's useless to spawn from here if the failure was , it almost certainly will fail again, but may use a timeout

Implementors