Home

Awesome

<div align="center"> <h1>CrossBus</h1> <p> <strong>A Platform-Less, Runtime-Less Actor Computing Model</strong> </p> <p>

API Document crates.io

</p> </div>

Overview

CrossBus is an implementation of Actor Computing Model, with the concept that

Currently crossbus is in its alpha version, all APIs and architecture is not stable yet, and evolves very quickly.

Documentation

Feature Flags

To reduce code redundancy and speed up compilation, crossbus use feature flag to mark the necessary modules/functions, Currently here are some supported Features:

How to Use

First of all, add crossbus to Cargo.toml of project

[dependencies]
crossbus = "0.0.6-a"

Types and Imports

define Some types and methods according to your business logic.

let's say a actor to add some number up, Okay, then the message should be numbers the actor should know the result after adding.

use crossbus::prelude::*;

struct Num(uisze);
impl Message for Num {}

struct CrossBus {
    sum: isize
}

Actor Implementation

the actor should be created before using it

we tell crossbus how to create it

impl Actor for CrossBus {
    type Message = Num;
...
    fn create(ctx: &mut Context<Self>) -> Self {
        Self { sum: 0, }
    }
...
}

Message Action & Reaction

Okay, How the actor respond when the message comes? we should tell crossbus.

    ...
    fn action(&mut self, msg: Self::Message, ctx: &mut Context<Self>) {
        self.sum += msg.0;
    }
    ...

So far so good, but how to obtain the final result

it can be available when the actor process all messages and stopped, right?

    ...
    fn stopped(&mut self, _: &mut Context<Self>) {
        println!("final sum: {}", self.sum);
    }
    ...

Done. Congratulation! You just knew the basic routine to use crossbus. the Full code see below.

Example

Here presents a simple demo to sum numbers.

For more examples, you can go to the examples folder and clone the repo and run them locally.

use crossbus::prelude::*;

struct Num(uisze);
impl Message for Num {}

struct CrossBus {
    sum: isize
}
impl Actor for CrossBus {
    type Message = Num;

    fn create(ctx: &mut Context<Self>) -> Self {
        Self { sum: 0, }
    }

    fn action(&mut self, msg: Self::Message, ctx: &mut Context<Self>) {
        self.sum += msg.0;
    }

    fn stopped(&mut self, _: &mut Context<Self>) {
        println!("final sum: {}", self.sum);
        assert_eq!(self.sum, 7);
    }

}

#[main(runtime = tokio)]
async fn main() {
    let (addr, id) = CrossBus::start();
		println!("actor {} started", id);
    let sender = addr.sender();
    sender.send(Num(1)).unwrap();
    sender.send(Num(2)).unwrap();
    sender.send(Num(4)).unwrap();
}

Contributing

Feel Free to Contribute! All issues / bugs-report / feature-request / etc are welcome!

References