Skip to content

Using async/tokio functions in the transition_fn #1

Description

@jgould22

First, thanks for the library it looks pretty cool.

I am trying to create a simple health check state machine that pings an external service and then transitions state based on the response from that service.

I am using tokio as the async runtime and the external connection requires an async call.

I start with the below and then crated some impl for each of the states my service could be in

// Struct that holds the family of states that HealthCheck will contain
struct HealthCheckFamily;

impl Family for HealthCheckFamily {
    // This is the public interface that will be exposed by the Automaton for all Modes in this Family.
    type Base = dyn HealthCheck;

    // This is the type that will be stored in the Automaton and passed into the Automaton::next() function.
    type Mode = Box<dyn HealthCheck>;
}

// This trait defines a common interface for all Modes in ActivityFamily.
//
#[async_trait]
trait HealthCheck: Mode<Family = HealthCheckFamily> {
    async fn update(self: Box<Self>) -> Box<dyn HealthCheck>;
}

However I obviously run into issues when trying to call Automaton::next as it expects a different type than the async function.

I am pretty new to rust so apologies if I am barking up the wrong tree or missing something obvious but is there a way to go about making an async call within my transition function ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions