[][src]Crate ghost_actor

A simple, ergonomic, idiomatic, macro for generating the boilerplate to use rust futures tasks in a concurrent actor style.

Example

ghost_actor::ghost_actor! {
    name: pub MyActor,
    error: MyError,
    api: {
        AddOne::add_one(
            "A test function, output adds 1 to input.",
            u32, u32),
    }
}

/// An example implementation of the example MyActor GhostActor.
struct MyActorImpl;

impl MyActorHandler<(), ()> for MyActorImpl {
    fn handle_add_one(
        &mut self,
        input: u32,
    ) -> Result<u32, MyError> {
        Ok(input + 1)
    }
}

impl MyActorImpl {
    /// Rather than using ghost_actor_spawn directly, use this simple spawn.
    pub async fn spawn() -> MyActorSender<()> {
        use futures::future::FutureExt;

        let (sender, driver) = MyActorSender::ghost_actor_spawn(Box::new(|_| {
            async move {
                Ok(MyActorImpl)
            }.boxed().into()
        })).await.unwrap();

        tokio::task::spawn(driver);

        sender
    }
}

async fn async_main() {
    let mut sender = MyActorImpl::spawn().await;

    assert_eq!(43, sender.add_one(42).await.unwrap());

    sender.ghost_actor_shutdown().await.unwrap();

    assert_eq!(
        "Err(GhostError(SendError(SendError { kind: Disconnected })))",
        &format!("{:?}", sender.add_one(42).await),
    );
}

Modules

dependencies

re-exported dependencies to help with macro references

example

Example usage for unit testing and showing documentation generation.

Macros

ghost_actor

The main workhorse macro for constructing GhostActors. This will define the protocol for building GhostActors.

ghost_chan

GhostChan provides a basis for constructing GhostChannels and eventually GhostActors. GhostChan provides differentiated constructor functions, that generate appropriate input and async await output types.

Structs

GhostChanItem

Container for GhostChan messages

Enums

GhostError

Ghost error type.

Traits

GhostChanSend

Sender trait for GhostChan Send subtraits

Type Definitions

GhostActorDriver

This future represents a spawned GhostActor task, you must await or spawn this task into an executor for the actor to function.

GhostActorSpawn

This is the factory callback signature for spawning new actor tasks.

GhostChanRespond

Response callback for an GhostChan message

GhostResult

Ghost result type.